我想在java小程序中创建移动卡车的连续动画。在这种情况下,卡车一消失就应该从另一侧重新出现。 我尝试使用一辆卡车,但只有在从一侧完全消失后才出现在另一侧。因此,我使用了两个。
以下是我的代码:
public class prc1 extends Applet implements Runnable{
Thread t;
int x=0;
int y=0;
int i;
public void start(){
t = new Thread(this);
t.start();
}
public void paint(Graphics g){
g.setColor(Color.gray);
g.fillRect(x,10,70,45);
g.setColor(Color.yellow);
g.fillRoundRect(50+x,15,35,35,10,10);
g.setColor(Color.black);
g.fillArc(5+x,50,10,10,0,360);
g.fillArc(55+x,50,10,10,0,360);
g.setColor(Color.gray);
g.fillRect(y,10,70,45);
g.setColor(Color.yellow);
g.fillRoundRect(50+y,15,35,35,10,10);
g.setColor(Color.black);
g.fillArc(5+y,50,10,10,0,360);
g.fillArc(55+y,50,10,10,0,360);
}
public void run(){
try{
y=-90;
x=0;
for(; ;){
Thread.sleep(100);
repaint();
run();
x=x+5;
if(x==400){
for(; ;){
y=y+5;
Thread.sleep(100);
repaint();
run();
x=x-5;
if(x==0){
y=-90;
break;
}
}
}
}
}catch(InterruptedException e){ }
}
}
现在的问题是卡车根本不动。
答案 0 :(得分:1)
所以基本的想法是,你需要确定卡车什么时候开始离开可视区域,然后计算“溢出”的数量,这样你就可以在第二次在新的位置上画卡车了。溢出。
我已经破坏了你的代码,所以Truck
是它自己的类,这使得它更容易处理并允许对绘画过程进行更多的控制。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Truck truck;
public TestPane() {
truck = new Truck();
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
truck.update(getSize());
repaint();
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
truck.paint(g2d);
g2d.dispose();
}
}
public class Truck {
private int x, y;
// I had to calculate these
private int width = 85;
private int height = 65;
private int xDelta = 4;
private int xOverFlow = 0;
public void update(Dimension bounds) {
x += xDelta;
if (x > bounds.width) {
x = 0;
}
if (x + width > bounds.width) {
xOverFlow = (x + width) - bounds.width;
} else {
xOverFlow = -1;
}
}
public void paint(Graphics2D g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(x, y);
paintTruck(g2d);
g2d.dispose();
if (xOverFlow > 0) {
g2d = (Graphics2D) g.create();
g2d.translate(xOverFlow - width, y);
paintTruck(g2d);
g2d.dispose();
}
}
protected void paintTruck(Graphics2D g2d) {
g2d.setColor(Color.gray);
g2d.fillRect(0, 10, 70, 45);
g2d.setColor(Color.yellow);
g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
g2d.setColor(Color.black);
g2d.fillArc(5, 50, 10, 10, 0, 360);
g2d.fillArc(55, 50, 10, 10, 0, 360);
g2d.setColor(Color.gray);
g2d.fillRect(0, 10, 70, 45);
g2d.setColor(Color.yellow);
g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
g2d.setColor(Color.black);
g2d.fillArc(5, 50, 10, 10, 0, 360);
g2d.fillArc(55, 50, 10, 10, 0, 360);
}
}
}
另一个选择可能是拥有多个卡车实例,因此当第二个卡车开始离开可见区时,您可以创建另一个卡车
例如......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Truck> trucks;
public TestPane() {
trucks = new ArrayList<>(2);
trucks.add(new Truck());
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Truck[] truckArray = trucks.toArray(new Truck[trucks.size()]);
for (Truck truck : truckArray) {
truck.update(getSize());
if (truck.getX() > getWidth()) {
trucks.remove(truck);
} else if (truck.getX() + truck.getWidth() > getWidth() && trucks.size() == 1) {
trucks.add(new Truck());
}
}
repaint();
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Truck truck : trucks) {
truck.paint(g2d);
}
g2d.dispose();
}
}
public class Truck {
// I had to calculate these
private int width = 85;
private int height = 65;
private int x = -width;
private int y;
private int xDelta = 4;
public void update(Dimension bounds) {
x += xDelta;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void paint(Graphics2D g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.translate(x, y);
paintTruck(g2d);
g2d.dispose();
}
protected void paintTruck(Graphics2D g2d) {
g2d.setColor(Color.gray);
g2d.fillRect(0, 10, 70, 45);
g2d.setColor(Color.yellow);
g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
g2d.setColor(Color.black);
g2d.fillArc(5, 50, 10, 10, 0, 360);
g2d.fillArc(55, 50, 10, 10, 0, 360);
g2d.setColor(Color.gray);
g2d.fillRect(0, 10, 70, 45);
g2d.setColor(Color.yellow);
g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
g2d.setColor(Color.black);
g2d.fillArc(5, 50, 10, 10, 0, 360);
g2d.fillArc(55, 50, 10, 10, 0, 360);
}
}
}