Displaying Dots in a Frame
1. What Is a Dot? (beginner)
Dot = tiny filled oval (1-4 px!) or single-pixel line. Scatter random dots = starfield/confetti! Uses fillOval + Random.
2. Code — 100 Random Dots (exam program!)
import java.awt.*;
import java.util.Random;
class Dots extends Frame {
Random r = new Random();
Dots() { setSize(300, 200); setVisible(true); }
public void paint(Graphics g) {
g.setColor(Color.BLUE);
for (int i = 0; i < 100; i++) {
int x = 10 + r.nextInt(280); // random x!
int y = 50 + r.nextInt(140); // random y (below title!)
g.fillOval(x, y, 3, 3); // 3px dot!
}
}
}
new Dots();
Single dot: g.drawLine(x, y, x, y); (1px!) or g.fillRect(x, y, 2, 2);.
3. Animation Taste (beyond static!)
Move dots: store list, Thread updates + repaint() every 100ms (game loop preview!).
4. Traps
- Dots vanish on resize (repaint redraws RANDOM new!). Store points to keep!
- y < 50 hidden under title (start lower!).
fillOvalneeds width/height ≥ 1 (0 = invisible!).
5. Exam Points
- Random-dots program — 5 marks, frequent.
fillOvaldot trick + repaint — 3 marks, frequent.