Ich kann die Farbe meines Hintergrunds im Schwingen nicht ändern. Es ist ein wirklich einfaches Pong-Spiel, mit dem ich nur herumgespielt habe, aber ich kann die Hintergrundfarbe nicht ändern. Hier ist mein Code [die Hintergrundfarbe in der Haupt geändert wird] (ich weiß, es ist chaotisch, ich lerne immer noch):kann Farbe des Hintergrundschwingens nicht ändern
public class Pong extends JPanel {
int x = 0;
int y = 000;
int yP = 300;
int xP = 300;
int border = 50;
boolean ballGoingDown = true;
boolean ballGoingRight = true;
private void moveBall() throws InterruptedException {
if (ballGoingRight == true) {
x++;
}
if (ballGoingRight == false) {
x--;
}
if (ballGoingDown == true) {
y++;
}
if (ballGoingDown == false) {
y--;
}
if (y == getHeight() - border) {
ballGoingDown = false;
}
if (y == 0) {
ballGoingDown = true;
}
if (x == getWidth() - border) {
ballGoingRight = false;
}
if (x == 0) {
ballGoingRight = true;
}
}
@
Override
public void paint(Graphics G) {
super.paint(G);
G.fillOval(x, y, 50, 50);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Pong");
frame.setSize(700, 500);
frame.setVisible(true);
frame.getContentPane().setBackground(Color.red);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pong game = new Pong();
frame.add(game);
while (true) {
game.repaint();
game.moveBall();
Thread.sleep(1);
}
}
}
Da Ihre 'Pong' Klasse ist ein' JPanel' Sie den Hintergrund von 'game' Objekt ändern sollten. Versuchen Sie 'game.setBackground (Color.red);' anstelle von 'frame.getContentPane(). SetBackground (..)' – ArcticLord