Simple Grahpic porgram wont work.
I am a new programmer and I'm reading the "Sams Teach Yourself Java in 24 Hours" and I'm doing a chapter on the color class. The program displays two rectangles on blue and on a random color I made and also a string which is red. But when I run it the only thing that comes up is the frame. I'm using NetBeans in linux. Here is the code for a class I named Peach and for a class named eric(I know there dumb names but they are just random names I thought of.) Can someone tell me why it doesn't work. It works in the book but not for me.
Peach class
Code :
import java.awt.*;
import javax.swing.*;
public class Peach extends JPanel{
public void paintComponet(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.fillRect(25, 25, 100, 30);
g.setColor(new Color(190, 81, 215));
g.fillRect(25, 65, 100, 30);
g.setColor(Color.RED);
g.drawString("this is text", 25, 120);
}
}
eric class
Code :
import javax.swing.*;
public class eric{
public static void main(String[] args){
JFrame f = new JFrame("Title");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,250);
Peach p = new Peach();
f.add(p);
f.setVisible(true);
}
}
Re: Simple Grahpic porgram wont work.
Spelling....
Code :
public void paintComponent(Graphics g){//not paintComponet
Using the @Override annotation lets the compiler know you are overriding a function and would not have compiled your original code
Code :
@Override public void paintComponent(Graphics g){
...
}
@Override public void paintComponet(Graphics g){//compile time error
...
}
Re: Simple Grahpic porgram wont work.
Thanks for the help. It works now. I have to learn how to spell.
Re: Simple Grahpic porgram wont work.
You also need to pay attention to casing (upper or lower case). Java's quite picky about casing, too.