Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you create object several times to move simultaneously on screen?
    primarykey
    data
    text
    <p>I have a thread which drops a circle in the y direction. I want to now create several circles on screen dropping at the same time with random x positions. </p> <pre><code>import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JFrame; import javax.swing.JPanel; public class Goo { protected GooPanel gooPanel; private boolean loop = true; protected int width , height; private int frameTimeInMillis = 50; private RenderingHints renderingHints = new RenderingHints( RenderingHints.KEY_ANTIALIASING , RenderingHints. VALUE_ANTIALIAS_ON); @SuppressWarnings("serial") class GooPanel extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHints(renderingHints); draw(g2d); } } public Goo() { this (800, 500); } public Goo(int w, int h) { width = w; height = h; JFrame frame = new JFrame (); frame.setSize(width , height); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gooPanel = new GooPanel (); gooPanel.setPreferredSize(new Dimension(w, h)); frame.getContentPane ().add(gooPanel); frame.pack(); frame.setVisible(true); } public void go() { while (loop) { gooPanel.repaint(); try { Thread.sleep(frameTimeInMillis); } catch (InterruptedException e) {} } } public void draw(Graphics2D g) {} public void setFrameTime(int millis) { frameTimeInMillis = millis; } public Component getGooPanel () { return gooPanel; } } </code></pre> <p>My FallingDrop class:</p> <pre><code>import java.awt.*; public class FallingDrops extends Goo { double x, y, r; int red, green, blue = 0; Color a; FallingDrops() { x = width / 2; r = 10; y = -r; } FallingDrops(double x) { this.x = x; r = 10; y = -r; } public void draw(Graphics2D g) { g.setColor(Color.GRAY); g.fillRect(0, 0, width , height); g.setColor(Color.WHITE); g.fillOval ((int) (x - r), (int) (y - r), (int) (2 * r), (int) (2 * r)); y++; if (y - r &gt; height) y = -r; } public static void main(String [] args) { int num = 10; Goo gooDrop [] = new FallingDrops[num]; for(int i = 0; i &lt; gooDrop.length; i++) { double x = Math.random()*800; gooDrop[i] = new FallingDrops(x); System.out.println(x); gooDrop[i].go(); } } } </code></pre> <p>At current, the loop fails to complete when the go() method is executed; thus only painting ONE object on screen, and not several as indicated in my loop. This is a simple fix I am sure. Any ideas what I am doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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