Results 1 to 5 of 5
  1. #1
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default Double drawing problem with 2D

    Hi, my code works fine all I wanted to ask is my codes prints double of same triangles like it should draw 10 random triangles and it draws 10 random triangles then again it wipes of the screen and draw again 10 random triangles so all I want is 10 not 20 so any inputs will be really helpful thanks.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class Triangles2 extends JFrame
    	{
    
    
    		public Triangles2()
    		{
    			super( "Random Triangles" );
    			getContentPane().setBackground( Color.black );
    			setSize( 400, 600 );
    			setVisible( true );
    		}
    
    
    
    		public void paint( Graphics g )
    		{
    			super.paint( g );
    
    
    			int xP, yP, zP;
    
    
    
    			Graphics2D g2d = ( Graphics2D ) g;
    
    
    			for( int count = 1; count <= 10; count++ )
    			{
    		
    
    			GeneralPath triangle = new GeneralPath();
    
    			xP = ( int ) ( Math.random() * 256 );
    			yP = ( int ) ( Math.random() * 256 );
    			zP = ( int ) ( Math.random() * 256 );
    
    			try
    			{
    				Thread.sleep(700);
    			}
    				catch (Exception e) {}
    
    
    			triangle.moveTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 400 ) );
    
    			triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 400 ) );
    
    			triangle.lineTo( ( int ) ( Math.random() * 400 ), ( int ) ( Math.random() * 400 ) );
    
    			triangle.closePath();
    
    
    			g2d.setColor( new Color( xP, yP, zP ) );
    
    
    			g2d.fill( triangle );
    
    			}
    
    		}
    
    
    
    		public static void main( String args[] )
    		{
    
    			Triangles2 myTriangles = new Triangles2();
    			myTriangles.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		}
    	}//class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by leapinlizard View Post
    Hi, my code works fine
    I'm afraid that I'm going to have to disagree here as you are violating several rules of Swing graphics, and the reason for your error is due to these problems:

    1) You should not draw directly in the JFrame but instead should draw in a JPanel or JComponent and then add this to the JFrame.

    2) You should not paint in a paint method override but rather in a JPanel or JComponent's paintComponent override.

    3) Never, I repeat, NEVER, have a Thread.sleep(...) in a drawing routine. All this does is freezes the app.

    To learn how to draw properly, please see the Sun graphics tutorials.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If this were my app, I'd do the following:

    1) Create a Triangle class that accepts an array of x points and an int array of y points as well as a Color and that has a paint(Graphics g) method that allows it to paint itself.

    2) Create a class that extends JPanel and overrides JPanel's paintComponent. The first method call inside the paintComponent override will be super.paintComponent(g).

    3) Give this class a triangle list: ArrayList<Triangle>

    4) In the paintComponent override, iterate through the list above calling paint on each of the triangles.

    5) If I wanted to stagger the creation of the triangles, I'd use a Swing Timer that would add a new Triangle to the list and would increment an int counter (triangleCount), create a new triangle with random points and color, add it to the list and call repaint on the JPanel. If triangleCount >= MAX_TRIANGLE_COUNT, then the timer would stop.
    Last edited by Fubarable; 02-11-2010 at 09:18 PM.

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Hi. I have a question (a dumb one). What is a JComponent? I looked through this page ( A Visual Guide to Swing Components (Java Look and Feel) (The Java™ Tutorials > Graphical User Interfaces > Swing Features) ) and couldn't find it.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Hi. I have a question (a dumb one). What is a JComponent? I looked through this page ( A Visual Guide to Swing Components (Java Look and Feel) (The Java™ Tutorials > Graphical User Interfaces > Swing Features) ) and couldn't find it.
    Pretty much everything on that page is a JComponent as it's the class that underlies most Swing components. It's somewhat like a primative JPanel.

Similar Threads

  1. variable double problem
    By DarkoDrljaca in forum New To Java
    Replies: 5
    Last Post: 03-02-2009, 09:00 AM
  2. Problem with 3D drawing.
    By Supamagier in forum Advanced Java
    Replies: 0
    Last Post: 08-31-2008, 01:39 PM
  3. [SOLVED] Problem in Drawing(Repaint)
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 07-16-2008, 08:28 AM
  4. Double Value problem
    By sakthivel123 in forum New To Java
    Replies: 2
    Last Post: 07-10-2008, 05:18 PM
  5. Double Buffering problem
    By aprenz in forum Java Applets
    Replies: 0
    Last Post: 05-28-2008, 05:26 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
  •