私は、最初の状態が1ピクセルを右下に斜めに動かすべき円を含むビッグバンの世界を作成しようとしています。ユーザーが4つの矢印キーを押すたびに、円はその方向に向かって1ピクセル移動し続けます。たとえば、右矢印キーを押し続けると、円は右に移動し、押すたびに高速かつ高速に移動します。私が持っている問題は、円を動かすことができないということです!私は、それぞれの方法についてxとyの座標を変更することがそれに応じてそれを動かすべきだと考えました。私は間違って何をしていますか?私がそれを記述したように円を動かすために私は何ができますか?ここで私は修正する必要があるファイルGame.javaさ:単純なJavaでオブジェクトを移動するBigBang World
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Game implements World {
public Game() { }
int x, y = 200;
int width, height = 100;
void Circle(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void draw(Graphics g) {
int x = 200;
int y = 200;
int width = 100;
int height = 100;
g.setColor(Color.red);
g.fillOval(x,
y,
width,
height);
}
public void teh() {
this.x++;
this.y++;
}
public void meh(MouseEvent e) {
int x = e.getX(), y = e.getY();
System.out.println("Mouse event detected.");
}
public void keh(KeyEvent e) {
int x = e.getKeyLocation(), y = e.getKeyLocation();
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP){
this.y--;
System.out.println("Up.");
}
else if (key == KeyEvent.VK_DOWN){
this.y++;
System.out.println("Down.");
}
else if (key == KeyEvent.VK_RIGHT){
this.x++;
System.out.println("Right.");
}
else if (key == KeyEvent.VK_LEFT){
this.x--;
System.out.println("Left.");
}
else{}
// switch(keyCode) {
// case KeyEvent.VK_UP:
// // handle up
// break;
// case KeyEvent.VK_DOWN:
// // handle down
// break;
// case KeyEvent.VK_LEFT:
// // handle left
// break;
// case KeyEvent.VK_RIGHT :
// // handle right
// break;
// }
}
public boolean hasEnded() {
return false;
}
public void sayBye() {
System.out.println("BYE!");
}
public static void main(String[] args) {
BigBang b = new BigBang(new Game());
b.start(50, // delay
400 // size
);
}
}
は、これらが使用されている補助的なファイルですが、手つかずのままにしてください。私はあなたの参照のためにそれらを含めています:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BigBang extends JComponent implements ActionListener, MouseListener, MouseMotionListener, KeyListener {
Timer timer;
World world;
public BigBang(World world) {
this.world = world;
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.addKeyListener(this);
this.setFocusable(true);
this.requestFocus();
}
public void start(int delay, int size) {
JFrame a = new JFrame();
a.add(this);
a.setVisible(true);
a.setSize(size, size);
this.timer = new Timer(delay, this);
this.timer.start();
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
this.world.meh(e);
this.repaint();
}
public void mouseDragged(MouseEvent e) {
this.world.meh(e);
this.repaint();
}
public void mouseMoved(MouseEvent e) { }
public void mouseReleased(MouseEvent u) {
this.world.meh(u);
this.repaint();
}
public void mouseClicked(MouseEvent e) { }
public void keyPressed(KeyEvent e) {
this.world.keh(e);
this.repaint();
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
// int count;
public void actionPerformed(ActionEvent e) {
// this.count += 1;
// System.out.println("Ouch" + this.count);
if (this.world.hasEnded()) {
this.timer.stop();
this.world.sayBye();
} else {
this.world.teh();
}
this.repaint();
}
public void paintComponent(Graphics g) {
this.world.draw(g);
}
}
およびインターフェイスWorld.java:
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
interface World {
void draw(Graphics g);
void teh();
void meh(MouseEvent e);
void keh(KeyEvent e);
boolean hasEnded();
void sayBye();
}
私のミスが嘘のでしょうか?矢印キーを押したときにサークルを移動できないのはなぜですか?矢印キーを押すたびに、どのようにオブジェクトのベロシティに追加し続けることができますか?変更やアドバイスが役立ちます。
矢印キーwiを押しながら 'this.x ++;'で加速度の代わりに速度のように振舞うでしょう – phflack