Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can i draw a circle again if it goes too far on a JPanel
    primarykey
    data
    text
    <p>I am trying to make a program that starts with 7 circles at the top of a JPanel. All the circles have a random size and color. The circles start at the top of the screen and move downwards once they reach the bottom of the JPanel they should reappear at the top of the JPanel and move downwards again. I can already make the circles move downwards but I am unsure of how to draw them back at the top again. I made a method called replaceCircle to use to draw the circles back at the top of the JPanel but its not working. </p> <pre><code>import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.Rectangle2D; import java.util.Random; import javax.swing.JPanel; import javax.swing.Timer; public class keyExample extends JPanel implements ActionListener, KeyListener { private Circle[] circles = new Circle[7]; Timer t = new Timer(5, this); //current x and y double x = 150, y = 200; double changeX = 0, changeY = 0; private Circle c1, c2, c3, c4, c5, c6, c7, circleone; private int circlex = 10, circley = 0; // makes initial starting point of circles 0 private int newCirclex = 0, newCircley = 0; private javax.swing.Timer timer2; private Random num = new Random(); private int s = num.nextInt(8); public keyExample() { t.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); Random num = new Random(); Random colors = new Random(); Color color1 = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); Color color2 = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); Color color3 = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); Color color4 = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); Color color5 = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); Color color6 = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); Color color7 = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); int radius1 = num.nextInt(40); int radius2 = num.nextInt(20); int radius3 = num.nextInt(25); int radius4 = num.nextInt(45); int radius5 = num.nextInt(15); int radius6 = num.nextInt(40); int radius7 = num.nextInt(50); if (radius1 &lt; 5) { radius1 = 10; } else if (radius2 &lt; 5) { radius2 = 10; } else if (radius3 &lt; 5) { radius3 = 10; } else if (radius4 &lt; 5) { radius4 = 10; } else if (radius5 &lt; 5) { radius5 = 10; } else if (radius6 &lt; 5) { radius6 = 10; } else if (radius7 &lt; 5) { radius7 = 10; } else { } c1 = new Circle(circlex, circley, radius1, color1); c2 = new Circle(circlex + 70, circley, radius2, color2); c3 = new Circle(circlex + 150, circley, radius3, color3); c4 = new Circle(circlex + 220, circley, radius4, color4); c5 = new Circle(circlex + 270, circley, radius5, color5); c6 = new Circle(circlex + 340, circley, radius6, color6); c7 = new Circle(circley + 410, circley, radius7, color7); circles[0] = c1; circles[1] = c2; circles[2] = c3; circles[3] = c4; circles[4] = c5; circles[5] = c6; circles[6] = c7; timer2 = new javax.swing.Timer(33, new MoveListener()); timer2.start(); } public void NewCircle() { for (int i = 0; i &lt; circles.length; i++) { Random num = new Random(); Random colors = new Random(); Color color = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); int radius = num.nextInt(40); if (radius &lt; 5) { radius = radius + 10; } else { } circles[i] = new Circle(circlex, circley, radius, color); } } public void replaceCircle() { int height = getHeight(); newCircley = newCircley + s; circley = newCircley; Random num = new Random(); int radius = num.nextInt(34); Random colors = new Random(); Color color = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256)); if (circley &gt; height) { c1 = new Circle(10, 0, radius, color); } else { } } public void createCircle() { } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.BLUE); g2.fill(new Rectangle2D.Double(x, y, 40, 40)); for (int i = 0; i &lt; circles.length; i++) { circles[i].fill(g); } } public void actionPerformed(ActionEvent e) { repaint(); x += changeX; y += changeY; changeX = 0; changeY = 0; } public void up() { if (y != 0) { changeY = -3.5; changeX = 0; } } public void down() { if (y &lt;= 350) { changeY = 3.5; changeX = 0; } } public void left() { if (x &gt;= 0) { changeX = -3.5; changeY = 0; } } public void right() { if (x &lt;= 550) { changeX = 3.5; changeY = 0; } } private class MoveListener implements ActionListener { public void actionPerformed(ActionEvent e) { Random speed = new Random(); int s2 = speed.nextInt(12); int s3 = speed.nextInt(4); int s4 = speed.nextInt(20); int s5 = speed.nextInt(7); int s6 = speed.nextInt(5); int s7 = speed.nextInt(8); c1.move(0, s); c2.move(0, s2); c3.move(0, s3); c4.move(0, s4); c5.move(0, s5); c6.move(0, s6); c7.move(0, s7); repaint(); } } public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_UP) { up(); } if (code == KeyEvent.VK_DOWN) { down(); } if (code == KeyEvent.VK_RIGHT) { right(); } if (code == KeyEvent.VK_LEFT) { left(); } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public static void main(String[] args) { new keyExample(); } } </code></pre> <p>Circle Class</p> <pre><code>import java.awt.*; </code></pre> <p>public class Circle {</p> <pre><code> private int centerX, centerY, radius, coord; private Color color; public Circle(int x, int y, int r, Color c) { centerX = x; centerY = y; radius = r; color = c; } public void draw(Graphics g) { Color oldColor = g.getColor(); g.setColor(color); g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g.setColor(oldColor); } public void fill(Graphics g) { Color oldColor = g.getColor(); g.setColor(color); g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g.setColor(oldColor); } public boolean containsPoint(int x, int y) { int xSquared = (x - centerX) * (x - centerX); int ySquared = (y - centerY) * (y - centerY); int RadiusSquared = radius * radius; return xSquared + ySquared - RadiusSquared &lt;= 0; } public void move(int xAmount, int yAmount) { centerX = centerX + xAmount; centerY = centerY + yAmount; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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