import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test4_3 extends JFrame{ PaoPao mp2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Test4_3 test4_3=new Test4_3();
}
public Test4_3()
{
mp2=new PaoPao();
this.add(mp2);
this.setSize(800,600);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Thread t=new Thread(mp2);
t.start();
}}class PaoPao extends JPanel implements Runnable
{
int x=100;
int y=100;
int width=800;
int height=600;
//定义泡泡的方向
int direction=1;
//定义泡泡速度
int speed=5;
//逆时针走
private Image iBuffer;
private Graphics gBuffer; public void running()
{
switch (direction)
{
//右下方向 ↘
case 1:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x+=speed;
y+=speed;
if(y>=height)
{
this.direction=2;
}else if(x>=width)
{
this.direction=-2;
}
break;
//右上 ↗
case 2:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x+=speed;
y-=speed;
if(x>=width)
{
direction=-1;
}else if(y<=0)
{
direction=1;
}
break;
//左上 ↖
case -1:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x-=speed;
y-=speed;
if(y<=0)
{
direction=-2;
}else if(x<=0)
{
direction=2;
}
break;
//坐下 ↙
case -2:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x-=speed;
y+=speed;
if(x<=0)
{
direction=1;
}else if(y>=height)
{
direction=-1;
}
break;
}
this.repaint();
}
public void paint(Graphics scr) {
iBuffer=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/paopao.png"));
scr.drawImage(iBuffer,x,y,134,134,this);
}
public void update(Graphics scr)
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,this.getSize().width,this.getSize().height);
paint(gBuffer);
scr.drawImage(iBuffer,0,0,this);
} public void run() {
// TODO Auto-generated method stub
while(true)
{
this.running();
}
}
}
最后都粘一块了,但是如果把那张图片换成自己画的,就不会粘一块,这是为什么?具体怎么解决??
import java.awt.event.*;
import javax.swing.*;public class Test4_3 extends JFrame{ PaoPao mp2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Test4_3 test4_3=new Test4_3();
}
public Test4_3()
{
mp2=new PaoPao();
this.add(mp2);
this.setSize(800,600);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Thread t=new Thread(mp2);
t.start();
}}class PaoPao extends JPanel implements Runnable
{
int x=100;
int y=100;
int width=800;
int height=600;
//定义泡泡的方向
int direction=1;
//定义泡泡速度
int speed=5;
//逆时针走
private Image iBuffer;
private Graphics gBuffer; public void running()
{
switch (direction)
{
//右下方向 ↘
case 1:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x+=speed;
y+=speed;
if(y>=height)
{
this.direction=2;
}else if(x>=width)
{
this.direction=-2;
}
break;
//右上 ↗
case 2:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x+=speed;
y-=speed;
if(x>=width)
{
direction=-1;
}else if(y<=0)
{
direction=1;
}
break;
//左上 ↖
case -1:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x-=speed;
y-=speed;
if(y<=0)
{
direction=-2;
}else if(x<=0)
{
direction=2;
}
break;
//坐下 ↙
case -2:
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x-=speed;
y+=speed;
if(x<=0)
{
direction=1;
}else if(y>=height)
{
direction=-1;
}
break;
}
this.repaint();
}
public void paint(Graphics scr) {
iBuffer=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/paopao.png"));
scr.drawImage(iBuffer,x,y,134,134,this);
}
public void update(Graphics scr)
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,this.getSize().width,this.getSize().height);
paint(gBuffer);
scr.drawImage(iBuffer,0,0,this);
} public void run() {
// TODO Auto-generated method stub
while(true)
{
this.running();
}
}
}
最后都粘一块了,但是如果把那张图片换成自己画的,就不会粘一块,这是为什么?具体怎么解决??
解决方案 »
- JAVA里的泛型
- java arraylist.toArray()
- List<Object> 对象怎么转换为List<RefDTO>?
- synchronized加在方法前,锁作用范围?
- jtable中如果先选中了第一个单元格,然后再选中同一行的最后一个单元格,为什么会选中一行
- 求文件中特定字符串的替换的程序(语言限用JAVA)
- IBM要求SUN对Java开源,究竟是什么意思呢?开些什么“源”呢?
- 浮点型如何转成整形??谢谢
- 怎么现在不能使用搜索了??????
- 请问那里有MS SQLServer的JDBC驱动呢?
- android-ndk编译生成的so库 可以在java工程中调用么
- 在eclipse 中applet
scr.drawImage(iBuffer,x,y,134,134,this);
在网上查了很多方法,但没有得到根本的解决请大侠指教
super.paint(scr); //加上这个
//xxxx你的代码
}