在java中填充颜色的形状

时间:2015-09-28 14:09:50

标签: java swing colors shapes

我希望能够制作一个有三种形状的程序:圆形,正方形和三角形。它需要具有颜色名称的按钮。如果我单击红色按钮,然后单击圆圈,则圆圈将变为红色。

我的代码基于我发现的每次点击随机化形状颜色的东西。我设法在按钮上做了一个布局但是它们不会出现在形状上。三角形也不存在。按钮的动作者被注释掉了,因为我无法让它们起作用,我也不想删除它们。

我对形状很新,我无法理解。提前谢谢!

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.List;
import java.util.*;
import javax.swing.*;

public class DemoShapes {
       public static final Color DEFAULT_COLOR = Color.BLACK;

JFrame frame = new JFrame("Shapes");
Container contentPane;
JButton Red = new JButton("Red");
JButton Orange = new JButton("Orange");
JButton Yellow = new JButton ("Yellow");
JButton Green = new JButton ("Green");
JButton Blue = new JButton ("Blue");
JButton Pink = new JButton ("Pink");
JButton White = new JButton ("White");
JButton Gray = new JButton ("Gray");
JButton Black = new JButton ("Black");
JButton Back = new JButton ("Back to Menu");
JPanel panel1 = new JPanel();

public DemoShapes() {


    ArrayList<ShapeItem> shapes = new ArrayList<ShapeItem>();

    shapes.add(new ShapeItem(new Ellipse2D.Double(1, 1, 100, 100),
            DEFAULT_COLOR));
    shapes.add(new ShapeItem(new Rectangle2D.Double(110, 1, 100, 100),
            DEFAULT_COLOR));
Polygon triangle = new Polygon();

contentPane = frame.getContentPane();


Red.setBounds (150, 380, 110, 30);
Orange.setBounds (290, 380, 110, 30);
Yellow.setBounds (430, 380, 110, 30);
Green.setBounds (150, 430, 110, 30);
Blue.setBounds (290, 430, 110, 30);
Pink.setBounds (430, 430, 110, 30);
White.setBounds (150, 480, 110, 30);
Gray.setBounds (290, 480, 110, 30);
Black.setBounds (430, 480, 110, 30);
Back.setBounds (5, 5, 170, 30);


panel1.add(Red);
//Red.addActionListener(this);
panel1.add(Orange);
//Orange.addActionListener(this);
panel1.add(Yellow);
//Yellow.addActionListener(this);
panel1.add(Green);
//Green.addActionListener(this);
panel1.add(Blue);
//Blue.addActionListener(this);
panel1.add(Pink);
//Pink.addActionListener(this);
panel1.add(White);
//White.addActionListener(this);
panel1.add(Gray);
//Gray.addActionListener(this);
panel1.add(Black);
//Black.addActionListener(this);
panel1.add(Back);
//Back.addActionListener(this);

    ShapesPanel panel = new ShapesPanel(shapes);
panel1.setLayout(null);
    frame.add(panel);
//frame.add(panel1);    
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,600);
frame.setResizable(false);
    frame.setVisible(true);
}

class ShapeItem {
    private Shape shape;
    private Color color;

    public ShapeItem(Shape shape, Color color) {
        super();
        this.shape = shape;
        this.color = color;
    }

    public Shape getShape() {
        return shape;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}


class ShapesPanel extends JPanel {
    private List<ShapeItem> shapes;
    private Random rand = new Random();

    public ShapesPanel(List<ShapeItem> shapesList) {
        this.shapes = shapesList;

        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                Color color = getRandomColor();
                for (ShapeItem item : shapes) {
                    if (item.getShape().contains(e.getPoint())) {
                        item.setColor(color);
                    }
                }
                repaint();
            }
        });
    }


    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g.create();

        for (ShapeItem item : shapes) {
            g2.setColor(item.getColor());
            g2.fill(item.getShape());
        }

        g2.dispose();
    }


    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    private Color getRandomColor() {
        return new Color(rand.nextFloat(), rand.nextFloat(),
            rand.nextFloat());
    }
}

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

1 个答案:

答案 0 :(得分:3)

变量名不应以大写字母开头。

  

我设法在按钮上进行布局,但它们不会出现形状

您使用的是null布局。不要使用null布局,也不要使用setBounds(...)。布局管理器将设置每个组件的大小/位置。

您有10个按钮,因此我建议您开始使用GridLayout

panel1.setLayout ( new GridLayout(0, 5) );
panel1.add(red);
...

JFrame的默认布局管理器是BorderLayout

frame.add(panel);
//frame.add(panel1);   

如果未指定约束,则组件将添加到CENTER。问题是只有一个组件可以添加到CENTER中,因此您需要指定不同的约束:

frame.add(panel, BorderLayout.CENTER);
frame.add(panel1, BorderLayout.PAGE_END);   

阅读Layout Managers上Swing教程中的部分,了解更多信息和工作示例。