用java线程怎么使图片滚动,最好是有代码,谢谢
3个回答
展开全部
给你一个参考,可以作为开发java游戏的基本框架
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class CarDemo extends JFrame implements Runnable{
private BufferStrategy bs;
private volatile boolean running;
private Thread thread;
private Car car;
protected void createAndShowGUI() {
Canvas canvas = new Canvas();
canvas.setSize( 600, 400 );
canvas.setBackground( Color.BLACK );
canvas.setIgnoreRepaint( true );
getContentPane().add( canvas );
setIgnoreRepaint( true );
pack();
setVisible( true );
canvas.createBufferStrategy( 2 );
bs = canvas.getBufferStrategy();
car = new Car(new Point(300, 200), new Point(2, 5), new Point(canvas.getWidth(), canvas.getHeight()));
thread = new Thread( this );
thread.start();
}
public void run() {
running = true;
while( running ) {
updateObject();
renderFrame();
try {
Thread.sleep(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void updateObject() {
car.update();
}
public void renderFrame() {
do {
do {
Graphics g = null;
try {
g = bs.getDrawGraphics();
g.clearRect( 0, 0, getWidth(), getHeight() );
render( g );
} finally {
if( g != null ) {
g.dispose();
}
}
} while( bs.contentsRestored() );
bs.show();
} while( bs.contentsLost() );
}
private void render( Graphics g ) {
car.render(g);
}
protected void onWindowClosing() {
try {
running = false;
thread.join();
} catch( InterruptedException e ) {
e.printStackTrace();
}
System.exit( 0 );
}
public static void main( String[] args ) {
final CarDemo app = new CarDemo();
app.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
app.onWindowClosing();
}
});
SwingUtilities.invokeLater( new Runnable() {
public void run() {
app.createAndShowGUI();
}
});
}
}
class Car {
Point p;
Point v;
Point border;
BufferedImage image;
public Car(Point p, Point v, Point b) {
this.p = p;
this.v = v;
this.border = b;
try {
image = ImageIO.read(new File("1.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update() {
if(p.x <= 0) v.x = -v.x;
if(p.x + image.getWidth() > border.x) v.x = -v.x;
if(p.y <= 0) v.y = -v.y;
if(p.y + image.getHeight() > border.y) v.y = -v.y;
p.x += v.x;
p.y += v.y;
}
public void render(Graphics g) {
g.drawImage(image, p.x, p.y, null);
}
}
展开全部
分析说明:
java的swing是线程非安全的. 所以一般不会使用非UI线程去控制UI线程
而是使用线程安全的 Timer类来控制
如果想比较流畅, 那么可以调解Timer的参数 毫秒数, 和每次移动的大小 来实现
参考代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class FrameDemo extends JFrame {
SunPanel sp;
Timer timer;
int w = 600;
int h = 400;
int px = w - 20;
int py = h - 20;
public FrameDemo() {
setTitle("太阳窗口");
setSize(w, h);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
sp = new SunPanel();
sp.setBounds(px, py, 58, 58);
add(sp);
timer = new Timer(10, new ActionListener() {//每10毫秒改变一次太阳的位置, 可以自己加快或者放慢,修改毫秒数就可以
public void actionPerformed(ActionEvent e) {
sp.setLocation(px -= 2, py -= 1);// 可以修改成px-=3 ,py-=2等方式来控制每次移动的快慢
if (px <= 0 || py <= 0) {
timer.stop();
}
}
});
timer.start();//开始
}
public static void main(String[] args) {
new FrameDemo().setVisible(true);
}
class SunPanel extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.RED);
g.drawOval(2, 2, 50, 50);
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-01-06
展开全部
不断改变图片位置
追问
可是会有明显的连接线
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询