1

我正在尝试绘制用户在 JPanel 上单击的点。能够识别坐标,但是在repaint()调用之前的点时会被覆盖。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.util.*;


/**
 *
 * @author emil
 */
public class GuiForTsp extends javax.swing.JFrame {

    /** Creates new form GuiForTsp */
    class MousePanelA extends JPanel implements MouseListener{
    public MousePanelA(){
        super();
        addMouseListener(this);
        System.out.println("constructing MousePanelA");
    }

    int x;
    int y;
    int count = 0;
    ArrayList xlist = new ArrayList();
    ArrayList ylist = new ArrayList();



    /*  public void update(Graphics g)
    {
        paintComponent(g);
        System.out.println("hi");
        }*/

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillRect(x,y,5,5);
        System.out.println("filling rectangle");
        //repaint();
    }

    public void mouseClicked(MouseEvent mouse){
        System.out.println("Mouse clicked"); 
        count++;   
        x = mouse.getX();
        y = mouse.getY();
        xlist.add(x);
        ylist.add(y);
        System.out.println("x " + xlist);
        System.out.println("y " + ylist);

        System.out.println("x " + x);
        System.out.println("y " + y);
        //g.fillRect(x,y,5,5);
        repaint();
    }

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

    JButton jButton1;
    MousePanelA jPanel1;

    public GuiForTsp() {
    initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel1 = new MousePanelA();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(30, 157, 164));


        jButton1.setText("Generate ");

        jPanel1.setBackground(new java.awt.Color(67, 152, 46));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 287, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 283, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GuiForTsp().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     

    // End of variables declaration                   

}

请告诉我如何修改程序,以便在单击新点时它与以前的点一起显示。我需要知道是否有任何简单的方法可以做到这一点,而不是存储单击的点值并在每次单击新点时重新绘制所有这些值。如果您可以提供代码片段,那将是可观的。

4

3 回答 3

2

首先,您应该将此 Graphics 复制到 Graphics2D 中。

其次,每次重绘都会重新绘制整个屏幕,您需要在此方法上每次绘制所有点。

那些列表 xList 和 yList 有前面的点,为什么不画呢?

 public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    for(int i = 0; i < xList.size(); i ++)
      g2d.fillRect(xList.get(i), yList.get(i), 5, 5);
    g2d.dispose();
    }

另外,考虑使用一个列表,而不是两个列表

于 2011-04-05T14:30:55.650 回答
0

您应该将点坐标存储在集合中(我建议使用 LinkedList),然后解析该列表(对于您喜欢的每个或任何其他循环)并再次绘制每个点。

问题是,每次调用“repaint()”时都需要执行此操作,但是(我认为)例如,当您调整窗口大小时会调用它,并且您不能覆盖此方法。我知道您必须将该代码片段放在特定方法中,但我忘记了哪一个(可能在 update(Graphics g) 中?)。

即使我没有提供完美的答案,也希望这会有所帮助。

于 2011-04-05T14:41:34.763 回答
0

自定义绘画方法展示了两种方法来做到这一点。

于 2011-04-05T15:18:44.847 回答