0
가 내가 버튼을 클릭하면 그 다음 하나의 검은 원으로 시작해야
가, 흰색 원이 나타나는 사진으로 출력 같은 결과를이 코드를 할 노력하고있어 시프트 우측으로하면 창을 지우고 처음부터 시작하고 난 단지 하나의 원 컬러 또는 두 개의 원
여기에 코드
이다가 계속해야 4 원 후 등 20 픽셀 더 크게import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.WindowConstants;
public class CircleList extends JFrame implements ActionListener {
JButton addCircle;
int nCircle = 1;
int step = 0;
int shift = 0;
int d = 0;
public static void main (String [] args){
new CircleList();
}
private class Circle {
public int x1;
public int y1;
public int w;
public int h;
public Color color;
}
private List<Circle> whiteList = new ArrayList<>();
private List<Circle> blackList = new ArrayList<>();
public CircleList() {
super("CircleList");
setSize(500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.gray);
addCircle = new JButton("Add Circle");
add(addCircle,BorderLayout.SOUTH);
addCircle.addActionListener(this);
repaint();
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.fillOval(35, 35, 35, 35);
if (d==1) {
for (Circle s: whiteList) {
paintWhiteCircle(g, s);
}
addWhiteCircle();
}
else if (d==2) {
for (Circle s: blackList) {
paintBlackCircle(g, s);
}
addBlackCircle();
}
}
public void paintWhiteCircle(Graphics g, Circle circle) {
g.setColor(circle.color);
g.fillOval(circle.x1, circle.y1, circle.w, circle.h);
}
public void paintBlackCircle(Graphics g, Circle circle) {
g.setColor(circle.color);
g.fillOval(circle.x1, circle.y1, circle.w, circle.h);
}
public void addBlackCircle(){
Circle circle = new Circle();
circle.x1 = 40 + shift;
circle.y1 = 30 ;
circle.w = 30 + step;
circle.h = 30 + step;
circle.color = new Color(0,0,0);
this.blackList.add(circle);
}
public void addWhiteCircle() {
Circle circle = new Circle();
circle.x1 = 100 + shift;
circle.y1 = 30 ;
circle.w = 50 + step;
circle.h = 50 + step;
circle.color = new Color(255*65536+255*256+255);
this.whiteList.add(circle);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
d = 0;
if (source == addCircle) {
for (nCircle = 2; nCircle <= 5; nCircle++) {
if (nCircle == 2) {
d = 1;
step+=20;
shift += 20;
nCircle++;
}
else if (nCircle == 3) {
d = 2;
step+=20;
shift += 20;
}
else if (nCircle == 4) {
d=1;
step+=20;
shift += 20;
}
else if (nCircle == 5)
repaint(); // it should clear the window and start with one circle like the beginning
}
repaint();
}
}
}
출력은 내가 당신의 예를 수정 한이
[여기] (https://stackoverflow.com/a/37063037/230513), "스윙 프로그램은 오버라이드 (override)'의 paintComponent()'대신 오버라이드 (override)의 '페인트()'."- [* AWT 및 스윙의 페인팅 : The Paint Methods *] (http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod