Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By Fubarable

Thread: Simple Graphics program not working.

  1. #1
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Simple Graphics program not working.

    Hi, i copied this code from the Java Stanford lectures video 26 on youtube around 34:37.
    Lecture 26 | Programming Methodology (Stanford)
    The program should show a window with "Hello" in the middle, then when you click the screen the text "Hello" should move to where you click it.
    The program below just shows a window with no text. When the lecturer runs the exact same program it works fine.

    Obviously i have made an error somewhere but i cant see where. Any help would be greatly appreciated. Thanks.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MovingLabel extends JComponent implements MouseListener  {
    	
    	public MovingLabel(String labelText, int startX, int startY){
    		text = labelText;
    		x = startX;
    		y = startY;
    		addMouseListener(this);
    	}
    	
    	public void paintCompontent(Graphics g){
    		g.drawString(text, x, y);
    	}
    
    	//Implementing the MouseListener Interface
    	public void mouseClicked(MouseEvent e){
    		x = e.getX();
    		y = e.getY();
    		repaint();
    	}
    
    	public void mouseEntered(MouseEvent arg0) {}
    
    	public void mouseExited(MouseEvent arg0) {}
    
    	public void mousePressed(MouseEvent arg0) {}
    
    	public void mouseReleased(MouseEvent arg0) {}
    
    
    	//instance variables
    	String text;
    	int x;
    	int y;
    }

    Java Code:
    import javax.swing.*;
    public class MovingLabelTest {
    	
    	public static void main(String [] args){
    		JFrame frame = new JFrame("Interactive Hello");				
    		frame.add(new MovingLabel("Hello", 200, 200));
    		frame.setSize(500, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Graphics program not working.

    When you add something to a JFrame you're actually adding something to its ContentPane; a ContentPane has a BorderLayout by default (see the API documentation for the JFrame class and the JRootPane class).
    A BorderLayout doesn't allow anything to move in it. Get rid of that LayoutManager.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: Simple Graphics program not working.

    Hi Jos,

    Im still not exactly sure what i need to modify in my existing code.
    Im new to this so im still a bit slow.

    Thanks for your help.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Graphics program not working.

    Remove the LayoutManager from the ContentPane in your frame:

    Java Code:
    frame.getContentPane().setLayout(null);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: Simple Graphics program not working.

    Do you know why the code i wrote above worked for the lecturer in the youtube video i mentioned?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple Graphics program not working.

    Use the @Override annotation to be sure that any methods you're trying to override are actually and truly overriding a super method. If you did this, you would eventually find out your hard to catch spelling mistake.

    hint: it has to do with painting.
    jcarosella10 and forms like this.

  7. #7
    forms is offline Member
    Join Date
    Nov 2011
    Posts
    23
    Rep Power
    0

    Default Re: Simple Graphics program not working.

    Thank you so much for your help. A silly little spelling mistake caused me so much trouble.
    Thanks for the tip, will be sure to use this when i have a similar problem.
    I need to go away and work on my spelling as well as my Java. Thanks.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple Graphics program not working.

    You're quite welcome. I didn't catch the mistake myself until I used the @Override annotation, a useful trick that I use a lot.

Similar Threads

  1. Simple draw line program not working
    By forms in forum New To Java
    Replies: 7
    Last Post: 01-24-2012, 04:51 PM
  2. Replies: 4
    Last Post: 12-03-2011, 04:38 PM
  3. Drawing simple graphics in NetBeans
    By arifin in forum AWT / Swing
    Replies: 1
    Last Post: 11-12-2010, 10:01 AM
  4. Graphics program
    By ccie007 in forum New To Java
    Replies: 6
    Last Post: 10-27-2010, 08:08 PM
  5. Simple Graphics
    By Freakzoyd in forum New To Java
    Replies: 6
    Last Post: 10-05-2010, 02:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •