Note that there are some explanatory texts on larger screens.

plurals
  1. POContentPane background color changes when adding object
    text
    copied!<p>I have the following code:</p> <pre><code>import java.awt.*; import javax.swing.*; import java.util.ArrayList; import java.util.List; public class Ball extends JPanel implements Runnable { List&lt;Ball&gt; balls = new ArrayList&lt;Ball&gt;(); Color color; int diameter; long delay; private int x; private int y; private int vx; private int vy; public Ball(String ballcolor, int xvelocity, int yvelocity) { color = Color.PINK; diameter = 30; delay = 40; x = 1; y = 1; vx = xvelocity; vy = yvelocity; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(color); g.fillOval(x,y,50,50); //adds color to circle g.setColor(Color.pink); g2.drawOval(x,y,50,50); //draws circle } public void run() { while(isVisible()) { try { Thread.sleep(delay); } catch(InterruptedException e) { System.out.println("interrupted"); } move(); repaint(); } } public void move() { if(x + vx &lt; 0 || x + diameter + vx &gt; getWidth()) { vx *= -1; } if(y + vy &lt; 0 || y + diameter + vy &gt; getHeight()) { vy *= -1; } x += vx; y += vy; } private void start() { while(!isVisible()) { try { Thread.sleep(25); } catch(InterruptedException e) { System.exit(1); } } Thread thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String[] args) { Ball ball1 = new Ball("pink",6,6); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().setBackground(Color.BLACK); f.getContentPane().add(ball1); f.setSize(1000,500); f.setLocation(200,200); new Thread(ball1).start(); f.setVisible(true); } } </code></pre> <p>I would like to make the background black instead of the default grey one, but this only works if I get the ball out of the code, basically deleting <code>f.getContentPane().add(ball1);</code>. What I want is a pink ball bouncing on a black background, but it keeps going back to grey when adding the ball.</p> <p>What is the problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload