Hier ist meine Hauptklasse:Warum wird mein Symbol nicht neu gezeichnet?
package fast;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fast {
public JFrame frame = new JFrame("Fast");
public JPanel panel = new JPanel();
public Screen screen = new Screen();
public Fast() throws IOException {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 500);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
frame.setBackground(Color.YELLOW);
frame.add(screen);
screen.setBackground(Color.WHITE);
}
public static void main(String[] args) throws IOException {
Fast f = new Fast();
Thread thread = new Thread(new Screen());
thread.start();
}
}
Hier meine Bildschirm-Klasse ist:
package fast;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Screen extends JPanel implements Runnable {
BufferedImage img = ImageIO.read(new File("bg.png"));
ImageIcon car = new ImageIcon("sCar.png");
public int x = 50;
public Screen() throws IOException {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
car.paintIcon(this, g, x, 50); // I want this to move
drawBackground(g);
}
private void drawBackground(Graphics g) { // testing
g.setColor(Color.YELLOW);
g.fillRect(x, 100, 50, 50);
}
@Override
public void run() {
System.out.println("Hello");
x = 300;
repaint();
}
}
Wenn mein Programm erreicht „Hallo“, ich will es das Auto bei x = 300 neu zu zeichnen, aber es nicht. Was muss ich tun, damit dies funktioniert? Ich habe es in einer Run-Methode, weil ich es später als Thread ausführen lassen möchte, aber jetzt will ich nur, dass es sich bewegt.