Results 1 to 5 of 5
- 10-29-2011, 12:03 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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
- 11-03-2011, 09:53 PM #2
Member
- Join Date
- Aug 2011
- Posts
- 54
- Rep Power
- 0
Re: What is wrong with this simple code?
are you able to compile the file?
- 11-03-2011, 10:48 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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...
- 11-03-2011, 10:58 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
- 11-06-2011, 09:55 AM #5
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
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
-
Simple number guessing game but something's wrong???
By JohnPringle83 in forum New To JavaReplies: 11Last Post: 05-29-2011, 08:43 PM -
What's wrong with my code?
By Isong in forum AWT / SwingReplies: 1Last Post: 11-16-2010, 06:00 PM -
What's wrong with my (simple) code
By kracer in forum New To JavaReplies: 13Last Post: 05-01-2010, 03:55 PM -
Simple search-function: what am I doing wrong?
By Bas in forum New To JavaReplies: 4Last Post: 07-23-2009, 09:45 PM -
Simple Addition Program Outputting Wrong Value
By carlodelmundo in forum New To JavaReplies: 4Last Post: 08-05-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks