Java Awt钢笔工具:图形消失了

时间:2013-03-21 16:34:23

标签: java graphics applet awt

我将尝试使用钢笔工具实现Java Applet。 当我启动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){}    

    }

1 个答案:

答案 0 :(得分:1)

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