Results 1 to 5 of 5
- 04-26-2012, 07:58 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 14
- Rep Power
- 0
One test print statement prints multiple times in extended paintComponent method
Hi there Everyone! This is my 1st post to Java Forums. Impressive site too btw. :)
My background is generally Oracle and database development. However lately i decided to dig into a book. So far so good up to the point i can understand the main points in basic graphics, inheritance and so on. Soooo now i decided to do some old simple examples to test my knowledge so far.
Drawing a basic starfield in an application window. This shows a random number of white pixels on a black 800x600 background.
Java Code:import javax.swing.*; import java.awt.*; public class Starfield extends JFrame{ public Starfield(){ StarPanel starpanel = new StarPanel(); starpanel.setBackground(Color.BLACK); add(starpanel); } class StarPanel extends JPanel{ protected void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.WHITE); int times = 1+(int)(Math.random() * 500); System.out.println("Number of Stars: "+ times); for (int i = 0; i < times; i++){ int x = (int)(Math.random() * getWidth()); int y = (int)(Math.random() * getHeight()); g.drawLine(x, y, x, y); // pixel } } } public static void main(String[] args){ Starfield frame = new Starfield(); frame.setSize(800, 600); frame.setTitle("Starfield v1.01"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Output: nice picture of random number of stars and a typical run console shows:
Number of Stars: 490
Number of Stars: 221
Number of Stars: 187
Number of Stars: 316
Number of Stars: 7
This works but it displays my debugging println messages 4 times. I read somewhere that java calls protected void paintComponent(Graphics g) multiple times but reading it - i would only expect 1 print statement. But the stars print out correctly once each time in the for loop. I would love to understand why this is printing my statement multiple times and how i might be able to show it only once. I guess i could pass it back as variable and print it outside the method. But i never knew that paint would run 4 times. The correct value is always the last println statement printed each time i run it.
Thanks for reading this if you had time.
Apologies again - title of this question should read "protected" instead of extended.
Regards,
HamsterLast edited by hamster; 04-26-2012 at 08:33 PM. Reason: additional info
- 04-26-2012, 08:38 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: One test print statement prints multiple times in extended paintComponent method
The paintComponent method is called whenever it is determined that the component needs to be redrawn. This could be when it becomes visible, is resized, or any number of other types of changes that may occur to the component. For instance, try resizing the JFrame and see what happens. If you wish to draw a constant pattern, then perform your logic that sets up the drawing (eg all those randomly generated values) outside of the paintComponent method
- 04-27-2012, 01:21 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 14
- Rep Power
- 0
Re: One test print statement prints multiple times in extended paintComponent method
Thanks - I'll see if there is some way i can generate the random values outside and pass each star in as parameter (or array).
- 04-27-2012, 02:13 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 14
- Rep Power
- 0
Re: One test print statement prints multiple times in extended paintComponent method
After some analysis - i think i might to have to use a 2D arraylist (or two 1D) to pass the numbers to the draw component, since the number of stars is random and a 2D arraylist to store each x and y. Additionally the coordinate range was determined by the current window size, using getHeight() and getWidth(). Unfortunately that is belonged to that draw component - defining these outside might be tricky.
- 04-27-2012, 09:43 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Similar Threads
-
Run the constructor method of the class that I extended possible?
By Addez in forum New To JavaReplies: 1Last Post: 07-21-2011, 10:46 PM -
New to Applets..How do you draw the figure in the paint method multiple times?
By etranman1 in forum Java AppletsReplies: 7Last Post: 05-05-2011, 04:26 PM -
Multiple colors in print-statement
By schutter07 in forum New To JavaReplies: 8Last Post: 11-20-2010, 11:46 AM -
write line multiple times
By relith in forum New To JavaReplies: 3Last Post: 10-27-2010, 08:38 AM -
Playing an AudioInputStream multiple times
By pmgallardo in forum Advanced JavaReplies: 6Last Post: 03-09-2009, 04:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks