我创建了一个程序,用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();
}
}
如果目标是用随机颜色绘制每个形状,则必须为绘制的每个形状更新graphics
颜色。Ie:
for (Rectangle2D r : arrOfRect) {
g.setColor(getRandomColor());
g2.draw(r);
}
不需要在每次呼叫中分配新的Random
,您可以使其成为私人成员。例如:
private Random randomNumber = new Random();
private Color getRandomColor() {
return new Color(randomNumber.nextFloat(),
randomNumber.nextFloat(), randomNumber.nextFloat());
}
如果你想在add()
方法中为每个形状指定一个随机颜色并保持它,那么你必须维护一个颜色映射,或者其他一些为目标服务的结构。
其他次要但重要的注意事项:
不要忘记在
paintComponent
的实现中调用super.paintComponent()
。不要在
paintComponent()
中调用repaint()
,因为它最终会触发paintComponent
。因此,您正在创建不必要的重新绘制。请在
add()
内部调用repaint()
,以使用新创建的形状重新绘制零部件。有关更多详细信息和示例,请参见执行自定义绘制。