在JPanel java中显示多边形的坐标

时间:2015-10-31 10:49:30

标签: java user-interface jpanel

我是java的新手,我试图一个接一个地绘制一些线条。当我正确clik时,(种类)多边形结束,然后在下一次(左)单击时启动另一个多边形。我想在JLabel中显示这些多边形的每个点的坐标。它应该像这样看(例如)

http://i.stack.imgur.com/pmlDF.png

只有在单击“Ausgabe”按钮时才会显示坐标。我试图将坐标保存在列表中,但我不知道如何在JLabel中显示它。这是我的代码,直到现在。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.List;


class MousePanel extends JPanel implements MouseListener,ActionListener{

private int x,y,x2,y2,a=1;
Point2D p= new Point2D.Double(x,y);
public final List <Point2D> coordinates= new ArrayList <Point2D>();
public final String listString= coordinates.toString();


public MousePanel(){
    super();
    addMouseListener(this);   
}

public void paint(Graphics g){              // draws the lines
    Graphics2D g2d= (Graphics2D) g;
    GeneralPath gp = new GeneralPath ();
    gp.moveTo (x,y);
    gp.lineTo(x2,y2);

    g2d.draw(gp);

} 

public void mousePressed(MouseEvent mouse){         
    if (SwingUtilities.isLeftMouseButton(mouse)){   // if left mouse button is clicked
        if (a == 1) {                               // the lines ar one after the another
            a = 0;                                  // set the first click coordinates
            x = x2 = mouse.getX();
            y = y2 = mouse.getY();

            coordinates.add(p);                     // saves the coordinates in the list
        } 
        else {
            x = x2;                         
            y = y2;
            x2 = mouse.getX();
            y2 = mouse.getY();
            repaint();
            coordinates.add(p);
        }}
    else {                                      // if right mouse button is clicked
                a = 1;                          // --> new polygon/ line
                x = x2;
                y = y2;
                x2 = mouse.getX();
                y2 = mouse.getY();
                repaint();
                coordinates. add(p);
            }

    }

public void mouseEntered(MouseEvent mouse){ }   
public void mouseExited(MouseEvent mouse){ }
public void mouseClicked(MouseEvent mouse){ }
public void mouseReleased(MouseEvent mouse){ }


@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

} 
}

class MyGui extends JFrame implements ActionListener {
JLabel label = new JLabel ("<html>First line<br>Second line</html>");

public void createGUI() {                           // creates the frame
setTitle("Monica's first GUI");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 700);

JPanel container = (JPanel) getContentPane();
container.setLayout(null);
label.setBounds(0,430,500,130);
label.setBackground(Color.white);
label.setOpaque(true);
getContentPane().add(label);


JButton go = new JButton("Beenden");            // creates and adds the buttons
go.setBounds(250,580,130,40);
container.add(go);
go.addActionListener(this);

JButton go2 = new JButton("Ausgabe");
go2.setBounds(100,580,130,40);
container.add(go2);
go2.addActionListener(this);

JMenuBar menubar=new JMenuBar();                // creates the menu
JMenu menu=new JMenu("Menu");
JMenuItem exit=new JMenuItem("Exit");
JMenuItem reset=new JMenuItem ("Reset");
JMenuItem ausgabe= new JMenuItem ("Ausgabe2");
menu.add("Save"); 
menu.add(reset);
JMenu edit= new JMenu ("Edit");
menu.add(edit);
edit.add("Copy");
edit.add("Cut");
edit.add("Paste");
menu.add(exit);
menu.add(ausgabe);
menubar.add(menu);
setJMenuBar(menubar);
exit.addActionListener(this);
reset.addActionListener(this);
ausgabe.addActionListener(this);

}



  public void actionPerformed(ActionEvent e)        // the buttons respond when clicked
  {  
  if(e.getActionCommand()=="Beenden")
  {
    System.exit(0);
  }

  if(e.getActionCommand()=="Ausgabe")
  {

  }
  if(e.getActionCommand()=="Exit")
  {
    System.exit(0);
  } 
  if (e.getActionCommand()=="Reset")        // clears the JPanel
  {
     getContentPane().repaint();
     label.setText("");
     }
  }
}


public class MyFirstGui {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    MyGui myGUI = new MyGui();
    myGUI.createGUI();
    MousePanel panel = new MousePanel();
    panel.setBounds(0,0,500,430);
    myGUI.getContentPane().add(panel);
    myGUI.setVisible(true);
  }
});
}
}

任何帮助???

1 个答案:

答案 0 :(得分:1)

因此,为了实现这一点,您需要更改有关代码的一些内容。首先,要么使你的坐标ArrayList公共静态

public static final List <Point2D> coordinates= new ArrayList <Point2D>();

或将您正在创建的MousePanel实例传递给MyGui。

public class MyFirstGui {
  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {

        MousePanel panel = new MousePanel();
        panel.setBounds(0,0,500,430);
        MyGui myGUI = new MyGui(panel);
        myGUI.createGUI();
        myGUI.getContentPane().add(panel);
        myGUI.setVisible(true);
      }
    });
  }
}

然后,您将本地实例存储在MyGui中。

class MyGui extends JFrame implements ActionListener {

  private MousePanel storedMousePanel;

  public MyGui(MousePanel mousePanel) {
    this.storedMousePanel = mousePanel;
  }

}

接下来,您可以使用存储的实例或静态列表来访问您的坐标。要按照您想要的方式打印它们,我们可以使用for循环迭代List,然后将所有坐标添加到一个大字符串。

public void actionPerformed(ActionEvent e)        // the buttons respond when clicked
{  
    if (e.getActionCommand()=="Beenden") {
        ...
    }      
    else if(e.getActionCommand()=="Ausgabe")
    {
      getContentPane().repaint();

      String labelText = "";

      //Using the static List
      for (int i = 0; i < MousePanel.coordinates.size(); i++) {
        labelText += "(" + String.valueOf(MousePanel.coordinates.get(i).getX()) + ")(" + String.valueOf(MousePanel.coordinates.get(i).getY()) + ")\n";
      }

      //Using the stored instance
      for (int i = 0; i < storedMousePanel.coordinates.size()) {
        labelText += "(" + String.valueOf(storedMousePanel.coordinates.get(i).getX()) + ")(" + String.valueOf(storedMousePanel.coordinates.get(i).getY()) + ")\n";
      }

      label.setText(labelText);
    }
    else if(e.getActionCommand()=="Exit")
    {
      System.exit(0);
    } 
    else if (e.getActionCommand()=="Reset")        // clears the JPanel
    {
      getContentPane().repaint();
      label.setText("");
    }
 }