在JAVA GUI中添加随机形状矩形,椭圆和线条

时间:2017-11-06 11:47:29

标签: java swing random paint

嘿,我试图设计一个GUI程序ShapeFactory,它有按钮来生成:

一个。随机颜色的随机矩形。

湾随机椭圆随机颜色。

℃。随机颜色的随机线。

每当用户点击某个按钮时,相应的形状就会添加到JFrame中已绘制的形状中。

并且我一直试图将ActionListener与paint method

连接起来
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShapeFactory extends JFrame implements ActionListener{

    public static void main(String[] args) {
        new ShapeFactory();
}

    JButton ranRects = new JButton("Random Rectangles");
    JButton ranElli = new JButton("Random Ellipses");
    JButton ranLines = new JButton("Random Lines");
    Graphics g;
    Color c;
    ArrayList shapeList = new ArrayList( );

public ShapeFactory(){
    super("Shape Factory");
    setLayout(new FlowLayout());
    setSize(600,400);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    add(ranRects);
    add(ranElli);
    add(ranLines);

    JPanel nPanel = new JPanel (new GridLayout(1,3));
    nPanel.add(ranRects);
    nPanel.add(ranElli);
    nPanel.add(ranLines);

    add(nPanel);


    ranRects.addActionListener(this);

    ranElli.addActionListener(this);        

    ranLines.addActionListener(this);


    setVisible(true);
}

    public void paint (Graphics g) {

        super.paint(g);

    if (d == 1) {
        paintRect(g);
        shapeList.add(d);
        }

    else if (d == 2) {
        paintOval(g);
        shapeList.add(d);
        }

    else if (d == 3 ) {
        paintLine(g);
        shapeList.add(d);
        }
} 

    public void paintRect(Graphics g) {

        int rectX1 = (int)(Math.random() * getWidth() / 4.0);
        int rectY1 = (int)(Math.random() * getHeight()/ 4.0);
        int rectX2 = (int)(Math.random() * getWidth());
        int rectY2 = (int)(Math.random() * getHeight());
        c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
        g.setColor(c);
        g.fillRect(rectX1,rectY2,rectX2,rectY2);
    }
    public void paintOval(Graphics g) {

        int ciX1 = (int)(Math.random() * getWidth() / 4.0);
        int ciY1 = (int)(Math.random() * getHeight()/ 4.0);
        int ciX2 = (int)(Math.random() * getWidth());
        int ciY2 = (int)(Math.random() * getHeight());
        c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
        g.setColor(c);
        g.fillOval(ciX1,ciY1,ciX2,ciY2);
    }
    public void paintLine(Graphics g) {

        int lineX1 = (int)(Math.random() * getWidth() / 4.0);
        int lineY1 = (int)(Math.random() * getHeight()/ 4.0);
        int lineX2 = (int)(Math.random() * getWidth());
        int lineY2 = (int)(Math.random() * getHeight());
        c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
        g.setColor(c);
        g.drawLine(lineX1,lineY1,lineX2,lineY2);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (source == ranRects) { 
            d = 1;
        }
        else if (source == ranElli) {
            d = 2; 
        }
        else if (source == ranLines) {
            d = 3;

        }
        repaint();  

    }

}

1 个答案:

答案 0 :(得分:1)

因此,一种解决方案是使用3个成员变量来跟踪需要绘制的每种类型的形状数量。单击一个按钮后,您只需递增相应的变量,然后在paint方法中调用paintOval或多次调整形状。在这种情况下,您的paint方法可能如下所示:

public void paint(Graphics g) {
    super.paint(g);
    for (int i = 0; i < this.numberOfRects; i++)
        paintRect(g);
    for (int i = 0; i < this.numberOfEllis; i++)
        paintOval(g);
    for (int i = 0; i < this.numberOfLines; i++)
        this.paintLine(g);
}

actionPerformed就像这样:

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == ranRects) {
        this.numberOfRects++;
    } else if (source == ranElli) {
        this.numberOfEllis++;
    } else if (source == ranLines) {
        this.numberOfLines++;
    }
    repaint();
}

你当然需要将3个成员变量添加到类中。

如果你想存储形状的位置和颜色,我建议你创建一个新的类来将它们存储在某种List中。您可以创建内部类Shape来存储形状属性,如位置,大小和颜色。

private class Shape{
    public int x1;
    public int y1;
    public int x2;
    public int y2;
    public Color color;
}

然后,您可以为每种形状添加成员变量:

private List<Shape> rectList = new ArrayList<>();
private List<Shape> ovalList = new ArrayList<>();
private List<Shape> lineList = new ArrayList<>();

我建议使用一种方法来添加这样的形状:

public void addRect(){
    //Create new Shape
    Shape shape = new Shape();

    //Set shape's properties
    shape.x1 = (int) (Math.random() * getWidth() / 4.0);
    shape.x2 = (int) (Math.random() * getHeight() / 4.0);
    shape.y1 = (int) (Math.random() * getWidth());
    shape.y2 = (int) (Math.random() * getHeight());
    shape.color =  new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));

    //Add the shape to the list
    this.rectList.add(shape);
} 

并在actionListener中调用该方法

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == ranRects) {
        addRect();
    } else if (source == ranElli) {
        addOval();
    } else if (source == ranLines) {
        addLine();
    }
    repaint();
}

要绘制它们,您只需迭代每种形状的列表:

public void paint(Graphics g) {
    super.paint(g);
    for (Shape s: rectList )
        paintRect(g, s);
    //...
    //Add loops for all shapes
}

public void paintRect(Graphics g, Shape shape) {
    g.setColor(shape.color);
    g.fillRect(shape.x1, shape.y1, shape.x2, shape.y2);
}

最后你应该有这样的东西:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

public class ShapeFactory extends JFrame implements ActionListener {

    public static void main(String[] args) {
        new ShapeFactory();
    }

    JButton ranRects = new JButton("Random Rectangles");
    JButton ranElli = new JButton("Random Ellipses");
    JButton ranLines = new JButton("Random Lines");
    private List<Shape> rectList = new ArrayList<>();
    private List<Shape> ovalList = new ArrayList<>();
    private List<Shape> lineList = new ArrayList<>();

    private class Shape{
        public int x1;
        public int y1;
        public int x2;
        public int y2;
        public Color color;
    }

    public ShapeFactory() {
        super("Shape Factory");
        setLayout(new FlowLayout());
        setSize(600, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        add(ranRects);
        add(ranElli);
        add(ranLines);

        JPanel nPanel = new JPanel(new GridLayout(1, 3));
        nPanel.add(ranRects);
        nPanel.add(ranElli);
        nPanel.add(ranLines);

        add(nPanel);


        ranRects.addActionListener(this);

        ranElli.addActionListener(this);

        ranLines.addActionListener(this);


        setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        for (Shape s: rectList )
            paintRect(g, s);
        //...
        //Add loops for all shapes
    }

    public void paintRect(Graphics g, Shape shape) {
        g.setColor(shape.color);
        g.fillRect(shape.x1, shape.y1, shape.x2, shape.y2);
    }
    //...
    //Add paint methods for oval and lines

    public void addRect(){
        //Create new Shape
        Shape shape = new Shape();

        //Set shape's properties
        shape.x1 = (int) (Math.random() * getWidth() / 4.0);
        shape.x2 = (int) (Math.random() * getHeight() / 4.0);
        shape.y1 = (int) (Math.random() * getWidth());
        shape.y2 = (int) (Math.random() * getHeight());
        shape.color =  new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
        //Add the shape to the list

        this.rectList.add(shape);
    }
    //...
    //Add methods to add ovals and lines


    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == ranRects) {
            addRect();
        } else if (source == ranElli) {
            //addOval();
        } else if (source == ranLines) {
            //addLine();
        }
        repaint();
    }
}

我只为矩形实现了paintadd方法,但添加椭圆和线条并不困难。 希望这有帮助