如何在JavaSwing中命名坐标(点) 2 月,3 周 Questions & Answers 1664 我正在使用JavaSwing设计一个接口。有一个画布供用户绘制形状(圆形、三角形、正方形等)。当用户绘制形状时,我希望按字母顺序命名形状中的每个点。我知道如何获取坐标,但如何命名点
# 1 楼答案 如果我理解正确,您需要按字母顺序标记坐标(如A、B、C、D) 既然你说你已经知道坐标了。。。使用JLabel: JLabel A_label = new JLabel("A"); JLabel B_label = new JLabel("B"); A_label.setLocation(shape1_x, shape1_y); B_label.setLocation(shape2_x, shape2_y);
# 2 楼答案 这里有一种方法。您可以使用Character.toString(char)和'A'+offset从字母表中获取任何字符 请参见这个小演示示例,它绘制多边形 单击可创建多边形的顶点 双击存储当前多边形并开始创建新多边形 单击鼠标右键清除当前多边形并开始新多边形李> 旁注:文本的定位并不智能,所以它有时会和多边形的线条重叠 import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Point; import java.awt.Polygon; import java.awt.event.MouseAdapter; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class TestNaming { private static final int PANEL_WIDTH = 600; private static final int PANEL_HEIGHT = 600; public static class Drawing extends JPanel { private static final Font FONT = new Font("Arial", Font.PLAIN, 12); private List<Polygon> polygons = new ArrayList<Polygon>(); private Polygon currentPolygon = new Polygon(); private MouseAdapter mouseListener = new MouseAdapter() { @Override public void mouseClicked(java.awt.event.MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { if (e.getClickCount() == 1) { addPoint(e.getX(), e.getY()); } else if (e.getClickCount() == 2) { createPolygon(); } } else if (SwingUtilities.isRightMouseButton(e)) { clearCurrentPolygon(); } } }; public Drawing() { addMouseListener(mouseListener); } protected void addPoint(int x, int y) { currentPolygon.addPoint(x, y); repaint(); } protected void clearCurrentPolygon() { currentPolygon = new Polygon(); repaint(); } protected void createPolygon() { if (currentPolygon.npoints > 2) { polygons.add(currentPolygon); } clearCurrentPolygon(); repaint(); } @Override public Dimension getPreferredSize() { return new Dimension(PANEL_WIDTH, PANEL_HEIGHT); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.setFont(FONT); for (Polygon polygon : polygons) { drawPolygon(g, polygon); } g.setColor(Color.GREEN); drawPolygon(g, currentPolygon); } private void drawPolygon(Graphics g, Polygon polygon) { if (polygon.npoints < 3) { if (polygon.npoints == 1) { g.fillOval(polygon.xpoints[0] - 2, polygon.ypoints[0] - 2, 4, 4); drawNthPoint(g, polygon, 0); } else if (polygon.npoints == 2) { g.drawLine(polygon.xpoints[0], polygon.ypoints[0], polygon.xpoints[1], polygon.ypoints[1]); drawNthPoint(g, polygon, 0); drawNthPoint(g, polygon, 1); } } else { g.drawPolygon(polygon); for (int i = 0; i < polygon.npoints; i++) { drawNthPoint(g, polygon, i); } } } private void drawNthPoint(Graphics g, Polygon polygon, int nth) { // Only works 26 times! String name = Character.toString((char) ('A' + nth)); int x = polygon.xpoints[nth]; int height = g.getFontMetrics().getHeight(); int y = polygon.ypoints[nth] < height ? polygon.ypoints[nth] + height : polygon.ypoints[nth]; g.drawString(name, x, y); } } protected static void initUI() { JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Drawing()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initUI(); } }); } }
# 1 楼答案
如果我理解正确,您需要按字母顺序标记坐标(如A、B、C、D)
既然你说你已经知道坐标了。。。使用
JLabel
:# 2 楼答案
这里有一种方法。您可以使用
Character.toString(char)
和'A'+offset
从字母表中获取任何字符请参见这个小演示示例,它绘制多边形
旁注:文本的定位并不智能,所以它有时会和多边形的线条重叠