Any help would be appreciated. For some reason it isn't working please help A.S.A.P.


Code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class FinalProject extends JApplet implements MouseListener
{
    private boolean mouseClick;
    private Image[][] board;
    Point empty;
   
    public void init()
    {
        setBackground(Color.red);
        board = new Image[4][4];
        board[0][0] = getImage(getCodeBase(), "One.png");
        board[0][1] = getImage(getCodeBase(), "Two.png");
        board[0][2] = getImage(getCodeBase(), "Three.png");
        board[0][3] = getImage(getCodeBase(), "Four.png");
        board[1][0] = getImage(getCodeBase(), "Five.png");
        board[1][1] = getImage(getCodeBase(), "Six.png");
        board[1][2] = getImage(getCodeBase(), "Seven.png");
        board[1][3] = getImage(getCodeBase(), "Eight.png");
        board[2][0] = getImage(getCodeBase(), "Nine.png");
        board[2][1] = getImage(getCodeBase(), "Ten.png");
        board[2][2] = getImage(getCodeBase(), "Eleven.png");
        board[2][3] = getImage(getCodeBase(), "Tweleve.png");
        board[3][0] = getImage(getCodeBase(), "Thirteen.png");
        board[3][1] = getImage(getCodeBase(), "Fourteen.png");
        board[3][2] = getImage(getCodeBase(), "Fifteen.png");
        board[3][3] = null;
        Point empty = new Point(3,3);
        mouseClick = false;
        addMouseListener(this);
    }
   
    public void mouseClicked(MouseEvent e)
    {
        showStatus(""+e.getX()+ ", " + e.getY());
        mouseClick = true;
        Point toEmpty = new Point(e.getX()/100-1,e.getY()/100-1);
        if ((toEmpty.getX() == (empty.getX())) || (toEmpty.getY() == (empty.getY())) && ((toEmpty.getX() == (empty.getX() + 1)) || (toEmpty.getY() == (empty.getY() + 1)) || (toEmpty.getX() == (empty.getX() - 1)) || (toEmpty.getY() == (empty.getY() - 1))))
        {
            board[(int)empty.getY()][(int)empty.getX()] = board[(int)toEmpty.getY()][(int)toEmpty.getX()];
            board[(int)toEmpty.getY()][(int)toEmpty.getX()] = null;
            empty = toEmpty;
        }
        repaint();
    }
   
    public void mouseEntered(MouseEvent e)
    {
       
    }
   
    public void mouseExited(MouseEvent e)
    {
       
    }
   
    public void mousePressed(MouseEvent e)
    {
       
    }
   
    public void mouseReleased(MouseEvent e)
    {
       
    }
   
    public void paint(Graphics g)
    {
        g.setColor(Color.lightGray);
        g.fillRect(100,100, 400,400);
        pieces(g);
        Board(g);
    }
   
    public void Board(Graphics g)
    {
        g.setColor(Color.darkGray);
        g.drawLine(100,100, 100,500);
        g.drawLine(200,100, 200,500);
        g.drawLine(300,100, 300,500);
        g.drawLine(400,100, 400,500);
        g.drawLine(500,100, 500,500);
        g.drawLine(100,100, 500,100);
        g.drawLine(100,200, 500,200);
        g.drawLine(100,300, 500,300);
        g.drawLine(100,400, 500,400);
        g.drawLine(100,500, 500,500);
    }
   
    public void pieces(Graphics g)
    {
        g.drawImage(board[0][0],100,100,this);
        g.drawImage(board[0][1],200,100,this);
        g.drawImage(board[0][2],300,100,this);
        g.drawImage(board[0][3],400,100,this);
        g.drawImage(board[1][0],100,200,this);
        g.drawImage(board[1][1],200,200,this);
        g.drawImage(board[1][2],300,200,this);
        g.drawImage(board[1][3],400,200,this);
        g.drawImage(board[2][0],100,300,this);
        g.drawImage(board[2][1],200,300,this);
        g.drawImage(board[2][2],300,300,this);
        g.drawImage(board[2][3],400,300,this);
        g.drawImage(board[3][0],100,400,this);
        g.drawImage(board[3][1],200,400,this);
        g.drawImage(board[3][2],300,400,this);
        g.drawImage(board[3][3],400,400,this);
    }
   
}
 
More details. Just saying "it doesn't work" and pasting some code is a waste of everybody's time.
Smells like homework to me.
it works fine but the pieces won't move at all.
yesterday when I ran the program it worked just fine, then I added a bit of code to shuffle the cards and it dosen't work any more. Even when I changed it back to how it looked before it still dosen't work.
Do you have an archived copy to revert to? My guess is that you added stuff in more than one section of the program and forgot about it.

Look it over and break it down in your head. Add comments if you need to remember what's going, where and why. You're bound to do d what's wrong at that point.
Tari wrote:
More details. Just saying "it doesn't work" and pasting some code is a waste of everybody's time.
This. I think you need to read a few tutorials on tracing errors in code. Smile In fact, I know an upcoming book that has a whole chapter devoted to planning programs and debugging them... Wink Also, not to gang up on you, but the title of this topic is not descriptive, and does not follow the general netiquette around here.
To make it a lot easier to debug and maintain, I would suggest extracting functions where complexity lies. So for example, the giant if statement in your mouseClicked function is really complicated. You could move a parenthesis and break everything. I would move it to a function with a good name that returns a boolean. That way instead of:

Code:

if(tons of stuff) {
    even more stuff!
}

you get:

Code:

if(aptlyNamedFunctionThatShouldBeObviousWhatItDoesByItsNameAndNotTooLongLikeThisOneIs()) {
    doYourThing();
}


It helps in debugging a lot more than you think it does, because you can write that function and say "I know that does exactly what I want it to." This way, you get the guarantee that even if you ever edit other parts of the code, that still does what you want it to.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement