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);
}
}
}画面无任何显示,如何解决?
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);
}
}
}画面无任何显示,如何解决?
关于双缓冲,你可以参考一下这篇文章--“AWT和Swing中的绘画”(http://blog.csdn.net/UnAgain/archive/2006/05/13/727474.aspx)
BufferStrategy staategy=frame.getBufferStrategy();
Graphics g=stategy.getDrawGraphics();
draw(g);
g.dispose();
startegy.show;
大致是这样个过程,你再看看API上怎么说的。(需要JDK1.4 UP)
g.drawImage(buffer, 0, 0, null);
这里的g是哪个?如果实现双缓存的话,这里应该调用Pane的g,而你这里似乎是buffer的呀
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);
}
}
}
还请诸位多指教!