Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quick Question about a basic game template  (Read 1225 times)
0 Members and 1 Guest are viewing this topic.
Offline Lucas_F98

Senior Newbie





« Posted 2012-06-24 09:38:59 »

Hello all i have two questions about a game template that i quickly wrote up... I am used to writing applets but i need to make a standalone game this time.

1-  How do i load an image? "getImage(getCodeBase(), "res/image.png");" Isn't working...
2-  How do you use a keyListener. The usual way for applets isn't working.

Help much appreciated Smiley

Here is the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
package com.ion.rpgnoname;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable, KeyListener{
   
   private static final long serialVersionUID = 1L;
   
   private static final int SCALE = 4;
   static int WIDTH = 125;
   static int HEIGHT = 125;
   static String NAME = "GAME UNNAMED" ;

   Image guy;
   
   int x = 0;
   int y = 0;
   
   Image dbImage;
   Graphics dbg;

   public void init () {
      setSize  (WIDTH, HEIGHT);
      setFocusable(true);
      setBackground(Color.BLACK);  
      addKeyListener(this);
                guy = getImage(getCodeBase(), "res/image.png");

   }
   
   public void start() {
      Thread th = new Thread(this);
      th.start();
   }

   public void run() {
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
      while(true){
         repaint();
         try {
            Thread.sleep(10);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }    
         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
      }  
   }

   public void paint(Graphics g) {
      g.setColor(Color.BLACK);
      g.fillOval(x, y, 12, 12);
                g.drawImage(guy, 40, 40, this);
   }
   
   public void update(Graphics g){
      if (dbImage == null)
       {
           dbImage = createImage (this.getSize().width, this.getSize().height);
           dbg = dbImage.getGraphics ();
       }

       dbg.setColor (getBackground ());
       dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

       dbg.setColor (getForeground());
       paint (dbg);

       g.drawImage (dbImage, 0, 0, this);
   }
   
   public static void main(String[] args){
      Game game = new Game();
      game.setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
      game.setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
      game.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
     
      JFrame frame = new JFrame(Game.NAME);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(new BorderLayout());
      frame.add(game);
      frame.pack();
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
     
      game.start();
   }

   public void keyPressed(KeyEvent e) {
      if(e.getKeyCode() == KeyEvent.VK_SPACE){
         x++;
         y++;
      }
   }
   public void keyReleased(KeyEvent e) {  
   }
   public void keyTyped(KeyEvent e) {  
   }
}
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #1 - Posted 2012-06-24 12:38:07 »

http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

google is your friend Cheesy.

and as a rule of thumb, if something is a method in the Applet class, it is probably a method in the JFrame class (addKeyListener for example).

as for reaching into a jar file, read this:
http://www.java-gaming.org/topics/file-path-that-works-in-both-applets-and-applications/26357/msg/230737/view.html

hope I answered your questions
h3ckboy
Offline Phibedy

Senior Member


Medals: 8



« Reply #2 - Posted 2012-06-24 17:11:20 »

1. For the pictures you should create a Spritemanager, if you use them often.

2. You get files in your workspace-direc. with "test-folder\\Test.png"
1  
2  
3  
4  
5  
6  
7  
8  
public static Icon getIcon(String path) {
      Icon icon = null;;
      try {
          icon = new ImageIcon(ImageIO.read(new File(path)));
      } catch (IOException e) {
      }
      return icon;
   }}


hope it helps Smiley

edit: Keylistner Cheesy
You may don´t want to write all in one class  Wink
I add a player object and give it a InputHandler,
1  
p = new Player("Spieler1", new InputHandler(keys.get(0) , keys.get(1) ,keys.get(2) , keys.get(3)));

keys is an array with the Integers of the keys.

In the InputHandler
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class InputHandler implements KeyListener, MouseListener, MouseWheelListener, MouseMotionListener{

   private int k1, k2, k3, k4;

   public InputHandler(int k1, int k2, int k3, int k4) {
      this.k1 = k1;
      this.k2 = k2;
      this.k3 = k3;
      this.k4 = k4;
     
      GamePanel.gp.addMouseListener(this);
      GamePanel.gp.addMouseWheelListener(this);
      GamePanel.gp.addMouseMotionListener(this);
     
   }[...]

you add all all the Listeners to the Panel. (I don´t add the Motion listener, because I don´t need the Listener in gamepanel)
You got any questions left, feel free to ask :-)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #3 - Posted 2012-06-24 17:23:41 »

1  
ImageIO.read(CLASS_NAME.class.getClassLoader().getResourceAsStream(FILEPATH));


-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-06-24 18:05:06 »

Never use ImageIcon to load images, use the way I outlined in that thread h3ckboy linked, which StonePickaxes also mentioned Smiley

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #5 - Posted 2012-06-24 18:12:26 »

I learned from the ImageIO king himself.

-Nathan

Check out my website!
Offline Phibedy

Senior Member


Medals: 8



« Reply #6 - Posted 2012-06-24 19:08:03 »

Thx for critics you fixed a bug in my code  Grin
Offline Lucas_F98

Senior Newbie





« Reply #7 - Posted 2012-06-24 22:15:22 »

Thanks,

Seems to be working now
Cheers for the help.

Lucas Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (26 views)
2014-09-07 01:10:19

mitcheeb (34 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!