gpt4 book ai didi

java - 图4 圆圈一键不同尺寸和颜色

转载 作者:行者123 更新时间:2023-12-02 12:00:49 26 4
gpt4 key购买 nike

我正在尝试执行此代码以输出与图片相同的结果

它应该从一个黑色圆圈开始,然后当我单击按钮时,会出现一个白色圆圈,向右移动并变大 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();
}
}
}

输出应该是这样的

output

最佳答案

我已经纠正了你的例子。现在可以了。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class CircleList extends JPanel implements ActionListener, Runnable {

private static final int INTERVAL = 20;

private static final int INITIAL_DIAMETR = 20;

private int actualXPos;

private final List<Circle> circleList = new ArrayList<>();

public static void main(String[] args) {
SwingUtilities.invokeLater(new CircleList());
}

@Override
public void run() {
JFrame frm = new JFrame(getClass().getSimpleName());
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.getContentPane().add(this);
frm.pack();
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}

public CircleList() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(500, 500));
setBackground(Color.gray);
JButton addCircle = new JButton("Add Circle");
add(addCircle, BorderLayout.SOUTH);
addCircle.addActionListener(this);
addCircle();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Circle c : circleList) {
c.paint(g);
}
}

private void addCircle() {

Circle circle = new Circle();

int num = circleList.size();
circle.x1 = 40 + actualXPos;
circle.y1 = 30;
circle.w = 30 + INITIAL_DIAMETR * (num + 1);
circle.h = 30 + INITIAL_DIAMETR * (num + 1);
circle.color = num % 2 == 0 ? Color.BLACK : Color.WHITE;
circleList.add(circle);
actualXPos += circle.w + INTERVAL;
}

@Override
public void actionPerformed(ActionEvent e) {
if (circleList.size() == 4) {
circleList.clear();
actualXPos = 0;
}
addCircle();
repaint();
}

private static class Circle {
private int x1;

private int y1;

private int w;

private int h;

private Color color;

public void paint(Graphics g) {
g.setColor(color);
g.fillOval(x1, y1, w, h);
}
}
}

关于java - 图4 圆圈一键不同尺寸和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47264169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com