I tried to paint some figures as you can see below ( I did not include the other class which involve main method)... the problem that it does not repaint I just get three thick lines as output but it should draw an oval and two rectangles so they move in the frame as the while loop works ...
package shapes;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
int n1=0;
int n2=0;
int n3=0;
@Override
public void paintComponent(Graphics g) {
do {
// super.paintComponent(g);
g.drawRect(n1++, 33, 54, 84);
g.setColor(Color.BLUE);
g.fillRect(100, n2++, 56, 97);
// g.setColor(Color.BLUE);
g.drawOval(44, n3++, 44, 66);
} while (n1<=500 && n2<=500 && n3<=500);
n1++;
n2++;
n3++;
repaint();
}
}
how to correct the code to make it repaints the figures for each loop so it does not show three thick lines as output ??? (I want the do command is included) .. thanks
super.paintComponent
before you do any custom painting (and not within your loop); repaint
from within paint methods, this could setup an infinite loop of repaints which could consume all your CPU cycles; Timer
instead to generate scheduled updates and animation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.