我试图在运行时在JlayerdPane的任何一层上画一条线。我现在面临的是,一旦我释放鼠标,画的线就会自动删除。我希望绘制的线一直在那里,直到我再次单击鼠标为止。
我打电话给下面的书面课,这边是
iDimension = new getDimension();
iDimension.setBounds(1, 12, 441, 380);
//iDimension.setOpaque(true);
iDimension.setBackground(new Color(0,0,0,100));
我已经添加了iDimension与分层窗格在这种方式
layeredPane.add(iDimension, new Integer(1),0);
这是getDimension类
public class getDimension extends JPanel {
public getDimension() {
setDoubleBuffered(true);
this.setBorder(UIManager.getBorder("ComboBox.border"));
this.repaint();
}
Point pointStart = null;
Point pointEnd = null;
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
repaint();
}
});
}
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) {
g.setColor(Color.GREEN);
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
// System.out.println("" + pointStart.x +"," + pointStart.y +"," + pointEnd.x +"," +pointEnd.y);
}
}
}
我是java的新手。如果我的问题有歧义,请纠正。
我现在面临的是,一旦我释放鼠标,画的线就会自动删除。我希望绘制的线一直在那里,直到我再次单击鼠标为止。
代码只做你让它做的事情:
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
});
和:
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) { // *********
g.setColor(Color.GREEN);
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
请注意,如果pointStart是null
,则不绘制该线,而是在mouseReleased上将其设置为null!解决方案——不要那样做。
例如
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class GetDimension extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private Point pointStart = null;
private Point pointEnd = null;
public GetDimension() {
this.setBorder(UIManager.getBorder("ComboBox.border"));
this.repaint();
MouseAdapter myMouse = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
repaint();
}
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
repaint();
}
};
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (pointStart != null && pointEnd != null) {
g.setColor(Color.GREEN);
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
GetDimension mainPanel = new GetDimension();
JFrame frame = new JFrame("GetDimension");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}