0

我有一个程序应该根据用户单击的按钮在屏幕上放置咖啡杯的图像,但是我的图像并不总是显示,并且当它显示时,它不会移动。有人可以帮我找出我的代码不正确的地方吗?谢谢!

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MoveIt extends Applet implements ActionListener

{
    //Declare Variables
    private Image cup;
    private Panel keyPad;
    public int top = 15;
    public int left = 15;
    private boolean foundKey;

    public void init()
    {
        cup = getImage(getDocumentBase(), "cup.gif");

        Canvas myCanvas = new Canvas();

        keyPad = new Panel();

        keysArray = new Button[5];


        //Create Buttons
        keysArray[0] = new Button("Up");
        keysArray[1] = new Button("Left");
        keysArray[2] = new Button("Right");
        keysArray[3] = new Button("Down");
        keysArray[4] = new Button("Center");

        keysArray[0].setActionCommand("Up");
        keysArray[1].setActionCommand("Left");
        keysArray[2].setActionCommand("Right");
        keysArray[3].setActionCommand("Down");
        keysArray[4].setActionCommand("Center");

        setBackground(Color.blue);

        //Sets the layout manager for the frame
        setLayout(new BorderLayout());

        //Sets the layout manager for the Panel
        keyPad.setLayout(new BorderLayout());

        //Add Buttons to keypad panel
        keyPad.add(keysArray[0], BorderLayout.NORTH);
        keyPad.add(keysArray[1], BorderLayout.WEST);
        keyPad.add(keysArray[2], BorderLayout.EAST);
        keyPad.add(keysArray[3], BorderLayout.SOUTH);
        keyPad.add(keysArray[4], BorderLayout.CENTER);

        add(myCanvas, BorderLayout.NORTH);
        add(keyPad, BorderLayout.SOUTH);

        //adds actionlistener to the buttons
        for(int i = 0; i < keysArray.length; i++)
           keysArray[i].addActionListener(this);
    }

    //puts image on the canvas
    public void paint( Graphics g )
    {
       g.drawImage( cup, left, top, this );
    }

    public void actionPerformed(ActionEvent e)
    {
        String arg = e.getActionCommand();


        if(arg == "Up") top = top - 15;
        if(arg == "Left") left = left - 15;
        if(arg == "Right") left = left + 15;
        if(arg == "Down") top = top + 15;
        if(arg == "Center")
        {
            top = 60;
            left = 125;
        }
        repaint();
    }
}
4

2 回答 2

3

这有很多问题,但让我们从根本原因开始......

if (arg == "Up") {不是StringJava 中比较的工作方式。相反,您应该使用"Up".equals(arg)或者如果您不关心案例"Up".equalsIgnoreCase(arg)

用户一次按下多个按钮的可能性也很小,因此,而不是

if ("Up".equals(arg)) {...}
if ("Left".equals(arg)) {...}

你应该使用一个if-else声明

if ("Up".equals(arg)) {
    //...
} else if ("Left".equals(arg)) {
    //..
} ...

更新

我将要推荐的一些内容已经由rendon 开始讨论,所以给他+1。

  • (个人)避免使用 AWT 并改用 Swing。大约 13 年前,AWT 被 Swing 取代
  • 避免直接在顶层容器上绘画。通常有很多层位于这些容器的顶部,您可以通过直接在它们上绘画来搞砸。相反,创建一个容器(如 aJPanel并在其上构建您的应用程序)。这将使其在未来更加便携和可重复使用
  • 你必须打电话super.paintXxx,除非你有非常非常好的理由不这样做并且准备好处理这些方法所做的所有工作。
  • 避免“魔术”数字。而是依靠绝对值。在这种情况下,您正在绘制的容器的宽度和高度以及图像的大小。这将确保“中心”实际上是“中心”

以下示例演示了其中一些想法。基本上,我创建了一个ControlPane窗格,其中包含按钮和CupPane. 是独立的CupPane,负责对杯子进行绘画和定位

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

public class MoveIt extends JApplet {

    public void init() {
        setLayout(new BorderLayout());
        add(new ControlPane());
    }

    public class CupPane extends JPanel {

        private Image cup;
        public int top = 15;
        public int left = 15;

        public CupPane() {
            cup = getImage(getDocumentBase(), "cup.png");
            setBackground(Color.BLUE);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(cup, left, top, this);
        }

        public void moveUp() {
            top -= 15;
            repaint();
        }

        public void moveLeft() {
            left -= 15;
            repaint();
        }

        public void moveRight() {
            left += 15;
            repaint();
        }

        public void moveDown() {
            top += 15;
            repaint();
        }

        public void center() {
            int width = getWidth();
            int height = getHeight();
            top = (height - cup.getHeight(this)) / 2;
            left = (width - cup.getWidth(this)) / 2;
        }
    }

    public class ControlPane extends JPanel {

        private JPanel keyPad;
        public int top = 15;
        public int left = 15;
        private boolean foundKey;
        private JButton[] keysArray;
        private CupPane cupPane;

        public ControlPane() {
            keyPad = new JPanel();
            keysArray = new JButton[5];
            cupPane = new CupPane();

            //Create Buttons
            keysArray[0] = new JButton("Up");
            keysArray[1] = new JButton("Left");
            keysArray[2] = new JButton("Right");
            keysArray[3] = new JButton("Down");
            keysArray[4] = new JButton("Center");

            keysArray[0].setActionCommand("Up");
            keysArray[1].setActionCommand("Left");
            keysArray[2].setActionCommand("Right");
            keysArray[3].setActionCommand("Down");
            keysArray[4].setActionCommand("Center");

            setBackground(Color.blue);

            //Sets the layout manager for the frame
            setLayout(new BorderLayout());

            //Sets the layout manager for the Panel
            keyPad.setLayout(new BorderLayout());

            //Add Buttons to keypad panel
            keyPad.add(keysArray[0], BorderLayout.NORTH);
            keyPad.add(keysArray[1], BorderLayout.WEST);
            keyPad.add(keysArray[2], BorderLayout.EAST);
            keyPad.add(keysArray[3], BorderLayout.SOUTH);
            keyPad.add(keysArray[4], BorderLayout.CENTER);

            add(cupPane, BorderLayout.CENTER);
            add(keyPad, BorderLayout.SOUTH);

            ActionHandler handler = new ActionHandler();
            //adds actionlistener to the buttons
            for (int i = 0; i < keysArray.length; i++) {
                keysArray[i].addActionListener(handler);
            }

        }

        protected class ActionHandler implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                String arg = e.getActionCommand();

                if ("Up".equals(arg)) {
                    cupPane.moveUp();
                } else if ("Left".equals(arg)) {
                    cupPane.moveLeft();
                } else if ("Right".equals(arg)) {
                    cupPane.moveRight();
                } else if ("Down".equals(arg)) {
                    cupPane.moveDown();
                } else if ("Center".equals(arg)) {
                    cupPane.center();
                }
                repaint();
            }
        }
    }
}

您可能想查看在 AWT 和 Swing中执行自定义绘画和绘画以了解更多详细信息

于 2013-07-23T23:45:17.573 回答
2

如果我理解你有这样的事情:

______________________
|         ___        | 
|        |img|       | 
|                    | 
|____________________| 
|  <-     ^    ->    | 
|_________v__________|

我建议你重新组织你的方法。

  1. 为绘图创建一个专用类(例如DrawingArea)。
  2. 为每个动作创建一个方法:moveLeft()moveRight()moveUp()等。
  3. 然后在方法中actionPerformed()

    public void actionPerformed(ActionEvent e)
    {
      Button source = (Button)e.getSource();
    
      if(source == buttonUp)         // keysArray[0] in your case
        drawingArea.moveUp();
      else if(source == buttonLeft)  // keysArrays[1]
        drawingArea.moveLeft();
      else if(source == buttonRight) // keysArrays[2]
        drawingArea.moveRight();
      // etc.
    

    }

  4. 在其中创建DrawingArea对象MoveIt并添加它。

    DrawingArea drawingArea = new DrawingArea();
    add(drawingArea, BorderLayout.CENTER);
    
于 2013-07-23T23:41:53.930 回答