这个画板闪屏闪的厉害,我试过添加双缓冲程序,不过 依旧闪屏,也问过老师,老师让我先把程序运行后Console下的报错解决掉。。我无语啊。。本来就是初学的 我怎么知道这个报错怎么解决所以在这里求各位大哥大姐帮帮忙啦。下面是程序

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;import java.util.*;public class test1 extends Applet {
    private boolean b = false,a = false ;
    Color COLOR;
    Checkbox tc;
    Choice size1 = new Choice();
    BasicStroke cno = new BasicStroke(1, BasicStroke.CAP_SQUARE,
    BasicStroke.JOIN_ROUND);
    private int style = 0, measure = 1;
    private int X1 = 0, X2 = 0, Y1 = 0, Y2 = 0;

    private BorderLayout Bl = new  BorderLayout();
    private Vector<point> v = new Vector<point>();
    private Stack<point> o = new Stack<point>();
    Button btn = new Button("笔"), btn0 = new Button("直线"), btn1 = new Button(
    "圆"), btn2 = new Button("矩形"), btn3 = new Button("多边形"),
    btn4 = new Button("返回上一步"), btn5 = new Button("清屏"),
    btn6 = new Button("橡皮"), btn7 = new Button("颜色"),
    btn8 = new Button("撤销"); public class point {
    int x = 0;
    int y = 0;
    int type;
    int size;
    Color color;
    boolean A;
    } public void init() {
    this.setLayout(Bl);
    this.setSize(800,400);
    JPanel jp=new JPanel();
    jp.setBackground(Color.green);
    jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
    add(jp,BorderLayout.WEST);
    tc = new Checkbox("图形是否填充?");
    size1.add("1");
    size1.add("3");
    size1.add("5");
    jp.add(tc);
    jp.add(btn);
    jp.add(btn0);
    jp.add(btn1);
    jp.add(btn2);
    jp.add(btn4);
    jp.add(btn5);
    jp.add(btn8);
    jp.add(btn6);
    jp.add(btn7);
    jp.add(size1);
    tc.addItemListener(new BtnActionAdapter());
    this.setSize(new Dimension(400, 400));
    size1.addItemListener(new BtnActionAdapter());
    btn.addActionListener(new BtnActionAdapter());
    btn0.addActionListener(new BtnActionAdapter());
    btn1.addActionListener(new BtnActionAdapter());
    btn2.addActionListener(new BtnActionAdapter());
    btn3.addActionListener(new BtnActionAdapter());
    btn4.addActionListener(new BtnActionAdapter());
    btn5.addActionListener(new BtnActionAdapter());
    btn6.addActionListener(new BtnActionAdapter());
    btn7.addActionListener(new BtnActionAdapter());
    btn8.addActionListener(new BtnActionAdapter());
    this.addMouseListener(new MouseListener());
    this.addMouseMotionListener(new MouseMotionListener());
    //this.setVisible(true);
    }

    public void hua(int x1, int y1, int x2, int y2, int od, Graphics g) {
    if (od == 0) {
      
    if (X1 <= X2 && Y1 <= Y2)
    g.drawOval(X1, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));// od=0圆,od=1实心圆,od=2矩形,od=3实心矩形
    else if (X1 <= X2 && Y1 >= Y2)
    g.drawOval(X1, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else if (X1 >= X2 && Y1 <= Y2)
    g.drawOval(X2, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else
    g.drawOval(X2, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    } else if (od == 1) {
    if (X1 <= X2 && Y1 <= Y2)
    g.fillOval(X1, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else if (X1 <= X2 && Y1 >= Y2)
    g.fillOval(X1, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else if (X1 >= X2 && Y1 <= Y2)
    g.fillOval(X2, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else
    g.fillOval(X2, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    } else if (od == 2) {
    if (X1 <= X2 && Y1 <= Y2)
    g.drawRect(X1, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else if (X1 <= X2 && Y1 >= Y2)
    g.drawRect(X1, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else if (X1 >= X2 && Y1 <= Y2)
    g.drawRect(X2, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else
    g.drawRect(X2, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    } else if (od == 3) {
    if (X1 <= X2 && Y1 <= Y2)
    g.fillRect(X1, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else if (X1 <= X2 && Y1 >= Y2)
    g.fillRect(X1, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else if (X1 >= X2 && Y1 <= Y2)
    g.fillRect(X2, Y1, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    else
    g.fillRect(X2, Y2, Math.abs(X2 - X1), Math.abs(Y1 - Y2));
    }
    } public void paint(Graphics g)  {
    Graphics2D g2d = (Graphics2D) g;
    //System.out.println("here");
    if (b) {
    Color ad = new Color(0, 0, 0);
    COLOR = JColorChooser.showDialog(this, "请选择您的颜色", ad);
    b = false;
    }
    int n = v.size();
    for (int i = 0; i < n; i++) { if (v.elementAt(i).size == 3)
    cno = new BasicStroke(3, BasicStroke.CAP_SQUARE,
    BasicStroke.JOIN_ROUND);
    else if (v.elementAt(i).size == 5)
    cno = new BasicStroke(5, BasicStroke.CAP_SQUARE,
    BasicStroke.JOIN_ROUND);
    else
    cno = new BasicStroke(1, BasicStroke.CAP_SQUARE,
    BasicStroke.JOIN_ROUND);
    g2d.setStroke(cno);
    g2d.setColor(v.elementAt(i).color); if (v.elementAt(i).type == 1) {
    X1 = v.elementAt(i).x;
    Y1 = v.elementAt(i).y;
    i++;
    while (v.elementAt(i).type != -1) {
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    g.drawLine(X1, Y1, X2, Y2);
    v.removeElementAt(i);
    }
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    g.drawLine(X1, Y1, X2, Y2);
    } else if (v.elementAt(i).type == 2) {
    X1 = v.elementAt(i).x;
    Y1 = v.elementAt(i).y;
    i++;
    while (v.elementAt(i).type != -1) {
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    if (!v.elementAt(i).A) {
    hua(X1, Y1, X2, Y2, 0, g);
    v.removeElementAt(i);
    } else {
    hua(X1, Y1, X2, Y2, 1, g);
    v.removeElementAt(i);
    }
    }
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    if (!v.elementAt(i).A)
    hua(X1, Y1, X2, Y2, 0, g);
    else
    hua(X1, Y1, X2, Y2, 1, g);
    } else if (v.elementAt(i).type == 3) {
    X1 = v.elementAt(i).x;
    Y1 = v.elementAt(i).y;
    i++;
    while (v.elementAt(i).type != -1) {
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    if (!v.elementAt(i).A) {
    hua(X1, Y1, X2, Y2, 2, g);
    v.removeElementAt(i);
    } else {
    hua(X1, Y1, X2, Y2, 3, g);
    v.removeElementAt(i);
    }
    }
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    if (!v.elementAt(i).A)
    hua(X1, Y1, X2, Y2, 2, g);
    else
    hua(X1, Y1, X2, Y2, 3, g);
    } else if (v.elementAt(i).type == 0) {
    X1 = v.elementAt(i).x;
    Y1 = v.elementAt(i).y;
    while (v.elementAt(i).type != -1) {
    i++;
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    g.drawLine(X1, Y1, X2, Y2);
    X1 = X2;
    Y1 = Y2;
    }
    } else if (v.elementAt(i).type == 4) {
    Color aa = new Color(204, 232, 207);
    X1 = v.elementAt(i).x;
    Y1 = v.elementAt(i).y;
    while (v.elementAt(i).type != -1) {
    i++;
    X2 = v.elementAt(i).x;
    Y2 = v.elementAt(i).y;
    g2d.setColor(aa);
    g.drawLine(X1, Y1, X2, Y2);
    X1 = X2;
    Y1 = Y2;
    }
    } } } class BtnActionAdapter implements ActionListener, ItemListener { public void itemStateChanged(ItemEvent e) {
    Choice temp;
    Checkbox temp1;
    if (e.getItemSelectable() instanceof Choice) {
    temp = (Choice) (e.getItemSelectable());
    measure = Integer.parseInt(temp.getSelectedItem());
    } else if (e.getItemSelectable() instanceof Checkbox) {
    temp1 = (Checkbox) (e.getItemSelectable());
    a = temp1.getState();
    } } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == btn) {
    style = 0;
    } else if (e.getSource() == btn0) {
    style = 1;
    } else if (e.getSource() == btn1) {
    style = 2;
    } else if (e.getSource() == btn2) {
    style = 3;
    } else if (e.getSource() == btn3) {
    style = 5;
    } else if (e.getSource() == btn7) {
    b = true;
    repaint();
    } else if (e.getSource() == btn5) {
    v.removeAllElements();
    repaint();
    } else if (e.getSource() == btn6) {
    style = 4;
    } else if (e.getSource() == btn4) {
    int n = v.size();
    o.push(v.elementAt(n - 1));
    v.remove(n - 1);
    n = n - 1;
    if (n > 0) {
    while (v.elementAt(n - 1).type != -1) {
    o.push(v.elementAt(n - 1));
    v.removeElementAt(n - 1);
    n--;
    }
    repaint();
    } else {
    o.push(v.elementAt(1));
    o.push(v.elementAt(0));
    v.removeAllElements();
    repaint();
    } } else if (e.getSource() == btn8) {
    while (o.peek().type != -1) {
    v.add(o.pop());
    }
    v.add(o.pop());
    repaint();
    } }
    } class MouseListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
    point p = new point();
    p.x = e.getX();
    p.y = e.getY();
    p.type = style;
    p.size = measure;
    p.color = COLOR;
    p.A = a;
    v.add(p); } public void mouseReleased(MouseEvent e) {
    point p = new point();
    p.x = e.getX();
    p.y = e.getY();
    p.type = -1;
    p.size = measure;
    p.color = COLOR;
    p.A = a;
    v.add(p); }
    } class MouseMotionListener extends MouseMotionAdapter {
    public void mouseDragged(MouseEvent e) {
    point p = new point();
    p.x = e.getX();
    p.y = e.getY();
    p.type = style;
    p.size = measure;
    p.color = COLOR;
    p.A = a;
    v.add(p);
       repaint();
    }
    }
    }
      

  2.   

    BufferedImage image = new BufferedImage(WIDTH ,HEIGHT,BufferedImage.IMAGE_TYPE);
    Graphics g = image.getGraphics();组件放在JFrame中
      

  3.   

    你可以先画在一个后台的image里
    然后每次更新把image更新在前台画布
      

  4.   

    嗯 我现在用的是Swing组件 。Swing组件是自带双缓冲的?可是不知道为什么 依旧还是闪屏、
      

  5.   

    你好~我在我自己机器上做如下修改:
    public class test1 extends Applet
    改为
    public class test1 extends JApplet
    ,闪屏问题就没有了。
    原因是:~~我也不知道,等解释