求助一个JAVA作业题啊。。。各位大哥大姐帮帮忙~~~

limeng199012 2010-09-24 10:39:01
设计并实现一个代表大楼的Building类,利用传递给该类构造方法的参数指定大楼的宽度与高度。每幢大楼均为黑色,上面有一些黄颜色的小窗户,窗户数量为随机数。创建一个程序,绘制多幢大楼(大楼个数也为随机数)!!!
...全文
390 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sheng55555 2010-10-06
  • 打赏
  • 举报
回复
我喜欢做题 那来试试,谢谢喽
limeng199012 2010-10-06
  • 打赏
  • 举报
回复
这个只是借鉴一下。。。。。。我自己写出来一个简单一点的啦!!!和这个不一样哦。。。感谢各位~~~~
Tassdars 2010-09-28
  • 打赏
  • 举报
回复
很多人反感做作业题,但是却普遍忽略了一件事——自助者他助,既然别人为了完成作业想交差了事,我就一定要帮他?

其实只要是这种拿分帖,目的就已经不纯粹了,我本身也没有想过要帮或者不帮楼主,如果楼主可以研究一下我的代码,甚至比我做得好,这属于自助,我也间接帮到了别人,自己还拿了分,何乐不为?如果楼主只是交差而已,那是他不愿意自助,我也就没帮到他。有没帮到,全在楼主自己,不在我,我也没有义务必须手把手地教楼主。

对于我来说,目的当然非常明确,挣点分,锻炼一下将描述转化为代码的面向对象能力,就这么简单,这是自私的东西,也没什么不好意思说的。

最后说一下,作业题解不解答,只是个主观意愿问题,大家都有说要与说不的权利。
Jay_xiaolei 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 clariones 的回复:]
引用 7 楼 hemowolf 的回复:

引用 6 楼 zlllyk110 的回复:
拒绝回答作业题。学技术就是自己做,依赖别人你永远也学不会。


顶一个

同意! 用这种帖子拿分,其实不是帮别人。
[/Quote]
up
sainer 2010-09-25
  • 打赏
  • 举报
回复
又是作业题,~~
limeng199012 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tassdars 的回复:]
Java code
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;
……
[/Quote]

用JAVA小程序运行,怎么改啊。。。。。。
clariones 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hemowolf 的回复:]

引用 6 楼 zlllyk110 的回复:
拒绝回答作业题。学技术就是自己做,依赖别人你永远也学不会。


顶一个
[/Quote]
同意! 用这种帖子拿分,其实不是帮别人。
小灰狼 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zlllyk110 的回复:]
拒绝回答作业题。学技术就是自己做,依赖别人你永远也学不会。
[/Quote]

顶一个
zlllyk110 2010-09-25
  • 打赏
  • 举报
回复
拒绝回答作业题。学技术就是自己做,依赖别人你永远也学不会。
ksn52588a 2010-09-25
  • 打赏
  • 举报
回复
顶楼上的
Tassdars 2010-09-25
  • 打赏
  • 举报
回复
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);
}
}
}
}


Applet和Application都能运行,顺便带上Test.html代码:

<html>
<head>
<title>测试</title>
</head>
<body>
<applet width="800" height="600" code="Test.class">
</applet>
</body>
</html>
Tassdars 2010-09-24
  • 打赏
  • 举报
回复
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);
}
}


写得复杂了点,也不是完全用好了面向对象的思想,主要是题目的局限,很多时候抛开题目的限制自己去规划更好一些。大楼、窗户之间会重叠,这是一个BUG,解决比较费劲,就不解决了,留给楼主做思考题。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧