2013-04-26 29 views
0

我正在設計一個使用java swing的接口。用戶可以繪製畫布(圓形,三角形,方形等)。當用戶繪製形狀時,我想按字母順序命名形狀中的每個點。我知道如何獲得座標,但我如何命名點?如何命名Java Swing中的座標(點數)

+0

A,B,C,d ...?我不明白你的問題的重點。 – SJuan76 2013-04-26 20:28:32

+0

是的。就像用戶繪製三角形時一樣,我希望三角形的三個點被命名爲A,B和C. – 2013-04-26 20:31:29

+0

'Graphics#drawString()'? – trashgod 2013-04-26 20:34:29

回答

2

這是一種方法。您使用Character.toString(char)並使用'A'+offset從字母表中獲取任何字符。

請參閱這個小示例,它繪製多邊形。

  • 單一的點擊創建您的多邊形
  • 雙擊存儲當前多邊形的頂點,並開始創建一個新的多邊形
  • 右鍵單擊清除當前的多邊形,並開始一個新的。

注意:文字的定位不夠智能,所以它有時會與多邊形的線重疊。

Demo

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

非常感謝!但是有沒有辦法讓文字的定位更好?或者沒有可能的辦法? – 2013-05-02 21:40:37

+0

@csstudent當然,這是可能的。您需要考慮構成頂點的兩條邊並計算出最適合放置文本的位置。你可以通過使用'Rectangle2D stringBounds = g.getFontMetrics()。getStringBounds(name,g);' – 2013-05-03 12:46:23

-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); 
+1

'setLocation'將不能用於'LayoutManager' – Justin 2013-04-26 21:51:30