如何在Swing中使用双缓冲!??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JFrame {
int width, height, radius;
int X, Y, moveX, moveY;
Graphics g;
Image buffer;
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
//Construct the frame
public Draw() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
width = getWidth();
height = getHeight();
X = 30;
Y = 50;
moveX = 1;
moveY = 1;
radius = 20;
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
}
protected void paintComponent(Graphics g) {
buffer = createImage(width, height);
g = buffer.getGraphics();
if (X >= (width - radius)) {
X = width - radius;
moveX = -moveX;
}
if (X <= 0) {
X = 0;
moveX = -moveX;
}
if (Y >= (height - radius)) {
Y = height - radius;
moveY = -moveY;
}
if (Y <= 0) {
Y = 0;
moveY = -moveY;
}
X += moveX;
Y += moveY;
g.setColor(Color.blue);
g.drawOval(X, Y, radius, radius);
g.drawImage(buffer, 0, 0, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
画面无任何显示,如何解决?
问题点数:20、回复次数:6Top
1 楼wuyan19831013(骨灰级帅哥兼职卖TT)回复于 2006-08-02 08:37:49 得分 0
?没人指点么?Top
2 楼andrew80(竹林)回复于 2006-08-02 10:04:49 得分 0
我是隔壁贴的,支持一下
UnAgain() ( ) 信誉:98 2006-7-29 22:00:59 得分: 0
关于双缓冲,你可以参考一下这篇文章--“AWT和Swing中的绘画”(http://blog.csdn.net/UnAgain/archive/2006/05/13/727474.aspx)Top
3 楼whycloud(云木)回复于 2006-08-02 10:24:36 得分 0
swing里有个属性,并不用自己实现双缓冲,你查一下API我记不清楚了。而且设置后,系统会根据实际效率自动选择双缓存还是翻页模式
frame.createBufferStrategy(2); //生成缓存的数量,有可能使用翻页技术所以至少2
BufferStrategy staategy=frame.getBufferStrategy();
Graphics g=stategy.getDrawGraphics();
draw(g);
g.dispose();
startegy.show;
大致是这样个过程,你再看看API上怎么说的。(需要JDK1.4 UP)Top
4 楼whycloud(云木)回复于 2006-08-02 10:27:12 得分 0
而且你的代码有问题
g.drawImage(buffer, 0, 0, null);
这里的g是哪个?如果实现双缓存的话,这里应该调用Pane的g,而你这里似乎是buffer的呀Top
5 楼gtlang78()回复于 2006-08-02 10:56:56 得分 0
g = buffer.getGraphics(); 这句把原来的g参数给覆盖了, 换个名字。Top
6 楼wuyan19831013(骨灰级帅哥兼职卖TT)回复于 2006-08-02 11:23:39 得分 0
多谢诸位解答
不过,我依然不甚明了
我想在JFrame里画图,不知是否可行?我将代码修改如下,依然无任何显示```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class draw extends JFrame {
int width, height, radius;
int X, Y, moveX, moveY;
Graphics g,bufferg;
Image buffer;
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
//Construct the frame
public draw() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
width = getWidth();
height = getHeight();
X = 30;
Y = 50;
moveX = 1;
moveY = 1;
radius = 20;
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("-----Draw Test-----");
g = this.getGraphics();
}
protected void paintComponent(Graphics g) {
g.drawOval(100,100,20,20);
buffer = createImage(width, height);
bufferg = buffer.getGraphics();
if (X >= (width - radius)) {
X = width - radius;
moveX = -moveX;
}
if (X <= 0) {
X = 0;
moveX = -moveX;
}
if (Y >= (height - radius)) {
Y = height - radius;
moveY = -moveY;
}
if (Y <= 0) {
Y = 0;
moveY = -moveY;
}
X += moveX;
Y += moveY;
bufferg.setColor(Color.blue);
bufferg.drawOval(X, Y, radius, radius);
g.drawImage(buffer, 0, 0, this);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
还请诸位多指教!
Top




