Results 1 to 2 of 2
  1. #1
    Hawk is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Unhappy Generate 100 random rectangles and ovals

    Hello, Im a newbie :))

    I'm working with a fairly simple task which I will create a program that draws a number of rectangles and ovals. The size and position of the rectangles and ovals are to be generated randomly.

    But I have a problem, my code just draws ONE rectangle or oval, and I think it has something to do with the overlapping. So I need help finding my mistake.

    RandomShape

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;
    
    public class RandomShape extends JFrame {
    	private int n; //Instansvariabel
    	public RandomShape() {}
    	public RandomShape(int antal) {
    		n = antal;
    		for(int i=1; i<n; i++)
    			add(generateRandomShape());
    		setBackground(Color.white);
    		setVisible(true);
    		setSize(500,500);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    
    	}
    
    	public Component generateRandomShape() {
    		double r1 = (int)(2*Math.random());
    		if		(r1 == 0)
    			return new RandomRect();
    		else if (r1 == 1)
    			return new RandomOval();
    		else
    			return null;
    	}
    
    
    
    	public static void main (String[] arg) {
    		RandomShape r = new RandomShape(100);
    	}
    }
    RandomRect

    Java Code:
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    
    public class RandomRect extends JPanel {
    	private int width, height;
    	public RandomRect() {
    		width  = randomPixel();
    		height = randomPixel();
    		setPreferredSize(new Dimension(width, height));
    		setOpaque(false);
    
    	}
    
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.red);
    		g.fillRect(randomPixel(),randomPixel(), width, height);
    	}
    
    	private int randomPixel() {
    		int pix = (int)(Math.random()*400);
    		return pix;
    	}
    }

    RandomOval

    Java Code:
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    
    public class RandomOval extends JPanel {
    	private int width, height;
    	public RandomOval() {
    		width  = randomPixel();
    		height = randomPixel();
    		setPreferredSize(new Dimension(width, height));
    		setOpaque(false);
    
    	}
    
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.green);
    		g.fillOval(randomPixel(),randomPixel(), width, height);
    	}
    
    	private int randomPixel() {
    		int pix = (int)(Math.random()*400);
    		return pix;
    	}
    }
    I appreciate all kind of help :)

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

    Default Re: Generate 100 random rectangles and ovals

    There are a couple of conceptual errors here. For one you must realize that a JFrame's contentPane uses BorderLayout by default, and so any comopnent added to the JFrame will cover over any that were added before.

    But I think more importantly that your RandomRect and RandomOval classes should not extend JPanel but instead should create possibly a Rectangle2D and an Ellipse2D, both of which extend Shape, and your main GUI should hold a JPanel whose paintComponent has been overridden and an ArrayList of Shape or ArrayList<Shape>. Then create your random shapes and place them in the arraylist and in the one JPanel's paintComponent method iterate through the ArrayList drawing each shape.

Similar Threads

  1. [Q] Generate Random Letter
    By iriscience in forum New To Java
    Replies: 11
    Last Post: 01-31-2011, 12:10 AM
  2. Random integer generate
    By trbLeeciN in forum New To Java
    Replies: 6
    Last Post: 06-22-2010, 01:19 AM
  3. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  4. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 08:37 AM
  5. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 AM

Tags for this Thread

Posting Permissions

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