急)帮忙改下代码。(j2se)。谢谢。 在原基上改,让它不闪屏,用双缓冲,
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;public class Repain extends JFrame{
private static final long serialVersionUID = 1L;
int x = 10,y = 10;
Repain()
{
this.getContentPane().setBackground(Color.RED);
this.setBounds(300, 300, 600, 480);
this.setResizable(false);
new Thread (new PainThread()).start();
}
public void paint(Graphics g)
{
super.paint(g);
Color c = g .getColor();
g .fillOval(x, y, 40, 40);
g .setColor(c);
}
public class PainThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(10);
x = x + 1;
y += 1;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new Repain().setVisible(true);
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;public class Repain extends JFrame{
private static final long serialVersionUID = 1L;
int x = 10,y = 10;
Repain()
{
this.getContentPane().setBackground(Color.RED);
this.setBounds(300, 300, 600, 480);
this.setResizable(false);
new Thread (new PainThread()).start();
}
public void paint(Graphics g)
{
super.paint(g);
Color c = g .getColor();
g .fillOval(x, y, 40, 40);
g .setColor(c);
}
public class PainThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(10);
x = x + 1;
y += 1;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new Repain().setVisible(true);
}
}
解决方案 »
- Java项目开发全程实录,第一章的进销存程序,运行之后登陆出现问题,是什么原因?
- 求java中文语音识别例子
- 如何用面向对象方法建立一个现实问题的分析模型
- SCJP 6.0認證教戰手冊考題.147--printf
- 一个突然想到的关于字符串输出方式的问题
- 我在学接口中出现的问题帮我看一下下面的错误怎么改正啊,谢谢了。
- 谁能帮我分析一下这段代码
- 【求 2 个 正则表达式】
- 请问某个数据库(如oracle)的jdbc驱动程序是不是在所有平台下通用?
- 下面的代码用eclipse compile为什么不行,我是完全从一个视频里抄过来的,视频里用的是ultra editor写的。是不是我一定要把class分开
- 请教一个问题?为什么在MyEclipse里不能运行?
- JAVA能找到方法,但不执行
我给我自已顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶..........
**/import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JFrame;
import javax.swing.JPanel;public class Repain extends JPanel{//最好用JPanel来画图,
private static final long serialVersionUID = 1L;
int x = 10,y = 10;
JFrame jf = new JFrame();
public Repain()
{
this.setBackground(Color.RED);
jf.setLayout(new BorderLayout());
jf.setBounds(300, 300, 600, 480);
jf.add(this,BorderLayout.CENTER);
jf.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
jf.setResizable(false);
jf.setVisible(true);
new Thread (new PainThread()).start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);//这不可少。
g .fillOval(x, y, 40, 40);
}
public class PainThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(10);
x = x + 1;
y += 1;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new Repain();
}
}
//最后再顶下。呜呜呜呜呜呜.............
知道,兄弟,这个可根据实际应用改变run()方法中的while( )中的循环条件来控制吧,不过呢,也谢谢哥哥指出问题。
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class DoubleBuffer extends Frame{
private static final long serialVersionUID = 1L;
private int x = 0,y = 0;
Image offScreenImage = null;
boolean isRun = true;
public DoubleBuffer()
{
this.setBounds(200, 200, 600, 400);
this.setResizable(false);
this.setBackground(Color.RED);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
this.setVisible(true);
new Thread(new PainThread()).start();
}
public void paint(Graphics g)
{
g.fillOval(x, y, 40, 40);
}
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(600, 400);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.RED);
gOffScreen.fillRect(0, 0, 600, 400);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, x, y, null);
}
public static void main(String[] args) {
new DoubleBuffer();
} public class PainThread implements Runnable {
public void run() {
while (isRun) {
repaint();
try {
Thread.sleep(10);
x = x + 1;
y += 1;
if(x > 600 || y > 400)
isRun = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
但是我只会会Frame进行,能不能给我说下,在JFrame中如何实现,如上那样实现。谢谢。