Results 1 to 5 of 5
  1. #1
    hamster is offline Member
    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

    Default 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,
    Hamster
    Last edited by hamster; 04-26-2012 at 08:33 PM. Reason: additional info

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

    Default 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

  3. #3
    hamster is offline Member
    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

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

  4. #4
    hamster is offline Member
    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

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

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: One test print statement prints multiple times in extended paintComponent method

    You'll have to make the panel visible to whatever code you use to generate the stars, so it knows the sizes, or the width and height accessible some other way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 07-21-2011, 10:46 PM
  2. Replies: 7
    Last Post: 05-05-2011, 04:26 PM
  3. Multiple colors in print-statement
    By schutter07 in forum New To Java
    Replies: 8
    Last Post: 11-20-2010, 11:46 AM
  4. write line multiple times
    By relith in forum New To Java
    Replies: 3
    Last Post: 10-27-2010, 08:38 AM
  5. Playing an AudioInputStream multiple times
    By pmgallardo in forum Advanced Java
    Replies: 6
    Last Post: 03-09-2009, 04:29 PM

Posting Permissions

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