Results 1 to 5 of 5
  1. #1
    venandane is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default What is wrong with this simple code?

    Hi all,

    This simple code was taken from "Java how to program 9th ed". I don't understand why the output is printed twice. Someone please shed some light....

    Alvin


    // Demonstrates a problem
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;

    public class Problem extends JPanel
    {
    public void paintComponent( Graphics g )
    {
    super.paintComponent( g );

    Color c = new Color(241, 40, 50);

    System.out.println("This line is output twice! What am I doing wrong? (example taken from Java how to program 9th ed)");

    // draw ring
    g.setColor( c );
    g.fillOval( 10, 10, 200, 200 );

    } // end method paintComponent
    } // end class Problem


    The code below is the main method to call the above Class "Problem"

    // Test class problem
    import javax.swing.JFrame;

    public class ProblemTest
    {
    public static void main( String[] args )
    {
    Problem panel = new Problem();
    JFrame application = new JFrame();

    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    application.add( panel );
    application.setSize( 250, 250 );
    application.setVisible( true );
    } // end main
    } // end class ProblemTest

  2. #2
    kkji is offline Member
    Join Date
    Aug 2011
    Posts
    54
    Rep Power
    0

    Default Re: What is wrong with this simple code?

    are you able to compile the file?

  3. #3
    venandane is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: What is wrong with this simple code?

    Its compiles and run fine. Just that its running the for loop twice. I guess the paintcomponent method is being called from somewhere...

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: What is wrong with this simple code?

    Quote Originally Posted by venandane View Post
    Its compiles and run fine. Just that its running the for loop twice. I guess the paintcomponent method is being called from somewhere...
    What makes you think this is incorrect behavior?

  5. #5
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: What is wrong with this simple code?

    you are calling paintComponent.....

    i was having the same problem few days back.....you dont have a control over paint methods......they will be called whenever there is a need to paint the screen again....

    try to change the dimension of the appletviewer screen....you will get to know that you really dont have a control over calling of paint method......paint will simply be called when java thinks there is a need to reapaint the screen with the new content.....

Similar Threads

  1. Simple number guessing game but something's wrong???
    By JohnPringle83 in forum New To Java
    Replies: 11
    Last Post: 05-29-2011, 08:43 PM
  2. What's wrong with my code?
    By Isong in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2010, 06:00 PM
  3. What's wrong with my (simple) code
    By kracer in forum New To Java
    Replies: 13
    Last Post: 05-01-2010, 03:55 PM
  4. Replies: 4
    Last Post: 07-23-2009, 09:45 PM
  5. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM

Posting Permissions

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