3
我想讓汽車圖像沿着一條路徑。汽車必須以恆定的速度「驅動」,並且必須保持平穩。我能夠讓車輛遵循一個積分列表,但我不知道如何以恆定的速度使車輛行駛平穩(車輛每2秒鐘移動到列表中的下一個點),以及如何採取硬編碼輪流。任何人都可以幫忙?Java汽車動畫平滑並採取曲線
代碼
軌道類加載軌道底層。
public class Track{
BufferedImage track;
Point trackPosition;
static final Point TRACK_POS = new Point(0, 0);
static final Point SENSOR_POS = new Point(250, 70);
public Track(){
try {
track = ImageIO.read(Roundabout.class.getResource("track.png"));
} catch (Exception ex) {
System.out.println("Problem loading track image: " + ex);
}
trackPosition=new Point(TRACK_POS.x,TRACK_POS.y);
}
public void paint (Graphics g)
{
g.drawImage(track,TRACK_POS.x, TRACK_POS.y, null);
}
}
汽車類
public class Car extends JComponent implements Vehicle{
BufferedImage car;
Point carPosition;
static final Point START_POS = new Point(10, 150);
int counter=0;
public Car(){
try {
car = ImageIO.read(Car.class.getResource("beetle_red.gif"));
} catch (Exception e) {
System.out.println("Problem loading car images: " + e);
}
carPosition = new Point(START_POS.x, START_POS.y);
}
public void paint (Graphics g)
{
g.drawImage(car,carPosition.x, carPosition.y, null); //original paint
}
public Point getCarPosition() {
return new Point(carPosition.x,carPosition.y);
}
public void update(){
repaint();
if(counter < Lane.firstLane.size()){
carPosition.x = Lane.firstLane.get(counter).x;
carPosition.y= Lane.firstLane.get(counter).y;
System.out.println("Pos: "+getCarPosition());
counter++;
}
else{
System.out.println("Destination reached");
}
repaint();
}
}
巷類
public class Lane {
public static List<Point> firstLane = new ArrayList<>(Arrays.asList(new Point(10,135),new Point(124,190),new Point(363,190),new Point(469,210)));
}
環島(主類)
public class Roundabout extends JFrame{
Track track=new Track();
TrafficLight trafficLight=new TrafficLight();
Car car=new Car();
ArrayList<Car> cars = new ArrayList<>();
byte[] array=new byte[]{0,2,1,1}; //test byte array
class Surface extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
Dimension size = getSize();
Insets insets = getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
/* Draw the track first */
track.paint(g);
/* Draw a car */
car.paint(g);
cars.add(car); //add to list
trafficLight.paint(g);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public Roundabout(){
initUI();
}
private void initUI() {
setTitle("Roundabout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new Surface());
setSize(580, 550);
setLocationRelativeTo(null);
moveCar();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Roundabout roundabout=new Roundabout();
roundabout.setVisible(true);
}
});
}
public void moveCar() {
Runnable helloRunnable = new Runnable() {
public void run() {
car.update();
repaint();
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 2, TimeUnit.SECONDS);
}
}
'JFrame'是否有'paintComponent(...)'方法?你怎麼能在'Roundabout'類中重寫那個!對於'JComponent'重寫此方法,而不是'paint(...) – 2015-03-02 12:03:53
我該如何解決這個問題,並且可以幫助我解決OP中的問題? – Sybren 2015-03-02 12:46:23
只要給我一些時間,創建一個例子(即將到來...)。直接嘗試使用'JLabel''而不是直接繪圖, – 2015-03-02 13:00:14