Java Swing – 動的に図形を描画する

この記事では、JPanelにランダムにシェイプを適用するアプリケーションを作成します。ユーザーは、アプリケーションがサポートするさまざまな図形と描画される量の中から選択できます。設計上の決定として、アプリケーションは円と星を作ることができます。サンプルの最後にサンプルをダウンロードし、より多くの図形、ランダムな色、ランダムなサイズなどを試してみることができます。
1.サークル
コンストラクタは円のx座標とy座標を受け取り、直径は10pxに設定されます。
Circle.java
package com.techfou.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`クラスを使います。私たちは、 `GeneralPath`が星を描くために従わなければならないx座標とy座標の2つの配列を持っています。
行は(9,0)から始まり、一連のポイントを通過して(3,18)、最後に「closePath()」は「開始位置に戻る」を意味します。
Java’s coordinate system
を使用してこれを紙の上に描画すると、星があります!星を描く方法は一つもありません。あなたは数字で遊んで自分の星や他の形を作ることもできます!
Star.java
package com.techfou.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メソッド`では、ユーザーにシェイプの量と種類を尋ね、 `JFrame`を作成し、
DynamicShapes`クラスの呼び出しで
JPanel`を初期化します。 `JPanel`を拡張する
DynamicShapes`クラスは、
List`に追加されたシェイプを
paintComponent()
メソッドを通して描画します。 `List`は
DynamicShapes`クラスのコンストラクタから生成されます。このクラスはユーザの入力に応じて各シェイプのメソッドを呼び出します。
DynamicShapes.java
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」と入力し、「OK」をクリックします。

[両方]を選択し、[OK]をクリックします。

結果は100個の星と100個の円が `JPanel`内にランダムに配置されます:

すべての組み合わせで
100シェイプ:

ソースコードをダウンロードする
ダウンロード:
DrawShapesDynamically.zip
(4 KB)
参考文献
座標チュートリアル]。
https://docs/
JavaDoc]。
https://docs.oracle.com/javase/8/docs/api/javax/swing/JPanel.html
[javax.swing.JPanel
JavaDoc]