drawTest.java
package week_4_assignment;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class drawTest {
public static void main(String[] args) {
Shapes panel = new Shapes();
JFrame application = new JFrame();
String input = JOptionPane
.showInputDialog(" Enter 1 to draw 6~20 of Lines \n" + " Enter 2 to draw 6~20 of Circles \n"
+ " Enter 1 to draw 6~20 of Rectangles \n" + " Enter -1 to exit the program");
int choice = Integer.parseInt(input);
if (choice == -1) {
System.exit(0);
}
panel.setChoice(choice);
application.dispose();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300, 300);
application.setVisible(true);
while (true) {
String input_2 = JOptionPane.showInputDialog(
"Enter the number which you want to change color to red\n" + " Enter -1 to exit the program");
int choice_2 = Integer.parseInt(input_2);
if (choice_2 == -1) {
System.exit(0);
}
panel.setChoice_2(choice_2);
application.dispose();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300, 300);
application.setVisible(true);
}
}
}
Shapes.java
package week_4_assignment;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.util.Random;
public class Shapes extends JPanel {
private int num=0;
private int choice;
private int choice_2;
private Random randomNumbers = new Random();
private ShapeTest[] myShapes;
public Shapes() {
setBackground(Color.WHITE);
myShapes = new ShapeTest[ 6 + randomNumbers.nextInt(15)];
int key = -1;
for ( int count =0; count< myShapes.length; count++) {
int x1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);
Color color = new Color(0,0,0);
myShapes[count] = new ShapeTest(x1, x2, y1, y2, color);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(choice==1) {
if(num==0) {
for ( ShapeTest shapes: myShapes )
shapes.draw_line( g );
}
else {
myShapes[choice_2].changeColor();
for ( ShapeTest shapes: myShapes )
shapes.draw_line( g );
}
num++;
}
else if(choice==2) {
if(num==0) {
for ( ShapeTest shapes: myShapes )
shapes.draw_circle( g );
}
else {
myShapes[choice_2].changeColor();
for ( ShapeTest shapes: myShapes )
shapes.draw_circle( g );
}
num++;
}
else {
if(num==0) {
for ( ShapeTest shapes: myShapes )
shapes.draw_rectangle( g );
}
else {
myShapes[choice_2].changeColor();
for ( ShapeTest shapes: myShapes )
shapes.draw_rectangle( g );
}
num++;
}
}
public void setChoice(int choice) {
this.choice=choice;
}
public void setChoice_2(int choice_2) {
this.choice_2=choice_2;
}
}
ShapesTest.java
package week_4_assignment;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
import java.awt.Color;
import java.awt.Graphics;
public class ShapeTest {
private int x1;
private int x2;
private int y1;
private int y2;
private Color myColor;
public ShapeTest(int x1, int y1, int x2, int y2, Color color)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
myColor = color;
}
public void draw_line(Graphics g)
{
g.setColor(myColor);
g.drawLine(x1, y1, x2, y2);
}
public void draw_circle(Graphics g)
{
g.setColor(myColor);
g.drawOval(x1, y1, x2, y2);
}
public void draw_rectangle(Graphics g)
{
g.setColor(myColor);
g.drawRect(x1, y1, x2, y2);
}
public void changeColor() {
Color color = new Color(219, 68, 85);
this.myColor = color;
}
}