图形未显示在可点击的界面上

时间:2013-07-23 21:40:30

标签: java image swing awt actionlistener

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

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();
    }
}

2 个答案:

答案 0 :(得分:3)

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

if (arg == "Up") {不是String比较在Java中的工作方式。相反,您应该使用"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。 AWT在大约13年前被Swing取代
  • 避免直接涂在顶层容器上。有很多层通常位于这些容器的顶部,您可以通过直接绘制它们来搞砸。相反,创建一个容器(如JPanel并在其上构建应用程序)。这将使其在未来更具可移植性和可重用性
  • 你必须致电super.paintXxx,除非你有非常非常好的理由不准备和准备好处理这些方法的所有工作。
  • 避免使用“魔法”数字。而是依靠绝对值。在这种情况下,您要绘制的容器的宽度和高度以及图像的大小。这将确保“中心”实际上是“中心”

以下示例演示了其中一些想法。基本上,我创建了一个ControlPane窗格,其中包含按钮和CupPaneCupPane是自包含的,负责绘制和定位杯子

enter image description here

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();
            }
        }
    }
}

您可能需要查看Performing Custom PaintingPainting in AWT and Swing了解更多详情

答案 1 :(得分: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);