java小画板问题
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classPainPanel2extendsJPa...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class PainPanel2 extends JPanel implements MouseMotionListener, MouseListener {
private Point startPoint = new Point(-1,-1);
private Point endPoint = new Point(-1,-1);
public PainPanel2() {
super();
addMouseMotionListener(this);
addMouseListener(this);
}
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(3.0F));
g2d.setPaint(Color.BLACK);
g2d.drawLine(startPoint.x, startPoint.y,
endPoint.x, endPoint.y);
repaint();
//startPoint = endPoint;
}
public void mouseDragged(MouseEvent e) {
endPoint = e.getPoint();
repaint();
}
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
}
public void mouseMoved(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
public class MainPanel extends JPanel{
public MainPanel() {
super(new BorderLayout());
add(new PaintPanel());
setPreferredSize(new Dimension(320, 240));
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("PanitPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.getContentPane().add(new MainPanel());
frame.setContentPane(new MainPanel());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
简单的类似画笔的程序,如何解决窗口最小化的时候,画板重置的问题,谢谢了
public void paint(Graphics g) {
//super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(3.0F));
g2d.setPaint(Color.BLACK);
g2d.drawLine(startPoint.x, startPoint.y,
endPoint.x, endPoint.y);
repaint();
startPoint = endPoint;
}
这一段复制错了无效了super无效了可以正常运行,super有效可以避免重置问题,但是不能绘画了,开始point一直变成结束point
就是最小化了以后再还原时,画的东西都没了 展开
import java.awt.event.*;
import javax.swing.*;
class PainPanel2 extends JPanel implements MouseMotionListener, MouseListener {
private Point startPoint = new Point(-1,-1);
private Point endPoint = new Point(-1,-1);
public PainPanel2() {
super();
addMouseMotionListener(this);
addMouseListener(this);
}
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(3.0F));
g2d.setPaint(Color.BLACK);
g2d.drawLine(startPoint.x, startPoint.y,
endPoint.x, endPoint.y);
repaint();
//startPoint = endPoint;
}
public void mouseDragged(MouseEvent e) {
endPoint = e.getPoint();
repaint();
}
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
}
public void mouseMoved(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
public class MainPanel extends JPanel{
public MainPanel() {
super(new BorderLayout());
add(new PaintPanel());
setPreferredSize(new Dimension(320, 240));
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("PanitPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.getContentPane().add(new MainPanel());
frame.setContentPane(new MainPanel());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
简单的类似画笔的程序,如何解决窗口最小化的时候,画板重置的问题,谢谢了
public void paint(Graphics g) {
//super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(3.0F));
g2d.setPaint(Color.BLACK);
g2d.drawLine(startPoint.x, startPoint.y,
endPoint.x, endPoint.y);
repaint();
startPoint = endPoint;
}
这一段复制错了无效了super无效了可以正常运行,super有效可以避免重置问题,但是不能绘画了,开始point一直变成结束point
就是最小化了以后再还原时,画的东西都没了 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询