有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java初学者错误

我刚开始学Java。我写了一个这样的程序:

细菌。阶级

import java.awt.*;


public class Bacteria extends Creature{

public static final int COLOR_STEP = 256/MAX_AGE;
Bacteria(Board board, int _x, int _y){


    super.board = board;
    //initialize age, x, y
    super.age = 1;
    super.x = _x;
    super.y = _y;
}

//Draw a circle representing the creature at its position
//This function is done.

public void draw(Graphics g) {
    g.setColor(new Color(0, COLOR_STEP * age, 0));
    g.fillOval(x * board.CELL_SIZE, y * board.CELL_SIZE, board.CELL_SIZE, board.CELL_SIZE);
}
public void tick() {
    //your code
    // this is how the creature lives in one tick
    // make the creature get older if it is not dead.
    // if it's got older than MAX_AGE then it should die
    // ...call board.addDead() to register the dead creature
    // if it can have baby then try reproduce()
    age++;
    if (this.canHaveBaby()) {
        this.reproduce();
    }
    //this.reproduce();
    if (age > MAX_AGE) {
        board.addDead(this);
    }
}
}

生物。阶级

import java.awt.*;


public class Creature {
public static final int MAX_AGE = 5;
public Board board;

public int age; // age of the creature measures in ticks
public int x; // (x,y) is the position of the creature
public int y;

Creature(Board board, int _x, int _y) {
    this.board = board;
    //initialize age, x, y
    age = 1;
    x = _x;
    y = _y;
}

public Creature() {
}


//Draw a circle representing the creature at its position
//This function is done.
public void draw(Graphics g){}

public void tick(){}

public void reproduce() {
    //your code
    // if it can have a baby then produce a baby
    // ...in an empty cell next to its cell.
    // board.emptyCell() should be used
    // ....to check whether a cell in the board is empty
    // and call board.addBaby() to place the baby to the board
    //int x_current = x, y_current = y;
    if(board.emptyCell(x, y+1)){
        Creature baby = new Creature(board,x, y+1);
        //System.out.println("1");
        board.addBaby( baby);
    }
    else if(board.emptyCell(x+1,y)){
        Creature baby = new Creature(board,x+1, y);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x+1,y+1)){
        Creature baby = new Creature(board,x+1, y+1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x-1,y)){
        Creature baby = new Creature(board,x-1, y);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x,y-1)){
        Creature baby = new Creature(board,x, y-1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x-1,y-1)){
        Creature baby = new Creature(board,x-1, y-1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x-1,y+1)){
        Creature baby = new Creature(board,x-1, y+1);
        board.addBaby(baby);
    }
    else if(board.emptyCell(x+1,y-1)){
        Creature baby = new Creature(board,x+1, y-1);
        board.addBaby(baby);
    }
}

public boolean canHaveBaby() {
    //your code
    // if the creature is dead or it is too young
    // then it cannot have a baby
    if(age == 0 || age > MAX_AGE){
        return false;
    }
    return true;
}

public boolean atPosition(int _x, int _y) {
    //your code
    // return true if the creature is at the position (_x,_y)
    // you need this function for board.empty to function correctly
    if(_x == x && _y == y){
        return true;
    }
    return false;
}
}

董事会。阶级

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


public class Board extends JPanel {

public static final int CELL_SIZE = 10;
private final int rows = 10;
private final int cols = 20;
ArrayList<Creature> creatureList;

ArrayList<Creature> babies, dead;
// ArrayList<Bacteria> bacteriaList;
// ArrayList<Bacteria> babies_bac, dead_bac;
Board() {
    super();
    setBackground(Color.white);
    creatureList = new ArrayList<Creature>();
    //creatureList.size();
    babies = new ArrayList<Creature>();
    dead = new ArrayList<Creature>();
    setPreferredSize(new Dimension(cols * CELL_SIZE, rows * CELL_SIZE));
    creatureList.add(new Bacteria(this, 1, 1));
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Creature creature: creatureList) {
        creature.draw(g);
    }
}

public void tick() {
    dead = new ArrayList<Creature>();
    babies = new ArrayList<Creature>();

    for (Creature creature: creatureList) {
         creature.tick();
    }
    creatureList.addAll(babies);
    creatureList.removeAll(dead);
}

public void addBaby(Creature baby) {
    if (baby != null) babies.add(baby);
}

public void addDead(Creature deadCreature) {
    if (deadCreature != null) dead.add(deadCreature);
}

public boolean emptyCell(int x, int y) {
    if (x < 0 || y < 0 || x >= cols || y >= rows) return false;

    for (Creature creature : creatureList) {
        if (creature.atPosition(x, y)) {
            return false;
        }
    }
    for (Creature creature : creatureList) {
        if (creature.atPosition(x, y)) {
            return false;
        }
    }
    return true;
}

public String getPopulation() {
    return String.format("%d", creatureList.size());
}
}

和细菌刺激剂。爪哇

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class BacteriaSimulator extends JFrame {
private Board board;
private JLabel label;

public BacteriaSimulator() {
    initUI();
    setTitle("Bacteria Simulator");
    setSize(350, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void initUI() {
    JPanel panel = new JPanel();

    board = new Board();
    panel.add(board);

    label = new JLabel(board.getPopulation());
    panel.add(label);

    JButton button = new JButton("Tick");
    button.addActionListener(new ButtonNextListener());
    panel.add(button);

    add(panel);
    pack();
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            BacteriaSimulator ex = new BacteriaSimulator();
            ex.setVisible(true);
        }
    });
}

private class ButtonNextListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        board.tick();
        label.setText(board.getPopulation());
        repaint();
    }
}
}

我要登机。与creatureList一起上课。然而,电路板仍然用细菌(creatureList.add(new Bacteria(this, 1, 1));)初始化。我遇到了一个问题,那就是董事会。类无法运行我想要的draw()和tick()方法。如何画出细菌的物体,并用正确的方法打勾()?你能帮助我吗!谢谢!


共 (1) 个答案

  1. # 1 楼答案

    draw()和tick()函数是细菌的一部分。JAVA它们不能从板上使用。java,除非创建细菌实例,然后调用使用该实例的方法