绘图图形太慢了 [英] Drawing Graphics is too slow

查看:119
本文介绍了绘图图形太慢了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


public class DrawingPad扩展JPanel实现MouseListener,MouseMotionListener,ListSelectionListener {
public Color [] [] picture = new Color [601] [601];
选择了公共颜色;
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); (int y = 1; y< = 600; y ++){
picture [x] [y] =(
for(int x = 1; x <= 600; x ++)白颜色;


$ b $ 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); (int y = 1; y< = 600; y ++){
g.setColor(picture [x])的
(int x = 1; x <= 600; x ++) [Y]);
g.drawLine(x,y,x,y);
}
}
}
@Override
public void mouseClicked(MouseEvent arg0){
// TODO自动生成的方法存根

}
@Override
public void mouseEntered(MouseEvent arg0){
// TODO自动生成的方法存根

}
@Override
public void mouseExited(MouseEvent arg0){
// TODO自动生成的方法存根

$ b @Override
public void mousePressed(MouseEvent arg0){
// TODO自动生成方法存根

$ b @Override
public void mouseReleased(MouseEvent arg0){
// TODO自动生成的方法stub

$ b @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自动生成的方法存根


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

}
}


解决方案

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



例如,请查看此链接以及这一个

So, I have this project, and you can draw images in it. I wanted people to be able to draw on it, but at first it was too slow when I was using repaint() So i used the repaint(Rectangle r) tool. It's better, but still not the speed i was looking for. Here is the code:

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();
    }

}
}

解决方案

You might want to look into drawing that which will not be changed into a BufferedImage, and then displaying that BufferedImage in the paintComponent method as a background image.

For example, please have a look at this link and also this one.

这篇关于绘图图形太慢了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆