java 双缓冲 绘制图片 看不出效果 请教 高手 指点 并能为在下 讲解 详细
publicclassBextendsApplet{intgap=3;intmx,my;booleanflicker=true;Imagebuffer=null;intw...
public class B extends Applet {
int gap = 3;
int mx, my;
boolean flicker = true;
Image buffer = null;
int w, h;
public void init() {
Dimension d = getSize();
w = d.width;
h = d.height;
buffer = createImage(w, h);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
mx = me.getX();
my = me.getY();
flicker = false;
repaint();
}
public void mouseMoved(MouseEvent me) {
mx = me.getX();
my = me.getY();
flicker = true;
repaint();
}
});
}
public void paint(Graphics g) {
Graphics screengc = null;
if (!flicker) {
screengc = g;
g = buffer.getGraphics();
}
g.setColor(Color.blue);
g.fillRect(0, 0, w, h);
g.setColor(Color.red);
for (int i=0; i<w; i+=gap)
g.drawLine(i, 0, w-i, h);
for (int i=0; i<h; i+=gap)
g.drawLine(0, i, w, h-i);
g.setColor(Color.black);
g.drawString("Press mouse button to double buffer", 10, h/2);
g.setColor(Color.yellow);
g.fillOval(mx - gap, my - gap, gap*2+1, gap*2+1);
if (!flicker) {
screengc.drawImage(buffer, 0, 0, null);
}
}
public void update(Graphics g) {
paint(g);
}
} 展开
int gap = 3;
int mx, my;
boolean flicker = true;
Image buffer = null;
int w, h;
public void init() {
Dimension d = getSize();
w = d.width;
h = d.height;
buffer = createImage(w, h);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
mx = me.getX();
my = me.getY();
flicker = false;
repaint();
}
public void mouseMoved(MouseEvent me) {
mx = me.getX();
my = me.getY();
flicker = true;
repaint();
}
});
}
public void paint(Graphics g) {
Graphics screengc = null;
if (!flicker) {
screengc = g;
g = buffer.getGraphics();
}
g.setColor(Color.blue);
g.fillRect(0, 0, w, h);
g.setColor(Color.red);
for (int i=0; i<w; i+=gap)
g.drawLine(i, 0, w-i, h);
for (int i=0; i<h; i+=gap)
g.drawLine(0, i, w, h-i);
g.setColor(Color.black);
g.drawString("Press mouse button to double buffer", 10, h/2);
g.setColor(Color.yellow);
g.fillOval(mx - gap, my - gap, gap*2+1, gap*2+1);
if (!flicker) {
screengc.drawImage(buffer, 0, 0, null);
}
}
public void update(Graphics g) {
paint(g);
}
} 展开
展开全部
BufferedImage imagePath=null;
try
{
imagePath=ImageIO.read(new File("你要加的图片"));
} catch (IOException e)
{
e.printStackTrace();
}
BufferedImage bi=new BufferedImage(885, 680, BufferedImage.TYPE_INT_BGR);\
Graphics g2=bi.createGraphics();
g2.drawImage(imagePath, 0, 30, 885, 650, this);
这样就实现了双缓冲
try
{
imagePath=ImageIO.read(new File("你要加的图片"));
} catch (IOException e)
{
e.printStackTrace();
}
BufferedImage bi=new BufferedImage(885, 680, BufferedImage.TYPE_INT_BGR);\
Graphics g2=bi.createGraphics();
g2.drawImage(imagePath, 0, 30, 885, 650, this);
这样就实现了双缓冲
追问
为什么前面是 new BufferedImage(885, 680, BufferedImage.TYPE_INT_BGR);
后面drawimage 变成 885, 650, this 这个680 怎么变成 650 了 跟坐标有关系吗
追答
这个是做的时候的图片尺寸 你也可以设置成一样
参考资料: java api
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询