Java移动图像

时间:2016-04-19 15:43:25

标签: java

请帮助我,我需要按下按钮来旋转图像。目前,我有运动,但我不知道如何改变图像。我想如果我按下左箭头就是这样的package CarGame; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class CarGame extends JPanel implements ActionListener, KeyListener { Timer timer = new Timer(5, this); int x = 0; int y = 0; int a = 0; int b = 0; public CarGame() { this.timer.start(); addKeyListener(this); setFocusable(true); setBackground(Color.WHITE); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); ImageIcon i = new ImageIcon(getClass().getResource("right.png")); g.drawImage(i.getImage(), this.x, this.y, this); } @Override public void actionPerformed(ActionEvent e) { if (this.x < 0) { this.a = 0; this.x = 0; } if (this.x > 865) { this.a = 0; this.x = 865; } if (this.y < 0) { this.b = 0; this.y = 0; } if (this.y > 490) { this.b = 0; this.y = 490; } this.x += this.a; this.y += this.b; repaint(); } @Override public void keyPressed(KeyEvent e) { int c = e.getKeyCode(); if (c == 37) { this.a = -1; this.b = 0; } if (c == 38) { this.a = 0; this.b = -1; } if (c == 39) { this.a = 1; this.b = 0; } if (c == 40) { this.a = 0; this.b = 1; } } @Override public void keyTyped(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) { this.a = 0; this.b = 0; } public static void main(String[] args) { CarGame cg = new CarGame(); JFrame jf = new JFrame(); jf.setTitle(""); jf.setSize(1024, 600); jf.setResizable(false); jf.setLocationRelativeTo(null); jf.setVisible(true); jf.setDefaultCloseOperation(3); jf.add(cg); } } 等等。

Every startup needs a steady diet of funding to keep it strong and growing. Datadog, a monitoring service that helps customers bring together data from across a variety of infrastructure and software is no exception. Today it announced a massive $94.5 million Series D Round. The company would not discuss valuation.

0 个答案:

没有答案