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.
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;
}
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);
}
}
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
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.
Re: Simple Graphics program not working.
Remove the LayoutManager from the ContentPane in your frame:
Code:
frame.getContentPane().setLayout(null);
kind regards,
Jos
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. :o:
hint: it has to do with painting.
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.
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.