java中定义点坐标,如何在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?

解决方案

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.

Single click creates vertices of your polygon

Double-click stores the current polygon and starts creating a new polygon

Right-click clears the current polygon and starts a new one.

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

YeDjk.png

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 polygons = new ArrayList();

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

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值