1

呸。我发现了这个错误。当然,这简单得离谱。MovingPanelItem 下的覆盖更新需要写成如下:

@Override
public void update( int x, int y )
{
    xCoord = xCoord + getxStep();
    yCoord = yCoord + getyStep();
}

完全披露:这是家庭作业。

问题:在每个 Key listener 事件中,屏幕应该更新并且移动的对象应该移动。目前,虽然对象最初出现,但如果我按下规定的键,它们就会消失。

此外,所有 PanelItem 都存储在 ArrayList 中(在另一个类中)。所有不是 MovingPanelItem 子类的对象在 KeyEvent 时仍保留在屏幕上。

我知道我留下了很多想象空间,所以如果需要更多细节,请告诉我。

超类:

public class PanelItem
{
    private Image img;
    protected int xCoord;
    protected int yCoord;
    protected int width;
    protected int height;

    //constructor
    public SceneItem( String path, int x, int y, int w int h )
    {
         xCoord = x;
         yCoord = y;
         setImage( path, w, h );
    }

    public void SetImage( String path, int w, int h )
    {
        width = w;
        height = h;
        img = ImageIO.read(new File(path));
    }

    //to be Overriden
    public void update( int width, int height )
    {

    }

}

子类:

public class MovingPanelItem extends PanelItem
{
    private int xStep, yStep;

    // x & y correspond to the coordinate plane
    // w & h correspond to image width and height
    // xs & ys correspond to unique randomized 'steps' in the for the x and y values

    public MovingSceneItem(String path, int x, int y, int w, int h, int xs, int ys)
    {
        super( path, x, y, w, h);
        setxStep(xs);
        setyStep(ys);
        update( x, y );
    }


    @Override
    public void update( int x, int y )
    {
        this.xCoord = x + getxStep();
        this.yCoord = y + getyStep();
    }
}

(回应鳗鱼)面板/窗口本身包含在以下类中:

public class Panel extends JPanel {
    protected ArrayList<PanelItem> panelItems;
    private JLabel statusLabel;
    private long randomSeed;
    private Random random;

    public Panel(JLabel sl) {
        panelItems = new ArrayList<PanelItem>();
        statusLabel = sl;
        random = new Random();
        randomSeed = 100;
    }

    private void addPanel() {
        addPanelItems(10, "Mouse");
    }

    private void addPanelItems(int num, String type) {
        Dimension dim = getSize();
        dim.setSize(dim.getWidth() - 30, dim.getHeight() - 30);

        synchronized (panelItems) {
            for (int i = 0; i < num; i++) {
                int x = random.nextInt(dim.width);
                int y = random.nextInt(dim.height);

                if (type.equals("Person")) {
                    int xs = random.nextInt(21) - 10;
                    int ys = random.nextInt(21) - 10;
                    panelItems.add(new Mouse(x, y, xs, ys));
                 }
             }
             repaint();
         }
    // causes every item to get updated.
    public void updatePanel() {
        Dimension dim = getSize();
        synchronized (panelItems) {
            for (PanelItem pi : panelItems) {
                pi.update(dim.width, dim.height);
            }
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        synchronized (panelItems) {
            for (PanelItem pi : panelItems) {
                pi.draw(g);
            }
        }
    }
}

测试类中的 KeyListener 类:

private class MyKeyListener extends KeyAdapter
{
    @Override public void keyTyped(KeyEvent e)
    {
        if(e.getKeyChar() == 'f') {
            panel.updatePanel();
            panel.repaint();
        }

        // other key events include reseting the panel &
        // creating a new panel, both of which are working.
    }
}

在上面的代码中,我被允许更改的唯一代码(以及我编写的唯一代码)是子类 MovingPanel 项。

4

0 回答 0