java闪烁问题,用了双缓冲,可是仍然闪烁。求教如何改?
代码如下:importjava.awt.*;importjavax.swing.*;publicclassScGameextendsJFrame{intoldx,x,ol...
代码如下:
import java.awt.*;
import javax.swing.*;
public class ScGame extends JFrame{
int oldx,x,oldy,y;
Image buffer=null;
ScGame(){
setSize(800,600);
setVisible(true);
oldx=x=100;oldy=y=100;
}
public void paint(Graphics g)
{
g.drawImage(buffer,getInsets().left,getInsets().top,this);
}
void motion()
{ oldx=x;oldy=y;
x+=5;y+=5;
Color c1=new Color(255,0,0);
Graphics temp;
if(buffer==null)
buffer=createImage(800,600);
temp=buffer.getGraphics();
temp.setColor(c1);
temp.clearRect(oldx,oldy,100,100);
temp.fill3DRect(x, y, 100, 100,false);
temp.dispose();
}
public static void main(String args[])
{
ScGame scg=new ScGame();
try{
for(int i=0;i<1000;i++){
Thread.sleep(50);
scg.motion();
scg.repaint();
}
}catch(Exception e){
e.printStackTrace();}
}
} 展开
import java.awt.*;
import javax.swing.*;
public class ScGame extends JFrame{
int oldx,x,oldy,y;
Image buffer=null;
ScGame(){
setSize(800,600);
setVisible(true);
oldx=x=100;oldy=y=100;
}
public void paint(Graphics g)
{
g.drawImage(buffer,getInsets().left,getInsets().top,this);
}
void motion()
{ oldx=x;oldy=y;
x+=5;y+=5;
Color c1=new Color(255,0,0);
Graphics temp;
if(buffer==null)
buffer=createImage(800,600);
temp=buffer.getGraphics();
temp.setColor(c1);
temp.clearRect(oldx,oldy,100,100);
temp.fill3DRect(x, y, 100, 100,false);
temp.dispose();
}
public static void main(String args[])
{
ScGame scg=new ScGame();
try{
for(int i=0;i<1000;i++){
Thread.sleep(50);
scg.motion();
scg.repaint();
}
}catch(Exception e){
e.printStackTrace();}
}
} 展开
2个回答
展开全部
//改这里Thread.sleep(10);和x+=1;y+=1;
import java.awt.*;
import javax.swing.*;
public class ScGame extends JFrame{
int oldx,x,oldy,y;
Image buffer=null;
ScGame(){
setSize(800,600);
setVisible(true);
oldx=x=100;oldy=y=100;
}
public void paint(Graphics g)
{
g.drawImage(buffer,getInsets().left,getInsets().top,this);
}
void motion()
{ oldx=x;oldy=y;
x+=1;y+=1;
Color c1=new Color(255,0,0);
Graphics temp;
if(buffer==null)
buffer=createImage(800,600);
temp=buffer.getGraphics();
temp.setColor(c1);
temp.clearRect(oldx,oldy,100,100);
temp.fill3DRect(x, y, 100, 100,false);
temp.dispose();
}
public static void main(String args[])
{
ScGame scg=new ScGame();
try{
for(int i=0;i<1000;i++){
Thread.sleep(10);
scg.motion();
scg.repaint();
}
}catch(Exception e){
e.printStackTrace();}
}
}
import java.awt.*;
import javax.swing.*;
public class ScGame extends JFrame{
int oldx,x,oldy,y;
Image buffer=null;
ScGame(){
setSize(800,600);
setVisible(true);
oldx=x=100;oldy=y=100;
}
public void paint(Graphics g)
{
g.drawImage(buffer,getInsets().left,getInsets().top,this);
}
void motion()
{ oldx=x;oldy=y;
x+=1;y+=1;
Color c1=new Color(255,0,0);
Graphics temp;
if(buffer==null)
buffer=createImage(800,600);
temp=buffer.getGraphics();
temp.setColor(c1);
temp.clearRect(oldx,oldy,100,100);
temp.fill3DRect(x, y, 100, 100,false);
temp.dispose();
}
public static void main(String args[])
{
ScGame scg=new ScGame();
try{
for(int i=0;i<1000;i++){
Thread.sleep(10);
scg.motion();
scg.repaint();
}
}catch(Exception e){
e.printStackTrace();}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-10-17
展开全部
步长减少一些,效果会好很多
~
~
~
~~~~~~~~
~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
~
~
~
~~~~~~~~
~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询