Results 1 to 8 of 8
- 03-09-2012, 03:15 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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); } }
- 03-09-2012, 03:40 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-10-2012, 05:48 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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.
- 03-10-2012, 05:59 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Simple Graphics program not working.
Remove the LayoutManager from the ContentPane in your frame:
kind regards,Java Code:frame.getContentPane().setLayout(null);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-10-2012, 07:02 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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?
-
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.
- 03-11-2012, 03:24 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 23
- Rep Power
- 0
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.
-
Similar Threads
-
Simple draw line program not working
By forms in forum New To JavaReplies: 7Last Post: 01-24-2012, 04:51 PM -
Moving graphics with arrow keys simple problem
By zFader in forum Java 2DReplies: 4Last Post: 12-03-2011, 04:38 PM -
Drawing simple graphics in NetBeans
By arifin in forum AWT / SwingReplies: 1Last Post: 11-12-2010, 10:01 AM -
Graphics program
By ccie007 in forum New To JavaReplies: 6Last Post: 10-27-2010, 08:08 PM -
Simple Graphics
By Freakzoyd in forum New To JavaReplies: 6Last Post: 10-05-2010, 02:47 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks