Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help me find my misstake. Only 80 rows of code.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Help me find my misstake. Only 80 rows of code.

    Hi, I started to follow some tutorial on YouTube but now I'm really stuck. The window pups up but the little box that's suppose to follow my mouse doesn't show. And the colors doesn't change when click.
    Thanks for the help, I appreciate it!
    package mouse;
     
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
     
    import javax.swing.JFrame;
     
    public class Mouse extends JFrame implements MouseMotionListener {
     
    	private Image dpImage;
    	private Graphics dbg;
     
    	boolean mouseDragged;
     
    	int mx, my;
     
    	public Mouse(){
    		setSize (400, 300);
    		setVisible(true);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		addMouseMotionListener(this);
     
     
    	}
     
     
    	@Override
    	public void paint(Graphics g){
    		dpImage = createImage(getWidth(), getHeight());
    		dbg = dpImage.getGraphics();
    		paintComponents(dbg);
    		g.drawImage(dpImage, 0, 0, this);
    	}
     
    	public void paintComponet(Graphics g) {
    		if(mouseDragged){
    			g.setColor(Color.DARK_GRAY);
    			g.fillRect(0, 0, getWidth(), getHeight());
    			g.setColor(Color.LIGHT_GRAY);
    			g.fillRect(mx, my, 20, 20);
    		}
    		else{
    			g.setColor(Color.LIGHT_GRAY);
    			g.fillRect(0, 0, getWidth(), getHeight());
    			g.setColor(Color.DARK_GRAY);
    			g.fillRect(mx, my, 20, 20);
    		}
     
     
    		repaint();
    	}
     
    	public static void main (String[] args){
    		Mouse mouse = new Mouse();
    	}
     
     
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		mx = e.getX()-10;
    		my = e.getY()-10;
     
    		mouseDragged = true;
     
    		e.consume();
    	}
     
     
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		mx = e.getX();
    		my = e.getY();
     
    		mouseDragged = false;
     
    		e.consume();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help me find my misstake. Only 80 rows of code.

    Several problems:
    You should NOT override both paint() and paintComponent().
    You should NOT override either method of the JFrame class. NOTE: JFrame doesn't have a paintComponent() method.
    If you want to do custom drawing, create a class that extends the JPanel class, override its paintComponent() method and add an instance of that class to the JFrame object.
    The repaint() method should NOT be called from a paint method.
    Why is there a call to the paintComponents() method?

    Just noticed that the method's name is close to paintComponent() but different. Why use a name that can cause confusion. It's less confusing if you Give your method's unique names.

    To see if and when the mouse listeners are called, add a call to the println() method to them that prints a message when they are called.
    Move the call to repaint() to the listener methods.
    Last edited by Norm; March 29th, 2013 at 04:47 PM. Reason: Added note re calling repaint from listener
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following 2 Users Say Thank You to Norm For This Useful Post:

    curmudgeon (March 29th, 2013), Zexus (March 29th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me find my misstake. Only 80 rows of code.

    Okay thanks for the help mate. If you want and have time could you help me find find some tutorials and tell me WHY I should not do these things that you said?

    Once again, Thanks!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help me find my misstake. Only 80 rows of code.

    See the tutorial on custom painting: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Zexus (March 29th, 2013)

Similar Threads

  1. Cannot find symbol error in my java code
    By haliza hadi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2013, 07:20 PM
  2. [SOLVED] Can't find mistake in my code
    By Daler in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 19th, 2012, 12:19 AM
  3. Cannot find Error in Beginner code!
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2012, 08:44 PM
  4. Can't Find what is wrong with my Code
    By Raptorman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 04:12 PM
  5. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM

Tags for this Thread