Drawing Image to Screen *URGENT*

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 1669 Views - Last Post: 03 November 2010 - 07:56 PM Rate Topic: -----

#1 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:00 PM

Man, I feel like such a n00b begging, but my weekend wound up being wasted, and I didn't get to start this assignment until just a few short hours ago (like 4). If I post the seemingly relevant code, will you tell me why my Image isn't showing up? Thanks in advance. It's due at 5 til midnight... tonight. >_> I even skipped all my classes to do this. I know the code is incomplete and broken, but it should at least show the player icon and move around a little bit.

Main
package cs241project3;

/**
 *
 * @author Erica Boyer
 * @date 27 Oct 2010
 *
 * This project is to create a shooter-type game. This game is supposed to force
 * us to think in terms of objects by using graphics to torture us into doing
 * something that should take longer than 2 weeks.
 *
 * Specification Details:
 * 1.) Must have a game panel inside of a frame
 * 2.) Must have a way for the user to "fire" or "shoot"; a clickable button
 *     at bare minimum. If more complex, must give instructions.
 * 3.) Shots must be fired from a visible launcher, stationary or mobile, that
 *     Shoots in the direction the launcher is facing.
 * 4.) Movable targets that stays in the screen for at least 5 seconds, and
 *     at least one target on the screen every 30 seconds.
 * 5.) The shot should take about 3 seconds to get from one side of the screen
 *     to the other.
 * 6.) When shot collides with target, there must be both a graphic action that
 *     takes place and an addition to the player's score.
 * 7.) Current points must be displayed.
 * 8.) Must have 2 different types of targets with different size/speed/etc.
 *
 * As far as secondary specifications go, I'm going to go with B: small target
 * being double points of big target. 
 *
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new GameWindow();

    } // End main method

} // End Main class




GameWindow
package cs241project3;

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

/**
 *
 * @author Erica Boyer
 * @date 27 Oct 2010

 *
 * This class handles the GUI frame for the game.
 */
public class GameWindow extends JFrame {

    private final int WIDTH = 1000;
    private final int HEIGHT = 750;
    private final boolean VISIBLE = true;
    private final boolean RESIZABLE = false;
    private final String TITLE = "Generic \"Link Beats Bad People Up\""; // Oh god, the creativity, it hurts

    GamePanel gamePanel;
    HUDPanel hud;

    public GameWindow() {

        super();
        this.setTitle(TITLE);
        this.setSize(WIDTH, HEIGHT);
        this.setVisible(VISIBLE);
        this.setResizable(RESIZABLE);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.center(this);

        gamePanel = new GamePanel();
        hud = new HUDPanel();

        this.add(gamePanel, BorderLayout.CENTER);
        this.add(hud, BorderLayout.SOUTH);

        // infinite animation loop, program halts when window is closed.
        while (true) {
            pause();
            
            gamePanel.repaint();
        } // End while

    } // End constructor

    /** Helper routine to center a frame on the screen (will cause problems if
        frame is bigger than the screen!)
     */
    public static void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();

        int w = frame.getWidth();
        int h = frame.getHeight();

        int x = center.x - w/2, y = center.y - h/2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    } // End center method

    /**
     * Pause command used to control the speed of the bouncing ball animation.
     * Currently pauses for 20 ms.  Use smaller values for faster animation and
     * vice versa.
     */
    public static void pause()
    {
        try {
            Thread.sleep(20); // pause for 20 ms
        } // End try

        catch(Exception e) {
            System.out.println(e);
            e.printStackTrace();
        } // End catch
    } // End pause method

} // End GameWindow class




GamePanel

package cs241project3;

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

/**
 *
 * @author Erica Boyer
 * @date 27 Oct 2010

 *
 * This class handles the panel that displays the actual game.
 */
public class GamePanel extends JPanel implements ActionListener, KeyListener {

    Player player;
    ArrayList bullets = new ArrayList();

    public GamePanel() {

        super();
        this.setBackground(Color.BLACK);
        player = new Player();
        this.requestFocus();

    } // End constructor

    @Override
    public void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);                         // set color black
        g.fillRect(0,0,this.getWidth(),this.getHeight()); // paint background
        player.paint(g);
    } // End paintComponent method
    
    //~~~~~~Interface Methods~~~~~~~~~
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    } // End actionPerformed method

    public void keyTyped(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_KP_DOWN)
            player.move(Player.Direction.DOWN, this.getWidth(), this.getHeight());
        else if(e.getKeyCode() == KeyEvent.VK_KP_UP)
            player.move(Player.Direction.UP, this.getWidth(), this.getHeight());
        else if(e.getKeyCode() == KeyEvent.VK_KP_LEFT)
            player.move(Player.Direction.LEFT, this.getWidth(), this.getHeight());
        else if(e.getKeyCode() == KeyEvent.VK_KP_RIGHT)
            player.move(Player.Direction.RIGHT, this.getWidth(), this.getHeight());
        repaint();
    } // End keyTyped method

    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_KP_DOWN)
            player.move(Player.Direction.DOWN, this.getWidth(), this.getHeight());
        else if(e.getKeyCode() == KeyEvent.VK_KP_UP)
            player.move(Player.Direction.UP, this.getWidth(), this.getHeight());
        else if(e.getKeyCode() == KeyEvent.VK_KP_LEFT)
            player.move(Player.Direction.LEFT, this.getWidth(), this.getHeight());
        else if(e.getKeyCode() == KeyEvent.VK_KP_RIGHT)
            player.move(Player.Direction.RIGHT, this.getWidth(), this.getHeight());
        repaint();
    } // End keyPressed method

    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    } // End keyReleased method

} // End GamePanel class



HUDPanel

package cs241project3;

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

/**
 *
 * @author Erica Boyer
 * @date 27 Oct 2010

 *
 * This class handles the HUD (Heads Up Display) that displays the score,
 * lives, amount of ammo left, and it will contain the optional fire button
 * and possibly the directional buttons.
 */
public class HUDPanel extends JPanel implements ActionListener {

    JButton fire;

    public HUDPanel() {

        super();
        this.setBackground(Color.BLACK);
        this.setBorder(BorderFactory.createTitledBorder("HUD"));

        fire = new JButton("Fire!");

        this.add(fire);

    } // End constructor


    //~~~~~~~~Interface Methods~~~~~~~~~~~~~

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

} // End HUDPanel class



Player

package cs241project3;

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

/**
 *
 * @author Erica Boyer
 * @date 31 Oct 10

 *
 * This class handles the code pertaining to the player.
 */
public class Player {

    ImageIcon playerImage;
    private int life;
    private final int velocity  = 10;
    private int xCoord, yCoord;
    public enum Direction {UP, DOWN, LEFT, RIGHT}
    private Direction direction;
    private int num;
    
    public Player() {

        setLife(3);
        this.num = 1;
        playerImage = new ImageIcon("images/link_up_1.png");
        this.direction = Direction.UP;

    } // End constructor
    
    public void setLife(int life) {
        this.life = life;
    } // End setLife method

    public int getLife() {
        return this.life;
    } // End getLife method

    public int getVelocity() {
        return this.velocity;
    } // End getLife method
   
    public void setxCoord(int xCoord) {
        this.xCoord = xCoord;
    } // End setxCoordMethod

    public int getxCoord() {
        return this.xCoord;
    } // End getxCoord method

    public void setyCoord(int yCoord) {
        this.yCoord = yCoord;
    } // End setyCoordMethod

    public int getyCoord() {
        return this.yCoord;
    } // End getyCoord method

    public void setDirection(Direction direction) {
        this.direction = direction;
    } // End getDirection method

    public Direction getDirection() {
        return this.direction;
    } // End getDirection method

    public void move(Direction d, int panelWidth, int panelHeight) {
        String filepath;

        // Toggles the pic of the player to similate walking and junk.
        if(num == 1) num = 2;
        else if(num == 2) num = 1;
        else System.err.println("Error!");

        switch(d) {
            case UP:
                filepath = "images/link_up_" + num + ".png";
                playerImage = new ImageIcon(filepath);
                setxCoord(getyCoord() - velocity);
                if(getyCoord() <= 0) setyCoord(0);
                break;
            case DOWN:
                filepath = "images/link_down_" + num + ".png";
                playerImage = new ImageIcon(filepath);
                setxCoord(getyCoord() + velocity);
                if(getyCoord() + playerImage.getIconHeight() >= panelHeight)
                    setyCoord(panelHeight);
                break;
            case LEFT:
                filepath = "images/link_left_" + num + ".png";
                playerImage = new ImageIcon(filepath);
                setxCoord(getxCoord() - velocity);
                if(getxCoord() <= 0)
                    setyCoord(0);
                break;
            case RIGHT:
                filepath = "images/link_right_" + num + ".png";
                playerImage = new ImageIcon(filepath);
                setxCoord(getxCoord() + velocity);
                if(getxCoord() + playerImage.getIconWidth() >= panelWidth)
                    setyCoord(panelWidth);
                break;
        } // End switch

    } // End move method

    public void paint(Graphics g) {
        g.drawImage(playerImage.getImage(), getxCoord(), getyCoord(),
                    64, 64, null);
    } // End paint method

    /*
     * This nested class handles the code for the "bullets". Of course they're 
     * not really bullets per se, but magical projectiles coming from Link's
     * sword. Cba to fool with the bow and arrow. There was a sword sprite
     * already drawn, so... yea.
     */
    private class Bullet {

        ImageIcon bullet;
        String filepath;

        public Bullet() {
            filepath = "images/fire_" + getDirection() + ".png";
            bullet = new ImageIcon(filepath);
        } // End constructor

    } // End Bullet class

} // End Player class



I zipped and attached the images so you can compile. I'm pretty sure you guys know me enough to trust me not to be a douche and attach viruses. When calling the drawImage method, I said 64x64 even though the image is 32x32 because from what I got from it was that it would automatically scale it. It's kinda small. :( Thanks mucho.

Edit: oy, forgot to take out some personal info :blush:

Attached File(s)


This post has been edited by NeoTifa: 01 November 2010 - 07:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Drawing Image to Screen *URGENT*

#2 cdog5000  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 79
  • Joined: 31-October 09

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:11 PM

Works fine for me, Just does not move anywhere, is that your problem?
Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:23 PM

I don't see him at all.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:24 PM

Your .zip file is invalid
Confim thow that the paint() method of Player is called and drwaing are performed at 0,0
Was This Post Helpful? 0
  • +
  • -

#5 cdog5000  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 79
  • Joined: 31-October 09

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:25 PM

View PostNeoTifa, on 01 November 2010 - 04:23 PM, said:

I don't see him at all.

try checking your working directory for the images? Just make sure the images are accessable via '/images/bla.png'
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:26 PM

??? Either typos, translation error, or.... idk. The zip is a fold images that I put next to all my classes.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,128
  • Joined: 27-December 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:30 PM

Since you use NetBeans, perhaps this is a case of File location errors. Can you post a screenshot of your file system for this project in NetBeans? Also, where is the images folder located? When I deal with files in NetBeans, I just stick them in the project folder (but not in a subfolder), and you can reference them from your source code just by their names (ie., img1.jpg, test.txt, etc.).
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:35 PM

Attached Image
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:38 PM

Images have to be there in the same folder that contains cs241project3

/cs241project3
/images
Was This Post Helpful? 1
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 05:47 PM

Still nothing. Wtf?!
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 06:06 PM

Post your directories hierarchy in Window Explorer
Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 06:14 PM

Here, I put the folder back where it was.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 06:31 PM

Netbeans is weird. Try this:

/images/imagename.png instead of images/imagename.png
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 06:39 PM

Look at my post #9

Images have to be there in the same folder that contains cs241project3

/cs241project3
/images

your images directory has to be one level up
in the same directory than /cs241project3
Was This Post Helpful? 0
  • +
  • -

#15 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Re: Drawing Image to Screen *URGENT*

Posted 01 November 2010 - 06:40 PM

Nope. I even tried to switch the folder around again. v___v
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3