gpt4 book ai didi

java - 用随机颜色绘制形状

转载 作者:行者123 更新时间:2023-12-01 17:18:24 26 4
gpt4 key购买 nike

我创建了一个程序,使用 MousePressed Listiner 绘制 4 个形状。该程序应该用随机颜色绘制形状。我的程序正确绘制形状,但没有用随机颜色绘制它们。你能给我一些如何编程的想法吗?

谢谢

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

public class Pole extends JFrame {

public static int i;
public static void main(String[] args) {


JFrame frame= new JFrame("Shape Stamper!");

JPanel container;
JButton circle = new JButton("Circle");
JButton square = new JButton("Square");
JButton rectangle = new JButton("Rectangle");
JButton oval = new JButton("Oval");


container = new JPanel(new GridLayout(1, 4));
container.add(circle);
container.add(square);
container.add(rectangle);
container.add(oval);


circle.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
i = 1;
}
});

square.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
i = 2;
}
});

rectangle.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
i = 3;
}
});

oval.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
i = 4;
}
});


MyComponent shape = new MyComponent();
frame.setSize(500, 500);

frame.add(shape, BorderLayout.CENTER);
frame.add(container, BorderLayout.SOUTH);

frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}




import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JComponent;

/**
*
* @author Taras
*/
public class MyComponent extends JComponent {
int i;
Color randColor;
public MyComponent()
{
this.i = i;
addMouseListener(new MouseHandler());
}

private ArrayList<Rectangle2D> arrOfRect=new ArrayList<>();
private ArrayList<Ellipse2D> arrOfEllipse=new ArrayList<>();
// private ArrayList<Color> randColor = new ArrayList<>();
public void paintComponent(Graphics g)
{

Graphics2D g2=(Graphics2D) g;

g.setColor(new Color(randRed(), randGreen(), randBlue()));

for(Rectangle2D r: arrOfRect){

//g2.setColor(Color.GREEN);

g2.draw(r);

}
for(Ellipse2D e: arrOfEllipse){

g2.draw(e);}
repaint();


}
public void add(Point2D p)
{
double x=p.getX();
double y=p.getY();
if (Pole.i == 1){
Ellipse2D ellipse = new Ellipse2D.Double(x, y, 100,100);
//randColor = new Color(randRed(), randGreen(), randBlue());

arrOfEllipse.add(ellipse);
}
if (Pole.i == 2){
Rectangle2D rectangls=new Rectangle2D.Double(x, y, 100, 100);
arrOfRect.add(rectangls);

}
if (Pole.i == 3){
Rectangle2D rectangls=new Rectangle2D.Double(x, y, 150, 100);
arrOfRect.add(rectangls);

}
if (Pole.i == 4){
Ellipse2D ellipse = new Ellipse2D.Double(x, y, 100,50);
arrOfEllipse.add(ellipse);
}
}
private class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent event)
{
add(event.getPoint());
//Color rColor = new Color(randRed(), randGreen(), randBlue());
//randColor.add(rColor);
randColor = new Color(randRed(), randGreen(), randBlue());

}

}

private int randRed() {
int red;
Random randomNumber = new Random();
red = randomNumber.nextInt(255);
return red;
}

private int randGreen() {
int green;
Random randomNumber = new Random();
green = randomNumber.nextInt(255);
return green;
}

private int randBlue() {
int blue;
Random randomNumber = new Random();
blue = randomNumber.nextInt(255);
return blue;
}
}

最佳答案

接下来您的问题是为此处的所有组件仅设置一次Color:

g.setColor(new Color(randRed(), randGreen(), randBlue()));

但是如果您想更改每个形状的颜色,则需要每次调用 setColor()

您可以使用 Random 的一个实例更改您的 paintComponent() 方法,如下所示,您的形状将具有不同的颜色:

Random rand = new Random();
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;

for (Rectangle2D r : arrOfRect) {
g.setColor(new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
g2.draw(r);

}
for (Ellipse2D e : arrOfEllipse) {
g.setColor(new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()));
g2.draw(e);
}

}

paintComponent() 方法中调用 repaint() 也是错误的,请删除该行并在添加新形状后重新绘制面板,例如:

private class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent event) {
add(event.getPoint());
repaint();
}
}

关于java - 用随机颜色绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459029/

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