绘图图形太慢了

时间:2012-11-16 01:54:05

标签: java swing graphics bufferedimage paintcomponent

所以,我有这个项目,你可以在其中绘制图像。我希望人们可以使用它,但是当我使用repaint()时起初它太慢了所以我使用了repaint(Rectangle r)工具。它更好,但仍然不是我想要的速度。 这是代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class DrawingPad extends JPanel implements MouseListener,MouseMotionListener,ListSelectionListener{
public Color[][] picture = new Color[601][601];
public Color selected;
public String action;
public Maker m;
private static final long serialVersionUID = 1L;
public DrawingPad(Maker m){
    this.m = m;
    this.setPreferredSize(new Dimension(600,600));
    this.setVisible(true);
    for (int x = 1;x<=600;x++){
        for (int y = 1; y<=600;y++){
            picture[x][y]=Color.WHITE;
        }
    }
}
public void addColor(int x, int y){
    try{
        picture[x][y]=selected;
        repaint(new Rectangle(x,y,x,y));
    }catch (Exception e){

    }
}
@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.clearRect(0, 0, 600, 600);
    for (int x = 1;x<=600;x++){
        for (int y = 1; y<=600;y++){
            g.setColor(picture[x][y]);
            g.drawLine(x, y, x, y);
        }
    }
}
@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseDragged(MouseEvent e) {
    for (int x = -1;x<=1;x++){
        for (int y = -1;y<=1;y++){
            this.addColor(e.getX()+x, e.getY()+y);
        }   
    }
}
@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void valueChanged(ListSelectionEvent e) {
    if (e.getSource()==m.seeit){
        selected = m.colors[m.seeit.getSelectedIndex()];
    }else{
        action=(String) m.actions.getSelectedValue();
    }

}
}

1 个答案:

答案 0 :(得分:5)

您可能希望查看不会更改为BufferedImage的绘图,然后将paintComponent方法中的BufferedImage显示为背景图像。

例如,请查看this linkthis one