Jump to content

Modern Art Generator (Java)

Go to solution Solved by namarino,

Never mind I figured it out.....sorry about that.....

Hey guys. So I have an assignment that where I essentially have to generate random modern art. Theprogram generates either an oval or a rectangle and picks a color randomly from ones that I have chosen. But there's one part of the assignment that I don't quite understand. It says "the coordinates should be chosen based on the panel's width and height. Length of sides should be limited to half the width or height of the window." This is what I have so far. I'm just curious if I have interpreted those directions properly and if the program is behaving the way it should be. I'm having a hard time understanding the way shapes work in java. I really appreciate your help!

import javax.swing.JFrame;public class Application {	public static void main(String[]args){		Window window = new Window();	}}import javax.swing.JFrame;public class Window extends JFrame{	public Window(){		setSize(700,700);		setVisible(true);		add(new Image());		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	}}import java.awt.Color;import javax.swing.JPanel;import java.awt.Graphics;import java.util.Random;public class Image extends JPanel {	public void paintComponent(Graphics g){		super.paintComponent(g);		Random rand = new Random();		int width = getWidth();		int height = getHeight();				int counter = 0;				for(counter = 0; counter < 10; counter++){			int shape = rand.nextInt(2);			int color = rand.nextInt(6);			int size1 = rand.nextInt(width);			int size2 = rand.nextInt(height);			int size3 = rand.nextInt(getWidth());			int size4 = rand.nextInt(getHeight());						switch(color){			case 0:				g.setColor(Color.blue);				break;			case 1:				g.setColor(Color.red);				break;			case 2:				g.setColor(Color.yellow);				break;			case 3:				g.setColor(Color.green);				break;			case 4:				g.setColor(Color.black);				break;			case 5:				g.setColor(Color.pink);				break;					}						switch(shape){			case 0:				g.fillRect( (int)(.25*size1), (int)(.25*size2), (int)(.5*size3), (int)(.5*size4));				break;			case 1:				g.fillOval( (int)(.25*size1), (int)(.25*size2), (int)(.5*size3), (int)(.5*size4));				break;			}					}	}}
Link to comment
https://linustechtips.com/topic/300075-modern-art-generator-java/
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

×