Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple Grahpic porgram wont work.

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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
    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);
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple Grahpic porgram wont work.

    Spelling....
    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
    @Override public void paintComponent(Graphics g){
    ...
    }
    @Override public void paintComponet(Graphics g){//compile time error
    ...
    }

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Grahpic porgram wont work.

    Thanks for the help. It works now. I have to learn how to spell.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

Similar Threads

  1. [SOLVED] Can't get JFreeChart to work
    By igniteflow in forum Java SE APIs
    Replies: 2
    Last Post: February 15th, 2011, 02:19 AM
  2. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM
  3. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM
  4. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM