0

我将尝试使用 Pen Tool 实现 Java Applet。当我启动小程序时,我可以通过按下和释放鼠标在窗口上正确绘制我的线条。几秒钟后,一切都消失了。解决此问题的一些提示?

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;


public class MarkPen extends Applet implements ActionListener, AdjustmentListener,     MouseListener, MouseMotionListener {

 private int x = 0;
 private int y = 0;
 private int prevx  = 0;
 private int prevy  = 0;

 private boolean dragging = true;
 private Panel drawPanel = new Panel();

 public void init()
 {
    setLayout(new BorderLayout());

    drawPanel.setBackground(Color.white);
    add(drawPanel, "Center");
    drawPanel.addMouseMotionListener(this);
    drawPanel.addMouseListener(this);

 }


 public void setGraphicalDefaults(MouseEvent e)
 {
    x = e.getX();
    y = e.getY();
    prevx = e.getX();
    prevy = e.getY();
 }

 public void mouseDragged(MouseEvent e)
 {
     Graphics g  = drawPanel.getGraphics();
        g.setColor(Color.black);

        /*
          IF i'm not dragging...
        */
        if (dragging)
        {
           setGraphicalDefaults(e);
           dragging = false;
           g.drawLine(prevx,prevy,x,y);
        }

        /*
          If mouse moved..
        */
        if (x != e.getX() || y != e.getY())
        {
           x = e.getX();
           y = e.getY();

           g.drawLine(prevx,prevy,x,y);

           prevx = x;
           prevy = y;
        }
 }

 public void mouseReleased(MouseEvent e)
 {  
     dragging = true;
 }
 public void mouseEntered(MouseEvent e){}
 public void mouseExited(MouseEvent e){}
 public void mouseMoved(MouseEvent e){}
 public void mousePressed(MouseEvent e){}
 public void mouseClicked(MouseEvent e){}
 public void adjustmentValueChanged(AdjustmentEvent arg0){}
 public void actionPerformed(ActionEvent e){}

}

新版本:我更正了我的代码,下面是工作版本

   import java.applet.Applet;
   import java.awt.BorderLayout;
   import java.awt.Color;
   import java.awt.Graphics;
   import java.awt.Graphics2D;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import java.awt.event.AdjustmentEvent;
   import java.awt.event.AdjustmentListener;
   import java.awt.event.MouseEvent;
   import java.awt.event.MouseListener;
   import java.awt.event.MouseMotionListener;
   import java.awt.image.BufferedImage;

   import javax.swing.JComponent;
   import javax.swing.JPanel;



   public class MarkPen extends Applet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener {

     private int x = 0;
     private int y = 0;
     private int prevx  = 0;
     private int prevy  = 0;

     private JPanel drawPanel = new JPanel();

     public void init()
     {
        setLayout(new BorderLayout());

        drawPanel.setBackground(Color.white);
        add(drawPanel, "Center");
        drawPanel.addMouseMotionListener(this);
        drawPanel.addMouseListener(this);
        drawPanel.add(new Comp());
        drawPanel.setDoubleBuffered(true);

     }

    public void setGraphicalDefaults(MouseEvent e)
     {
        x = e.getX();
        y = e.getY();
        prevx = e.getX();
        prevy = e.getY();
     }

    public class Comp extends JComponent{
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g  = drawPanel.getGraphics();
            g.setColor(Color.black);

            g.drawLine(prevx,prevy,x,y);
            prevx = x;
            prevy = y;
            g.dispose();
            repaint();
        }
     }  


     public void mouseDragged(MouseEvent e)
     {
         if (x != e.getX() || y != e.getY())
         {
             x = e.getX();
                         y = e.getY(); 
             }    
        }

        public void mousePressed(MouseEvent e){
             setGraphicalDefaults(e);
        }

        public void mouseReleased(MouseEvent e){}
         public void mouseEntered(MouseEvent e){}
         public void mouseExited(MouseEvent e){}
         public void mouseMoved(MouseEvent e){}
         public void mouseClicked(MouseEvent e){}
         public void adjustmentValueChanged(AdjustmentEvent arg0){}
         public void actionPerformed(ActionEvent e){}    

    }
4

1 回答 1

1

图形正在消失是有道理的,因为您调用getGraphics()的组件会产生不稳定的 Graphics 对象,该对象不会持续存在。不要那样做。而是绘制一个paint(...)覆盖。更好的是使用 Swing GUI 并在 JPanel 的paintComponent(...)方法中绘制或在paintComponent(...)方法中显示的 BufferedImage 中绘制。

于 2013-03-21T16:47:15.567 回答