Java_Week_5_실습
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);
}
// while(key == -1) {
// String input_2 = JOptionPane.showInputDialog(
// "Enter the number which you want to change color to red\n" +
// "Enter 0 to stop\n" +
// "Enter -1 to exit the program");
// }
// String input_2 = JOptionPane.showInputDialog(
// "Enter the number which you want to change color to red\n" +
// "Enter 0 to stop\n" +
// "Enter -1 to exit the program");
// int choice_2 = Integer.parseInt(input_2);
// if (choice_2 == -1) {
// System.exit(0);
// }
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// super 클래스에 있는 paintComponent 구현내용을 실행해라, 그리고 다음 내용을 실행해라
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;
}
}
Author And Source
이 문제에 관하여(Java_Week_5_실습), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@shintaewon/JavaWeek5실습저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)