The example in this article describes how to achieve the starry sky effect in Java. Share it with everyone for your reference.
The specific implementation code is as follows:
Copy the code code as follows:
import java.awt.*;
public class Main {
public static void main(String[] args) {
Frame frame = new Frame("Gypsophila");
MyPanel panel = new MyPanel();
frame.add(panel);
frame.setBackground(Color.BLACK);
frame.setSize(1024, 768);
frame.setVisible(true);
}
}
class MyPanel extends Panel {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
g.setColor(Color.WHITE);
for (int i = 0; i < 300; i++) {
g.drawString("*", (int) (Math.random() * 1024),
(int) (Math.random() * 768));
}
g.fillOval(800, 100, 100, 100);
g.setColor(Color.BLACK);
g.fillOval(780, 80, 100, 100);
}
}
The running effect is shown in the figure below:
I hope this article will be helpful to everyone’s Java programming.