Click to See Complete Forum and Search --> : Why wont simple grahpics program work?


kogo50
May 8th, 2010, 06:35 AM
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);
}
}

keang
May 8th, 2010, 10:02 AM
public void paintComponet(Graphics g){
Check your spelling.

This shows why you should use the @Override annotation when overriding methods - if you had done so your IDE would have warned you that you weren't overriding a method ie your method name or signature was wrong.

dlorde
May 8th, 2010, 10:09 AM
A good IDE should allow you to pick from a list of superclass methods, and insert an overload for you. This helps prevent typos.

If you want to increase your success rate, double your failure rate...
T. J. Watson

kogo50
May 8th, 2010, 11:19 AM
Thanks for the help. It works now. I have to learn how to spell.