CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    1

    Why wont simple grahpics program 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);
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Why wont simple grahpics program work?

    Code:
    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Why wont simple grahpics program work?

    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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    May 2010
    Posts
    1

    Re: Why wont simple grahpics program work?

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured