私はJavaグラフィックスプログラミングの初心者ですが、最初は問題があるとは思いませんでした:
私はこの単純なループを持っています - これはキューブを移動してサイズを変更しますが、非常に遅く、「汚れています」。私が何を意味するかを知っていれば、ピクセルが変化するのを見ることができます。ここで何ができるでしょうか?そして、なぜそんなに遅いのですか?皆さん、ありがとうございました!
したがって、コードは次のとおりです。
package game;
import javax.swing.*;
import java.awt.*;
public class Main extends JPanel implements Runnable{
Box b = new Box(0, 0, 20, 20);
Thread t = new Thread(this);
public Main(){
JFrame f = new JFrame();
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setVisible(true);
setBounds(0, 0, f.getWidth(), f.getHeight());
setBackground(Color.BLUE);
f.add(this);
t.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
b.paint(g);
}
public void run(){
while(b.x < 100){
b.x++;
b.y++;
b.width++;
b.height++;
repaint();
try{Thread.sleep(10);}catch(Exception e){}
}
}
public static void main(String[] args) {
new Main();
}
public class Box {
public int x;
public int y;
public int width;
public int height;
public boolean used;
public Box(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(x, y, width, height);
}
}
}