Results 1 to 6 of 6

Thread: [RESOLVED] ask for help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    4

    Resolved [RESOLVED] ask for help!

    I'm a beginner of java.

    and next week is the mids

    I want to know how to solve this problem

    Write a graphics class RandomTriangle that define a triangle using the GeneralPath class. The x and y coordinates of each of the 3 points of the triangle are generated at random (but should still be within the window frame and should be dependent on the window size in case the window gets resized). The triangle is filled with a random color.
    Hint: to generate a random color, generate 3 numbers at random between 0 and 255 and use them to define the color.
    Note that each time you resize the window, a new triangle is defined

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    4

    Re: ask for help!

    the best way is explain each sentence beside it.

    Thx a lot.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    4

    Re: ask for help!

    Finally, I solved it by myself....

    solution:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class RandomTriangle extends JFrame
    {
    public RandomTriangle()
    {
    super();
    setSize(350,350);
    setVisible(true);
    }
    public void paint(Graphics g)
    {
    super.paint( g );
    int x,y,z;
    Graphics2D g2d = (Graphics2D) g;
    GeneralPath RandomTriangle = new GeneralPath();

    {
    x = (int)(Math.random()*256);
    y = (int)(Math.random()*256);
    z = (int)(Math.random()*256);


    RandomTriangle.moveTo(x,y);
    RandomTriangle.lineTo((int)(Math.random()*350),(int)(Math.random()*350));
    RandomTriangle.lineTo((int)(Math.random()*350),(int)(Math.random()*350));
    RandomTriangle.closePath();

    g2d.setColor( new Color(x,y,z));
    g2d.fill( RandomTriangle );
    }
    }
    public static void main( String args[] )
    {
    RandomTriangle r1 = new RandomTriangle();
    }
    }


    the number 350 can become any number,but 350 is easier to see.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: ask for help!

    Finally, I solved it by myself....
    Good for you. Maybe you should try it yourself before asking for help the next time.

    Anyway, please mark the Thread resolved from the "Thread Tools" menu
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: ask for help!

    Here's how your code can work better
    Code:
    public class RandomTriangle extends javax.swing.JFrame
    {
    	private static final long serialVersionUID = -3533865055201181465L;
    
    	public static void main(final String args[])
    	{
    		new RandomTriangle();
    	}
    
    	private final java.awt.geom.GeneralPath randomTriangle;
    	private final java.awt.Color backColor;
    
    	private final java.awt.Point p1, p2, p3;
    
    	/**
    	 * Construct the frame, Set random points and a random color
    	 */
    	public RandomTriangle()
    	{
    		super("Random Triangle");
    		randomTriangle = new java.awt.geom.GeneralPath();
    		final java.util.Random randomizer = new java.util.Random();
    		backColor = new java.awt.Color(randomizer.nextInt(255), randomizer.nextInt(255), randomizer
    				.nextInt(255));
    		setSize(350, 350);
    		p1 = new java.awt.Point(randomizer.nextInt(getWidth()), randomizer.nextInt(getHeight()));
    		p2 = new java.awt.Point(randomizer.nextInt(getWidth()), randomizer.nextInt(getHeight()));
    		p3 = new java.awt.Point(randomizer.nextInt(getWidth()), randomizer.nextInt(getHeight()));
    		setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    
    	/**
    	 * @see java.awt.Component#paint(java.awt.Graphics)
    	 */
    	@Override
    	public void paint(final java.awt.Graphics g)
    	{
    		super.paint(g);
    		final java.awt.Graphics2D g2d = (java.awt.Graphics2D) g;
    		randomTriangle.moveTo(p1.getX(), p1.getY());
    		randomTriangle.lineTo(p2.getX(), p2.getY());
    		randomTriangle.lineTo(p3.getX(), p3.getY());
    		randomTriangle.closePath();
    
    		g2d.setColor(backColor);
    		g2d.fill(randomTriangle);
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    4

    Re: [RESOLVED] ask for help!

    thanks,yours is much better, right now i just can write it like that.

Posting Permissions

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



Click Here to Expand Forum to Full Width