Jump to content

Hey guys, I have to do this project for my class. The goal is to draw 10 random shapes in random colors. Essentially what I have to do is make an abstract class Shape and then a bunch of classes that extend Shape, each of which specify the type of shape. So for example, I have fillOval and fillRectangle. What I'm having trouble with though is once I create the shape, I have to add it to an ArrayList. I know how to do that, but how do I get it out of there and draw it onto the screen? I can't quite figure out how to do that. I'm going to include my code, but only the sections that are really necessary.

import java.awt.Color;import java.awt.Graphics;import java.util.ArrayList;import java.util.List;import java.util.Random;import javax.swing.JPanel;public class Picture extends JPanel {	private static final long serialVersionUID = 1L;	private Random random;	private List<Shape> shapes;		public void PaintComponent(Graphics g){		//draws the shapes from list shapes		super.paintComponent(g);		shapes = new ArrayList<Shape>();		Shape rectangle;		rectangle = new FilledRectangle(10, 20, 10, 20, Color.BLUE);		shapes.add(rectangle);	}		public void generateShapes(int width, int height){					}}import java.awt.Color;import java.awt.Graphics;import java.util.ArrayList;import java.util.List;import java.util.Random;import javax.swing.JPanel;public class Picture extends JPanel {	private static final long serialVersionUID = 1L;	private Random random;	private List<Shape> shapes;		public void PaintComponent(Graphics g){		//draws the shapes from list shapes		super.paintComponent(g);		shapes = new ArrayList<Shape>();		Shape rectangle;		rectangle = new FilledRectangle(10, 20, 10, 20, Color.BLUE);		shapes.add(rectangle);	}		public void generateShapes(int width, int height){					}}import java.awt.Color;import java.awt.Graphics;public class FilledRectangle extends Shape{	FilledRectangle(int x, int y, int width, int height, Color color) {		super(x, y, width, height, color);	}		public void draw(Graphics g){		g.setColor(super.pickColor());		g.fillRect(x, y, width, height);	}}

My professor gave us a UML of what the program is supposed to look like so the basic structure that I have here is correct. Thanks so much for your help!!!

Link to comment
https://linustechtips.com/topic/319475-drawing-random-shapes/
Share on other sites

Link to post
Share on other sites

You are generating new shapes in the paint method, you should move that to generateShapes. Then in paintComponent, loop through the shapes list and call the draw method on each shape.

Oh yeah, that was just a test, Sorry about that. Let me try that and then I'll repost.

Link to comment
https://linustechtips.com/topic/319475-drawing-random-shapes/#findComment-4342450
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×