在 JAVA GUI 中添加随机形状矩形、椭圆和直线
adding random shapes Rectangles, Ellipses and lines in JAVA GUI
嘿,我正在尝试设计一个 GUI 程序 ShapeFactory,它具有要生成的按钮:
一个。随机颜色的随机矩形。
b。具有随机颜色的随机椭圆。
c。随机颜色的随机线条。
每当用户单击按钮时,相应的形状将添加到 JFrame 中已绘制的形状中。
我一直在尝试将 ActionListener 与绘画方法连接起来
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();
}
}
因此,一种解决方案是使用 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 个成员变量添加到 class。
如果您想存储形状的位置和颜色,我建议您创建一个新的 class 以将它们存储在某种 List
中。您可以创建一个内部 class 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();
}
}
我只为矩形实现了 paint
和 add
方法,但添加椭圆和直线并不难。
希望这对您有所帮助
嘿,我正在尝试设计一个 GUI 程序 ShapeFactory,它具有要生成的按钮:
一个。随机颜色的随机矩形。
b。具有随机颜色的随机椭圆。
c。随机颜色的随机线条。
每当用户单击按钮时,相应的形状将添加到 JFrame 中已绘制的形状中。
我一直在尝试将 ActionListener 与绘画方法连接起来
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();
}
}
因此,一种解决方案是使用 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 个成员变量添加到 class。
如果您想存储形状的位置和颜色,我建议您创建一个新的 class 以将它们存储在某种 List
中。您可以创建一个内部 class 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();
}
}
我只为矩形实现了 paint
和 add
方法,但添加椭圆和直线并不难。
希望这对您有所帮助