0

I have created a character in a JPeg file that I would like to use for a game that I'm creating. How do I add the file so that it can move around on the panel?

Here's a simplified version of my code, I used a 30x30 black square as a temporary version of a character.

// Initialize 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);
    } 
}

// Determines what to load and loads it. Also holds controls.

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

// Modifies all actions, and draws the levels.

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;
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Isaac
  • 29
  • 1
  • 7
  • Change `gc.fillRect(xChar,y,30,30);` to `gc.drawImage(..); // check docs for details`. And a few tips: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 4) Do more research into the specific problem before asking questions, it would be better to at least .. – Andrew Thompson Nov 12 '16 at 15:40
  • .. show an attempt to load an image, when asking how to draw one! Quote any JavaDocs of what you might think are relevant methods, link to tutorials, explain what you've done and where you are stuck. As it is, this question sound vaguely like *"Can you finish this for me?"* – Andrew Thompson Nov 12 '16 at 15:41

0 Answers0