如何将玩家添加到迷宫中

时间:2016-01-25 03:28:17

标签: java swing maze

我构建了一个迷宫并制作了一个玩家类。当我编译它时,我只能看到迷宫,而不是玩家。我尝试了不同的技术,但它对我来说仍然无效。

为了看到播放器并能够移动它,我到底需要做些什么?

另外,有没有办法调整迷宫并使其大小或与帧相同?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;  // Needed for ActionListener
import javax.swing.event.*;  // Needed for ActionListener
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

class www extends JFrame
{
    static Maze maze = new Maze ();
    static Timer t;

    //======================================================== constructor
    public www ()
    {
        // 1... Create/initialize components
        // 2... Create content pane, set layout
        JPanel content = new JPanel ();        // Create a content pane
        content.setLayout (new BorderLayout ()); // Use BorderLayout for panel
        JPanel north = new JPanel ();
        north.setLayout (new FlowLayout ()); // Use FlowLayout for input area

        DrawArea board = new DrawArea (500, 500);

        // 3... Add the components to the input area.

        content.add (north, "North"); // Input area
        content.add (board, "South"); // Output area

        // 4... Set this window's attributes.
        setContentPane (content);
        pack ();
        setTitle ("MAZE");
        setSize (490, 500);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo (null);           // Center window.
    }

    public static void main (String[] args)
    {
        www window = new www ();
        window.setVisible (true);
        player p = new player ();
        JFrame jf = new JFrame ();
        //jf.setTitle("Tutorial");
        //jf.setSize(600,400);
        jf.setVisible(true);
    }

    class DrawArea extends JPanel
    {
        public DrawArea (int width, int height)
        {
            this.setPreferredSize (new Dimension (width, height)); // size
        }

        public void paintComponent (Graphics g)
        {
            maze.show (g); // display current state of colony
        }
    }
}

class Maze 
{
    private int grid [][];

    public Maze ()
    {
        int [][] maze = 
            { {1,0,1,1,1,1,1,1,1,1,1,1,1},
                {1,0,1,0,1,0,1,0,0,0,0,0,1},
                {1,0,1,0,0,0,1,0,1,1,1,0,1},
                {1,0,0,0,1,1,1,0,0,0,0,0,1},
                {1,0,1,0,0,0,0,0,1,1,1,0,1},
                {1,0,1,0,1,1,1,0,1,0,0,0,1},
                {1,0,1,0,1,0,0,0,1,1,1,0,1},
                {1,0,1,0,1,1,1,0,1,0,1,0,1},
                {1,0,0,0,0,0,0,0,0,0,1,0,1},
                {1,1,1,1,1,1,1,1,1,1,1,0,1}};

        grid = maze;

    }

    public void show (Graphics g)
    {
        for (int row = 0 ; row < grid.length ; row++)
            for (int col = 0 ; col < grid [0].length ; col++)
            {
                if (grid [row] [col] == 1) // life
                    g.setColor (Color.black);
                else
                    g.setColor (Color.white);
                g.fillRect (col * 30 + 30, row * 30 + 30, 30, 30); // draw life form
            }
    }
    public class player extends JPanel implements ActionListener,KeyListener
    {
        Timer tm = new Timer(5,this);
        private int x = 0; 
        private  int y = 0;
        private int velX = 0 ;
        private int velY = 0;
        public void player ()
        {
            tm.start();
            addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
        }

        public void paintComponent (Graphics g)
        {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(20,30,50,50);
        }

        public void actionPerformed(ActionEvent e)
        {
            x = x+velX;
            y = y +velY;
            repaint();
        }

        public void keyPressed(KeyEvent e)
        {
            int c =  e.getKeyCode();
            if(c == KeyEvent.VK_LEFT)
            {
                velX = -1;
                velY = 0;
            }
            if (c == KeyEvent.VK_UP)
            {
                velX = 0;
                velY = -1;
            }
            if( c==KeyEvent.VK_RIGHT)
            {
                velX = 1;
                velY = 0;
            }
            if(c==KeyEvent.VK_DOWN)
            {
                velX = 0;
                velY = 1;
            }
        }

        public void keyTyped(KeyEvent e){}

        public void keyReleased(KeyEvent e){}


    }


}

0 个答案:

没有答案