問題描述
嘿,我知道繪制橢圓/矩形并使用填充它很簡(jiǎn)單
Hey I know that it is simple to draw oval/rectangle and fill it using
g.fillOval(30, 40, 20, 20);
但是如何畫一個(gè)三角形呢?最好有隨機(jī)坐標(biāo).
but how to draw a triangle? It would be the best if it would have random coordinates.
推薦答案
根據(jù)您的需要,至少有兩種基本方法可以實(shí)現(xiàn)這一目標(biāo).
There are at least two basics ways you can achieve this, based on your needs.
您可以使用 Polygon
或者你可以使用 2D Graphics Shape API一個(gè)>
您可能會(huì)選擇哪一種取決于您的要求.Polygon
要求您提前知道點(diǎn)在 3D 空間中的位置,而 Shape
API 讓您可以更自由地定義形狀,而無(wú)需事先關(guān)心位置.
Which you might choose comes down to your requirements. Polygon
requires you to know, in advance the position of the points within 3D space, where the Shape
API gives you more freedom to define the shape without caring about the position in advance.
這使得 Shape
API 更加靈活,因?yàn)槟梢远x一次形狀,然后根據(jù)需要簡(jiǎn)單地轉(zhuǎn)換 Graphics
上下文并重新繪制它.
This makes the Shape
API more flexible, in that you can define the shape once and simply translate the Graphics
context as needed and repaint it.
例如...
紅色是Polygon
,綠色是Shape
,翻譯成位置...
Red is a Polygon
, green is a Shape
, which is translated into position...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TriangleTest {
public static void main(String[] args) {
new TriangleTest();
}
public TriangleTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private TriangleShape triangleShape;
private Polygon poly;
public TestPane() {
triangleShape = new TriangleShape(
new Point2D.Double(50, 0),
new Point2D.Double(100, 100),
new Point2D.Double(0, 100)
);
poly = new Polygon(
new int[]{50, 100, 0},
new int[]{0, 100, 100},
3);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.fill(poly);
g2d.setColor(Color.GREEN);
g2d.translate(50, 100);
g2d.fill(triangleShape);
g2d.dispose();
}
}
public class TriangleShape extends Path2D.Double {
public TriangleShape(Point2D... points) {
moveTo(points[0].getX(), points[0].getY());
lineTo(points[1].getX(), points[1].getY());
lineTo(points[2].getX(), points[2].getY());
closePath();
}
}
}
這篇關(guān)于Java - 畫一個(gè)三角形的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!