矩形が画面の右側(画面の右端の右側)から開始し、左側に移動する単純なアニメーションを作成しようとしています。したがって、この場合、フレームの幅は1000になり、壁は1100のx値から始まります。最初は、長方形は であるはずです。しかし、長方形が左に移動すると、最終的に見えるようになります。しかし、このアニメーションはそれをしません。ウォールのx値が画面の境界内にある場合でも、レンダリングされません。JComponentがスクリーンから外れてレンダリングを停止する
私は壁のpaintComponent()
方法でprintln()
文を入れてみました、と私はpaintComponent()
は、フレームのrepaint()
メソッドによって呼び出さなっていなかったことを発見しました。私は、壁が最初にフレームに追加されたときに、スイングは画面外だったのでレンダリングする必要はないと判断し、壁が最終的に画面に表示されても、スイングはそれが必要ではないと考えましたレンダリングする。
フレームとコンポーネントの再検証と無効化を試みましたが、何も効果がありませんでした。これを解決するために私を助けてください。以下のコードです:
package graphics.simpleAnimation;
public class Simple_Animation implements Runnable {
private UI ui; // The UI (frame)
private Wall wall; // Wall object that moves across the screen
private Simple_Animation() {
// Initialize the wall object (intentionally given an x value that is greater than the frame's width)
wall = new Wall(1100, 400, 200, 400);
// Initialize the UI (width is only 1000)
ui = new UI(1000, 600, "Geometry Dash");
// Add the wall to the ui (the frame)
ui.add(wall);
}
public void run() {
// Set the frame visible
ui.setVisible(true);
// Repaint the frame and move the wall
while (true) {
ui.repaint();
wall.moveWall(-2, 0);
try {
Thread.sleep(16);
} catch (InterruptedException IE) {
System.out.println(IE);
}
}
}
// Starts the program in a new thread
public static void main(String[] args) {
Simple_Animation simpleAnimation = new Simple_Animation();
new Thread(simpleAnimation).start();
}
}
package graphics.simpleAnimation;
import javax.swing.*;
import java.awt.*;
public class UI extends JFrame {
// Variables storing the width and height of the content pane (where the components are being rendered)
public int content_pane_width;
public int content_pane_height;
public UI(int frameW, int frameH, String frameTitle) {
setTitle(frameTitle);
setSize(frameW, frameH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(null);
content_pane_width = getContentPane().getWidth();
content_pane_height = getContentPane().getHeight();
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
}
package graphics.simpleAnimation;
import java.awt.*;
import javax.swing.*;
public class Wall extends JComponent {
private int wallX;
private int wallY;
private int wallW;
private int wallH;
Wall(int x, int y, int sizeX, int sizeY) {
wallX = x;
wallY = y;
wallW = sizeX;
wallH = sizeY;
setSize(getPreferredSize());
}
public void moveWall(int moveX, int moveY) {
wallX += moveX;
wallY += moveY;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(wallW, wallH);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
setLocation(wallX, wallY);
g2d.fillRect(0, 0, wallW, wallH);
}
}
ペイントメソッド内で 'setLocation(wallX、wallY);'を実行しないでください。ペイントは現在の状態をペイントして決して変更しないでください。また、自分のプロパティをコンポーネントのプロパティと混在させているので、自分でコンポーネントを配置/サイズ変更しようとしているので、単にコンポーネントの既存のプロパティを使用することをお勧めします。また、 'getPreferredSize'は' null'レイアウトを使用しているときには何もしません。おそらく問題の一部を説明しています。 – MadProgrammer
[コードブロックのぶら下げ括弧の検出/修正](http:// meta .stackexchange.com/q/251795/155831)私はもはや気にすることができなかった問題を解決する。 –