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