java怎么控制图片在窗体中移动?
展开全部
写一个线程,在run方法中控制图片x,y的坐标位置,不能超过窗体的控制就是当你的x和Y的坐标不能大于窗体的最大值和最小值。
你
import java.awt.*;
import java.util.Date;
import javax.swing.*;
import sun.security.krb5.internal.rcache.ReplayCache;
public class CanvasPanel extends JPanel implements Runnable {
private Image back;
private Image cloud;
int time;
private Thread thread;
private boolean play = true;
int width = 458;
int height = 357;
int x = width;
public CanvasPanel() {
super();
back = new ImageIcon(getClass().getResource("back.jpg")).getImage();
cloud = new ImageIcon(getClass().getResource("cloud.png")).getImage();
time = 100;
play = true;
if (thread == null || !thread.isAlive())
thread = new Thread(this);
thread.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(back, 0, 0, width, height, this);
g.drawImage(cloud, x, 10, this);
x -= 2;
if (x <= -cloud.getWidth(this))
x = width;
}
public void run() {
while (play) {
try {
Thread.sleep(time);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageRun extends JFrame {
private JPanel panel;
public static void main(String args[]) {
try {
ImageRun frame = new ImageRun();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public ImageRun() {
super();
setBounds(100, 100, 458, 357);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getPanel(), BorderLayout.CENTER);
//
}
protected JPanel getPanel() {
if (panel == null) {
panel = new CanvasPanel();
}
return panel;
}
}
back.jpg和cloud.jpg是两张图片
你
import java.awt.*;
import java.util.Date;
import javax.swing.*;
import sun.security.krb5.internal.rcache.ReplayCache;
public class CanvasPanel extends JPanel implements Runnable {
private Image back;
private Image cloud;
int time;
private Thread thread;
private boolean play = true;
int width = 458;
int height = 357;
int x = width;
public CanvasPanel() {
super();
back = new ImageIcon(getClass().getResource("back.jpg")).getImage();
cloud = new ImageIcon(getClass().getResource("cloud.png")).getImage();
time = 100;
play = true;
if (thread == null || !thread.isAlive())
thread = new Thread(this);
thread.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(back, 0, 0, width, height, this);
g.drawImage(cloud, x, 10, this);
x -= 2;
if (x <= -cloud.getWidth(this))
x = width;
}
public void run() {
while (play) {
try {
Thread.sleep(time);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageRun extends JFrame {
private JPanel panel;
public static void main(String args[]) {
try {
ImageRun frame = new ImageRun();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public ImageRun() {
super();
setBounds(100, 100, 458, 357);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getPanel(), BorderLayout.CENTER);
//
}
protected JPanel getPanel() {
if (panel == null) {
panel = new CanvasPanel();
}
return panel;
}
}
back.jpg和cloud.jpg是两张图片
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询