私はミニゲームを試してみますが、最初に私はどのようにアニメーション化するかを学びます:)それは2Dゲームになります。 私はアニメーション化しようとしたときに矩形を描画しようとすると問題が発生することがあります(コードは多かったが動作しませんでした):Java Swing矩形の小さなアニメーションを作成しようとします
これを修正したり追加したり私はそれをやろうとすることができる方法をいくつかのヒント。
public class Window extends JPanel implements ActionListener {
Timer tm = new Timer(5 , this);
int x2 = 0 , velX = 2;
static int x= 500;
static int y= 500;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(x2, 30, 30, 30);
tm.start();
}
public Window(){
JFrame f = new JFrame();
f.pack();
f.setTitle("Game");
f.setSize(x,y);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
/*public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
Rectangle rect = new Rectangle(50, 50, 50, 50);
g2d.translate(25, 25);
g2d.rotate(Math.toRadians(45));
g2d.draw(rect);
}*/
public static void main(String [] args) throws InterruptedException{
Game g = new Game();
g.setName("Test");
System.out.println(g.getName());
g.setScore();
}
@Override
public void actionPerformed(ActionEvent e) {
x2 = x2 + velX;
repaint();
}
}
を見てみることをお勧めします。あなたがそれを追加しても、それは私が理解できる限りうまく動作します。 – resueman
クラス名を 'Window'から他のもの(MyWindow/userGUI/etcなど)に変更することをお勧めします。 AWTクラスは['Window'](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html)です。将来的にいくつかの問題を引き起こす可能性があります。 – Frakcool