CEMETECH
Leading The Way To The Future
Login [Register]
Username:
Password:
Autologin:

Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 127 users online: 12 members, 86 guests and 29 bots.
Members: CalebHansberry, GISdude, lafferjm, legodude, Link, ProgrammerNerd, Spyro543, Tari, willwac.
Bots: Alexa (1), VoilaBot (2), Spinn3r (1), Magpie Crawler (4), Yahoo! Slurp (1), Googlebot (20).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
Author Message
krazylegodrummer56


Power User


Joined: 20 Nov 2011
Posts: 404

Posted: 15 Jun 2012 04:19:01 pm    Post subject: Help my java Program isn't working!!!

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);
    }
   
}
 

_________________
Projects: Learn how to program in Prizm C
First C program!
Tic-Tac-Toe: Get it HERE!




Back to top
Tari


Systems Integrator


Joined: 03 Jul 2006
Posts: 2106
Location: Always-winter, Michigan

Posted: 15 Jun 2012 05:13:40 pm    Post subject:

More details. Just saying "it doesn't work" and pasting some code is a waste of everybody's time.
_________________


Ask questions the smart way · タリ
Back to top
allynfolksjr


Minor Calculator Deity


Joined: 20 Apr 2005
Posts: 1273

Posted: 15 Jun 2012 09:14:16 pm    Post subject:

Smells like homework to me.
_________________
http://tcpa.calcg.org/
Back to top
krazylegodrummer56


Power User


Joined: 20 Nov 2011
Posts: 404

Posted: 15 Jun 2012 11:41:36 pm    Post subject:

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.
_________________
Projects: Learn how to program in Prizm C
First C program!
Tic-Tac-Toe: Get it HERE!




Back to top
comicIDIOT


Guru


Joined: 01 May 2006
Posts: 5105
Location: SFBA, California

Posted: 16 Jun 2012 12:42:57 am    Post subject:

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.
_________________


-Alex
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55736
Location: Earth, Sol, Milky Way

Posted: 16 Jun 2012 06:24:29 am    Post subject:

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.
_________________


Back to top
graphmastur


Power User


Joined: 27 Jul 2010
Posts: 464

Posted: 16 Jun 2012 01:57:28 pm    Post subject:

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.
Back to top
Display posts from previous:   
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
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 
Jump to:  
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

© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.034789 seconds.