我创建了一个使用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;
}
}
答案 0 :(得分:3)
您接下来的问题是,您只需为此处的所有组件设置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);
}
}
在repaint()
方法中调用paintComponent()
也是错误的,删除该行并在添加新形状后重新绘制面板,例如:
private class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent event) {
add(event.getPoint());
repaint();
}
}
答案 1 :(得分:2)
如果目标是使用随机颜色绘制每个形状,则必须为绘制的每个形状更新graphics
颜色。即:
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()
方法中的每个形状分配一个随机颜色并保留它,那么你必须保持一个颜色图或一些服务于目标的其他结构。
其他次要但重要的注意事项:
不要忘记在super.paintComponent()
的实施中致电paintComponent
。
请勿在{{1}}中致电repaint()
,因为它最终会触发paintComponent()
。所以你要创造不必要的重绘。
在paintComponent
内调用repaint()
以使用新创建的形状重新绘制组件。
有关详细信息和示例,请参阅Performing Custom Painting。