【发布时间】:2014-09-25 22:40:44
【问题描述】:
目前,我对这项任务感到不知所措。我才 3 周进入 Java 编程,我得到了这段代码,并被告知以我不完全熟悉的方式对其进行修改。因此,任何帮助将不胜感激。我真的很想了解这一点,所以任何信息都会有所帮助。
我目前正在努力的方向是:
"修改 Model 类以存储 Turtles 的 ArrayList。使 Model.update 为 ArrayList 中的每个 Turtle 调用 Turtle.update。至 测试一下,在 ArrayList 中放两三只海龟,每只从 不同的位置。当您单击时(这会导致“setDestination” 被调用),让所有的海龟前往同一个目的地。得到 在你继续之前还有这么多工作。”
在我创建这些海龟的数组之前,一只海龟表现良好。现在我已经创建了一个数组,我的面板上没有显示任何内容。不过,它确实可以正确编译。我知道我应该制作单独的 Graphics 对象,但是如何以及在哪里是最好的地方呢?这是代码(在 4 个单独的文件中):
Controller.java
import java.awt.Graphics;
import java.io.IOException;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.Timer;
class Controller implements MouseListener
{
Model model;
View view;
Controller() throws IOException, Exception {
model = new Model();
view = new View(this);
new Timer(50, view).start();
}
public void update(Graphics g) {
model.update(g);
}
public void mousePressed(MouseEvent e) {
model.setDestination(e.getX(), e.getY(), view.getWidth(), view.getHeight());
}
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public static void main(String[] args) throws Exception {
// Use the following line to determine which directory your program
// is being executed from, since that is where the image files will
// need to be.
//System.out.println("cwd=" + System.getProperty("user.dir"));
new Controller();
}
}
模型.java
import java.awt.Graphics;
import java.io.IOException;
class Model
{
private Turtle[] t;
Model() throws IOException {
Turtle[] t = new Turtle[3];
for (int i = 0; i <3; i++)
t[i] = new Turtle();
// turtle = new Turtle();
}
public void update(Graphics g) {
for(int i = 0; i <3; i++)
t[i].update(g);
}
public void setDestination(int x, int y, int width, int height) {
for(int i = 0; i <3; i++)
{
t[i].setDest(x, y);
}
}
}
乌龟.java
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Random;
class Turtle
{
private int x;
private int y;
private int dest_x;
private int dest_y;
private Image image;
Turtle() {
try {
image = ImageIO.read(new File("turtle.png"));
} catch (IOException ioe) {
System.out.println("Unable to load image file.");
}
}
public int getX() { return x; }
public int getY() { return y; }
public void setX(int xIn) { x = xIn; }
public void setY(int yIn) { y = yIn; }
public void update(Graphics g) {
// Move the turtle
if (x < dest_x) {
x += 1;
} else if (x > dest_x) {
x -= 1;
}
if (y < dest_y) {
y += 1;
} else if (y > dest_y) {
y -= 1;
}
// Draw the turtle
}
public void setDest(int x, int y) {
dest_x = x;
dest_y = y;
}
}
View.java
import javax.swing.JFrame;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class View extends JFrame implements ActionListener {
private class MyPanel extends JPanel {
Controller controller;
MyPanel(Controller c) {
controller = c;
addMouseListener(c);
}
public void paintComponent(Graphics g) {
controller.update(g);
revalidate();
}
}
public View(Controller c) throws Exception{
setTitle("Assignment 4");
setSize(1000, 700);
getContentPane().add(new MyPanel(c));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
repaint();
}
}
【问题讨论】:
标签: java arrays object user-interface jpanel