62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.*;
import java.awt.Point;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
class Building {
private int width;
private int height;
private Color color = Color.BLACK;
private List<Window> windows = new ArrayList<Window>();
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Color getColor() {
return color;
}
public List<Window> getWindows() {
return windows;
}
public Building (int width, int height) {
this.width = width;
this.height = height;
int windowCount = new Random().nextInt(5) + 1;
for (int i = 0; i < windowCount; i++) {
windows.add(new Window());
}
}
}
class Window {
private Color color = Color.YELLOW;
public Color getColor() {
return color;
}
}
public class Test extends JApplet {
private List<Building> buildings = new ArrayList<Building>();
private List<Point> points = new ArrayList<Point>();
private Random r = new Random();
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public Test() {
initParam(WIDTH, HEIGHT);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.add(new Test());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
paintBuilding(g);
}
private void initParam(int width, int height) {
int count = r.nextInt(10) + 1;
for (int i = 0; i < count; i++) {
Building building = new Building(r.nextInt(100) + 50, r.nextInt(100) + 50);
buildings.add(building);
Point point = new Point(r.nextInt(width - building.getWidth()), r.nextInt(height - building.getHeight()));
points.add(point);
}
}
private void paintBuilding(Graphics g) {
for (int i = 0; i < buildings.size(); i++) {
Building building = buildings.get(i);
Point point = points.get(i);
g.setColor(building.getColor());
g.fillRect(point.x, point.y, building.getWidth(), building.getHeight());
List<Window> windows = building.getWindows();
for (Window window : windows) {
g.setColor(window.getColor());
int w = 5;
int h = 5;
g.fillRect(point.x + r.nextInt(building.getWidth() - w), point.y + r.nextInt(building.getHeight() - h), w, h);
}
}
}
}
<html>
<head>
<title>测试</title>
</head>
<body>
<applet width="800" height="600" code="Test.class">
</applet>
</body>
</html>
import java.util.*;
import java.awt.Point;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
class Building {
private int width;
private int height;
private Color color = Color.BLACK;
private List<Window> windows = new ArrayList<Window>();
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Color getColor() {
return color;
}
public List<Window> getWindows() {
return windows;
}
public Building (int width, int height) {
this.width = width;
this.height = height;
int windowCount = new Random().nextInt(5) + 1;
for (int i = 0; i < windowCount; i++) {
windows.add(new Window());
}
}
}
class Window {
private Color color = Color.YELLOW;
public Color getColor() {
return color;
}
}
public class Test {
public static void main(String[] args) {
final Random r = new Random();
int count = r.nextInt(10) + 1;
int width = 800;
int height = 600;
final List<Building> buildings = new ArrayList<Building>(count);
final List<Point> points = new ArrayList<Point>();
for (int i = 0; i < count; i++) {
Building building = new Building(r.nextInt(100) + 50, r.nextInt(100) + 50);
buildings.add(building);
Point point = new Point(r.nextInt(width - building.getWidth()), r.nextInt(height - building.getHeight()));
points.add(point);
}
JFrame frame = new JFrame();
frame.setSize(width, height);
frame.add(new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < buildings.size(); i++) {
Building building = buildings.get(i);
Point point = points.get(i);
g.setColor(building.getColor());
g.fillRect(point.x, point.y, building.getWidth(), building.getHeight());
List<Window> windows = building.getWindows();
for (Window window : windows) {
g.setColor(window.getColor());
int w = 5;
int h = 5;
g.fillRect(point.x + r.nextInt(building.getWidth() - w), point.y + r.nextInt(building.getHeight() - h), w, h);
}
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}