I'm very new to Java and programming in general and I'm having a bit of trouble with my homework assignment. We're supposed to give a variation of 4 different shapes, 10 shape in each picture with variation of at least 20, also variation of at least 20 different colors and variation of position.
I've made classes for each of my 5 shapes. But I somehow don't know how to add them to my ArrayList randomShape so that they can be called in the paintComponent section. Also I'm having trouble coming up with the code the positions :(
Its a bit frustrating that I've hit a road block of thoughts.
Thank you for any help/advice you give me It's very much appreciated
Here is my code that I have so far
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Cara extends JPanel implements ActionListener {
Random random = new Random();
public static final int MAX_AMOUNT_OF_SHAPES = 50,AMOUNT_OF_DISTINCT_SHAPES=5, AMOUNT_OF_DISTINCT_COLORS = 20, SIZE_MAX_X_COORDINATE = 400, SIZE_MAX_Y_COORDINATE = 300;;
public static ArrayList<RandomShape> randomShape = new ArrayList<RandomShape>();
int x = 0;
int xMax = 400;
int y = 0;
int yMax = 300;
int width = 0;
int height = 0;
int arcWidth = 0;
int arcHeight = 0;
int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B = (int)(Math.random()*256);
Color randomColor = new Color(R,G,B);
public Cara(){
setPreferredSize( new Dimension(400,300));
}
abstract class RandomShape {
protected Color color;
protected int x,y;
abstract void draw(Graphics g);
}
public class Circle extends RandomShape{
@Override
public void draw(Graphics g) {
g.drawOval(x,y,width,width);
g.fillOval(x,y,width,width);
g.setColor(randomColor);
}
//constructor for random position
Circle (){
for ( int x; x < xMax; x++){
}
for ( int y; y < yMax; y++){
}
}
}
public class Rectangle extends RandomShape{
@Override
public void draw( Graphics g){
g.drawRect(x,y,width,height);
g.fillRect(x,y,width,height);
g.setColor(randomColor);
}
//constructor for random position
}
public class RoundRectangle extends RandomShape{
@Override
public void draw(Graphics g){
g.drawRoundRect(x,y,width,height,arcWidth,arcHeight);
g.fillRoundRect(x,y,width,height,arcWidth,arcHeight);
g.setColor(randomColor);
}
//constructor for random position
}
public class Oval extends RandomShape{
@Override
public void draw(Graphics g){
g.drawOval(x,y,width,height);
g.fillOval(x,y,width,height);
g.setColor(randomColor);
}
//constructor for position
}
public class Square extends RandomShape{
Square square = new Square();
@Override
public void draw(Graphics g){
g.drawRect(x,y,width,width); //because is a rectangle with sides of equal size
g.fillRect(x,y,width,width);
g.setColor(randomColor);
}
//constructor for position
}
protected void paintComponent(Graphics g) {
//clear the background
super.paintComponent(g);
//draw all shapes
for ( RandomShape rs: randomShape){
rs.draw(g);
}
}
public void actionPerformed (ActionEvent e){
regenerate();
repaint();
}
private void regenerate() {
//clear the shapes list
randomShape.clear();
// create random shapes
RandomShape shape = null;
for (int i = 0; i < 10 + random.nextInt(MAX_AMOUNT_OF_SHAPES); i++){
int randomInt = random.nextInt(AMOUNT_OF_DISTINCT_SHAPES);
switch (randomInt) {
case 0: shape = new Oval(400,300);
break;
case 1: shape = new Circle(400,300);
break;
case 2: shape = new Rectangle(400,300);
break;
case 3: shape = new Square(400,300);
break;
case 4: shape = new RoundRectangle(400,300);
break;
}
}
//random position
RandomShape position = null;
for (int i = 0; i < (MAX_SIZE_X_COORDINATE*MAX_SIZE_Y_COORDINATE) ; i++){
int randomIntpos = random.nextInt();
}
}
public static void main(String[] args) {
final Cara cara = new Cara();
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
final JFrame frame = new JFrame("Computer Assisted Random Artist");
JButton button = new JButton("regenerate");
button.addActionListener(cara);
frame.add(button, BorderLayout.SOUTH);
frame.pack();
cara.regenerate();
frame.setVisible(true);
}
});
}
}
public class Cara extends JPanel implements ActionListener {
private static ArrayList<RandomShape> randomShape;
public Cara() {
randomShape = new ArrayList<RandomShape>();
}
}
private void regenerate() {
//clear the shapes list
randomShape.clear();
// create random shapes
RandomShape shape = null;
int randomInt;
for(int i = 0; i < 10 + random.nextInt(MAX_AMOUNT_OF_SHAPES); i++) {
randomInt = random.nextInt(AMOUNT_OF_DISTINCT_SHAPES);
switch (randomInt) {
case 0:
shape = new Oval(400,300);
break;
case 1:
shape = new Circle(400,300);
break;
case 2:
shape = new Rectangle(400,300);
break;
case 3:
shape = new Square(400,300);
break;
case 4:
shape = new RoundRectangle(400,300);
break;
}
randomShape.add(shape);
}
//random position
RandomShape position = null;
int randomIntpos;
for (int i = 0; i < (MAX_SIZE_X_COORDINATE*MAX_SIZE_Y_COORDINATE) ; i++){
randomIntpos = random.nextInt();
}
}
public class RoundRectangle extends RandomShape {
private int x;
private int y;
//constructor for random position
public RoundRectangle(int x, int y) {
this.x = x
this.y = y
}
@Override
public void draw(Graphics g){
g.drawRoundRect(x,y,width,height,arcWidth,arcHeight);
g.fillRoundRect(x,y,width,height,arcWidth,arcHeight);
g.setColor(randomColor);
}
}