在本文中,我们创建了一个将形状随机放置在JPanel上的应用程序。 用户可以在我们的应用程序支持的不同形状以及将要绘制的数量之间进行选择。 作为设计决策,我们的应用程序可以制作圆形和星形。 您可以在文章末尾下载示例,并尝试使用更多形状,随机颜色,随机大小等。
1.圈子
直径设置为10px时,构造函数将接收圆的x和y坐标。
package com.mkyong.dynamicshapes;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class Circle {
int x, y, width, height;
public Circle(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(x, y, 10, 10);
g2d.setColor(Color.GRAY);
g2d.fill(circle);
}
}
2.星
为了绘制星星,我们使用GeneralPath类。 我们有两个用于x和y坐标的数组, GeneralPath必须遵循这两个数组来绘制星形。 该行从(9,0)开始,经过一系列点到达(3,18),最后是closePath() ,这意味着“返回到我们开始的地方”。
使用Java的坐标系在纸上绘制此图形,您将获得一颗星星! 没有一种吸引星星的方法。 您可以玩弄数字,甚至制作自己的星星或其他形状!
package com.mkyong.dynamicshapes;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
public class Star {
int x, y, width, height;
public Star(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
int xPoints[] = {9, 15, 0, 18, 3};
int yPoints[] = {0, 18, 6, 6, 18};
Graphics2D g2d = (Graphics2D) g;
GeneralPath star = new GeneralPath();
star.moveTo(xPoints[0] + x, yPoints[0] + y);
for (int i = 1; i < xPoints.length; i++) {
star.lineTo(xPoints[i] + x, yPoints[i] + y);
}
star.closePath();
g2d.setColor(Color.YELLOW);
g2d.fill(star);
}
}
3.申请
在main method我们要求用户提供形状的数量和类型,然后创建JFrame并通过调用DynamicShapes类来初始化JPanel 。 扩展JPanel的DynamicShapes类通过paintComponent()方法绘制添加到List的形状。 该List从DynamicShapes类的构造函数填充,该构造函数根据用户输入为每种形状调用该方法。
package com.mkyong.dynamicshapes;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class DynamicShapes extends JPanel {
private List<Object> shapes = new ArrayList<>();
private Random random = new Random();
public DynamicShapes(int i, String shape) {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(400, 400));
switch (shape) {
case "Circles":
for (int j = 0; j < i; j++) {
addCircle(390, 390);
}
break;
case "Stars":
for (int j = 0; j < i; j++) {
addStar(380, 380);
}
break;
case "Both":
int mid = i / 2;
for (int j = 0; j < mid; j++) {
addCircle(390, 390);
}
for (int j = mid; j < i; j++) {
addStar(380, 380);
}
break;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Object s : shapes) {
if (s instanceof Circle) {
((Circle) s).draw(g);
} else if (s instanceof Star) {
((Star) s).draw(g);
}
}
}
public void addCircle(int maxX, int maxY) {
shapes.add(new Circle(random.nextInt(maxX), random.nextInt(maxY)));
repaint();
}
public void addStar(int maxX, int maxY) {
shapes.add(new Star(random.nextInt(maxX), random.nextInt(maxY)));
repaint();
}
public static void main(String[] args) {
String shapeAmount = JOptionPane.showInputDialog(null,
"How many shapes?", "Random Shapes...", JOptionPane.PLAIN_MESSAGE);
int amount = Integer.parseInt(shapeAmount);
String shapes[] = {"Stars", "Circles", "Both"};
String shape = (String) JOptionPane.showInputDialog(null,
"Pick the shape you want", "Random Shapes...",
JOptionPane.PLAIN_MESSAGE, null, shapes, shapes[0]);
JFrame frame = new JFrame();
frame.add(new DynamicShapes(amount, shape));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
输出:
输入“ 200”,然后单击“确定”
选择“两者”并单击“确定”
结果是将100个星星和100个圆圈随机放置在JPanel :
共有100种形状:
下载源代码
下载– DrawShapesDynamically.zip (4 KB)
参考文献
翻译自: https://mkyong.com/swing/java-swing-draw-shapes-dynamically-example/




