在JFrame面板中添加图片

时间:2016-11-12 02:29:32

标签: java image swing jpanel awt

我在JPeg文件中创建了一个字符,我想将其用于我正在创建的游戏中。如何添加文件以便它可以在面板上移动?

这是我的代码的简化版本,我使用30x30黑色方块作为角色的临时版本。

//初始化GUI。

import javax.swing.*;

public class GameGUI
{
    public static void main(String[] args){
      JFrame frame = new JFrame("Game");

      frame.getContentPane().add(new Panel());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);
    } 
}

//确定要加载和加载的内容。还有控件。

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

public class Panel extends JPanel implements ActionListener
{
    public Actions actions;
    private Timer timer = new Timer(5, this);

    // Panel's dimensions.
    int WIDTH = 700,
        HEIGHT = 500;

    public Panel(){
        actions = new Actions();

        KeyboardListener listener = new KeyboardListener();
        addKeyListener(listener);
        setFocusable(true);
        requestFocusInWindow();
        setPreferredSize(new Dimension(WIDTH, HEIGHT));

        timer.start();
    }

    // Draws the background on this panel.
    public void paintComponent(Graphics gc){
        super.paintComponent(gc);
        actions.drawLevel(gc);          
    }

    public void actionPerformed(ActionEvent e){
        // Vertical Movement
        if(actions.isJump && !actions.isFall) actions.jump();
        else {
            actions.isFall=true;
            actions.fall();
        }
        // Horizontal Movement
        if(actions.isRight) actions.moveRight();
        if(actions.isLeft) actions.moveLeft();
        repaint();    
    }

    /**
     * The listener for the events generated by the keyboard.
     */
    private class KeyboardListener extends KeyAdapter
    { 
        public void keyPressed(KeyEvent event){
            if (event.getKeyCode() == KeyEvent.VK_RIGHT){
                actions.isRight=true;
               actions.isLeft=false;
            }else if (event.getKeyCode() == KeyEvent.VK_LEFT){
                actions.isLeft=true;
                actions.isRight=false;
            }
            if (event.getKeyCode() == KeyEvent.VK_UP){
                if(!actions.isFall){
                    actions.isJump=true;
                }
            }
        }

        public void keyReleased(KeyEvent event){
            if (event.getKeyCode() == KeyEvent.VK_RIGHT){
                actions.isRight=false;
            }else if (event.getKeyCode() == KeyEvent.VK_LEFT){
                actions.isLeft=false;
            }
            if (event.getKeyCode() == KeyEvent.VK_UP){
                actions.isJump=false;
            }
        }

        public void keyTyped(KeyEvent event){
            repaint();
        }
    } 
}

//修改所有操作,并绘制级别。

import java.awt.*;

public class Actions
{
    // Character movement
    int xChar=50,                       // Moves character
        y=470;                          // Height for character
    boolean isRight=false,
            isLeft=false;
    //  Jump variables
    static final int jumpHeight=-140;   // Jump Height
    int maxJumpHeight = y+jumpHeight,   // Stop Height for top
        stopHeight = y;                 // Height that jump stops at (bottom)
    boolean isJump=false,
            isFall=false;

    // Draws everything in the level
    public void drawLevel(Graphics gc){






        // DRAW THE CHARACTER AT POSITION (xChar,y)
        gc.setColor(Color.black);
        gc.fillRect(xChar,y,30,30);






        // Draws the platform
        gc.setColor(Color.black);
        gc.fillRect(0,500,800,30);
        gc.setColor(new Color(128,255,0));
        gc.fillRect(1, 501, 798, 28);
    }

    // Movements-right
    public void moveRight(){
        xChar+=5;
    }

    // Movements-left
    public void moveLeft(){
        xChar-=5;
    }

    // Up portion to jumping
    public void jump(){
        y-=7;
        if(y<=maxJumpHeight){
            isFall=true;
        }
    }

    // Falling/second portion to jumping
    public void fall(){
        y+=7;
        if(y>=stopHeight){
            y=stopHeight;
            isFall=false;
        }
    }
}

0 个答案:

没有答案