java – 窗口的内容在最小化时消失

前端之家收集整理的这篇文章主要介绍了java – 窗口的内容在最小化时消失前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的类,当鼠标拖动时绘制一条线或鼠标按下(释放)时画一个点.

当我最小化应用程序然后恢复它时,窗口的内容消失,除了最后一个点(像素).我知道方法super.paint(g)每次窗口改变时重新绘制背景,但无论我是否使用它,结果似乎都是一样的.它们之间的区别在于,当我不使用它时,窗口上绘制的不仅仅是一个像素,而不是我的所有绘画.我怎样才能解决这个问题?

这是班级.

  1. package painting;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseMotionAdapter;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. class CustomCanvas extends Canvas{
  11. Point oldLocation= new Point(10,10);
  12. Point location= new Point(10,10);
  13. Dimension dimension = new Dimension(2,2);
  14. CustomCanvas(Dimension dimension){
  15. this.dimension = dimension;
  16. this.init();
  17. addListeners();
  18. }
  19. private void init(){
  20. oldLocation= new Point(0,0);
  21. location= new Point(0,0);
  22. }
  23. public void paintLine(){
  24. if ((location.x!=oldLocation.x) || (location.y!=oldLocation.y)) {
  25. repaint(location.x,location.y,1,1);
  26. }
  27. }
  28. private void addListeners(){
  29. addMouseListener(new MouseAdapter(){
  30. @Override
  31. public void mousePressed(MouseEvent me){
  32. oldLocation = location;
  33. location = new Point(me.getX(),me.getY());
  34. paintLine();
  35. }
  36. @Override
  37. public void mouseReleased(MouseEvent me){
  38. oldLocation = location;
  39. location = new Point(me.getX(),me.getY());
  40. paintLine();
  41. }
  42. });
  43. addMouseMotionListener(new MouseMotionAdapter() {
  44. @Override
  45. public void mouseDragged(MouseEvent me){
  46. oldLocation = location;
  47. location = new Point(me.getX(),me.getY());
  48. paintLine();
  49. }
  50. });
  51. }
  52. @Override
  53. public void paint(Graphics g){
  54. super.paint(g);
  55. g.setColor(Color.red);
  56. g.drawLine(location.x,oldLocation.x,oldLocation.y);
  57. }
  58. @Override
  59. public Dimension getMinimumSize() {
  60. return dimension;
  61. }
  62. @Override
  63. public Dimension getPreferredSize() {
  64. return dimension;
  65. }
  66.  
  67. }
  68. class CustomFrame extends JPanel {
  69. JPanel displayPanel = new JPanel(new BorderLayout());
  70. CustomCanvas canvas = new CustomCanvas(new Dimension(200,200));
  71. public CustomFrame(String titlu) {
  72. canvas.setBackground(Color.white);
  73. displayPanel.add(canvas,BorderLayout.CENTER);
  74. this.add(displayPanel);
  75. }
  76. }
  77. public class CustomCanvasFrame {
  78. public static void main(String args[]) {
  79. CustomFrame panel = new CustomFrame("Test Paint");
  80. JFrame f = new JFrame();
  81. f.add(panel);
  82. f.pack();
  83. SwingConsole.run(f,700,700);
  84. }
  85. }

解决方法

您没有存储您正在绘制的点的状态.重新绘制面板时,它仅显示其绘制的最后一点的信息.

回复评论

您需要有一个Points集合,例如ArrayList< Point> location = new ArrayList< Point>();

然后,在你的监听器中:location.add(new Point(me.getX(),me.getY()));

最后,在paintLine()中:

  1. for (Point location : locations) {
  2. repaint(location.x,1);
  3. }

收集位置通常称为显示列表.大多数图形程序都使用它们

回复评论

是的,我希望如此.我只是根据你的代码抛出一个想法给你一个起点.完全按照我的描述做几乎肯定是个坏主意.

猜你在找的Java相关文章