如何在Java Swing中命名坐标(点) [英] How to name the coordinates (points) in Java Swing

查看:271
本文介绍了如何在Java Swing中命名坐标(点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java swing设计一个界面。用户可以使用画布绘制形状(圆形,三角形,正方形等)。当用户绘制形状时,我想按字母顺序命名形状中的每个点。我知道如何获得坐标,但我如何命名这些点?

I'm designing an interface using java swing. There is a canvas for the user to draw shapes (circle, triangle, square, etc.). When the user draws a shape, I want to name each point in the shape alphabetically. I know how to get the coordinates but how do I name the points?

推荐答案

这是一种方法。您使用 Character.toString(char)并使用'A'+ offset 从字母表中获取任何字符。

Here is one way to do it. You use Character.toString(char) and use 'A'+offset to get any char from the alphabet.

请参阅此小型演示示例,其中绘制多边形。

See in this small demo example, which draws polygons.


  • 单击创建多边形的顶点

  • 双击存储当前多边形并开始创建新多边形

  • 右键单击清除当前多边形并启动一个新多边形一个。

侧注:文本的定位不够智能,因此有时会重叠多边形的线条。

Side-note: positioning of the text is not smart, so it overlaps lines of the polygon sometimes.

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

这篇关于如何在Java Swing中命名坐标(点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆