Java - draw a triangle [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

This question already has an answer here:

Hey I know that it is simple to draw oval/rectangle and fill it using

g.fillOval(30, 40, 20, 20); 

but how to draw a triangle? It would be the best if it would have random coordinates.

回答1:

There are at least two basics ways you can achieve this, based on your needs.

You could use Polygon or you could make use the 2D Graphics Shape API

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.

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.

For example...

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();         }      }  } 


回答2:

Using the legacy Graphics class, this would be done by using the legacy method
drawPolygon(int[] x, int[] y, int pointCount).

The newer class Graphics2D supports a much nicer implementation using Path2Ds. You will need either the draw(Shape) or fill(Shape) method. Given the fact that almost everywhere in Java a Graphics object is actually a Graphics2D object, you can cast it so, that is the way to go, IMHO.

Graphics g = ...; Graphics2D g2d = (Graphics2D) g; Path2D.Double triangle = new Path2D.Double(); triangle.moveTo(x1, y1); triangle.pathTo(x2, y2); triangle.pathTo(x3, y3); triangle.closePath(); g2d.fill(triangle); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!