Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Moving an object  (Read 973 times)
0 Members and 1 Guest are viewing this topic.
Offline MegaWolt

Senior Newbie




Java games rock!


« Posted 2005-04-22 19:13:56 »

Hi guys, I'm pretty new to programming, but I really want to make a game. I know this is a really easy question, but it is late, and I don't want to spend the next couple of hours doing something wrong. I want to move an object around with the keyboard, is this the best way of doing it? And how do I implements double buffering to avoid flicker?
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  
/**
 * Simple test of moving an objekt
 *
 * @author Lau
 * @version 0.1
 */


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

public class AnimationTest extends BasicJFrame implements Runnable
{
    private Ellipse2D.Double cirkel;
    private double xpos = 250;
    private double ypos = 250;
    private double rad = 25;
    private double xspeed;
    private double yspeed;
    private Thread animationThread;

   
    public AnimationTest()
    {
        super("Testarea", 500, 500);
        getContentPane().setBackground(Color.WHITE);
       
        addKeyListener(listener);
       
        xspeed = 0;
        yspeed = 0;
       
        setVisible (true);
       
        animationThread = new Thread(this);
        animationThread.start();
       
    }
   

   
    public void run()
    {
        while(Thread.currentThread() == animationThread)
        {
            xpos = xpos + xspeed;
            ypos = ypos + yspeed;
           
            repaint();
            try
            {
                Thread.sleep(10);  
            }
            catch(InterruptedException u)
            {
                System.err.println(u.toString());
            }
        }
    }  
   
    KeyListener listener = new KeyListener()
    {
        public void keyPressed(KeyEvent e)
        {
            int code = e.getKeyCode();
           
            switch(code)
            {
                case KeyEvent.VK_UP: yspeed = -3; break;
                case KeyEvent.VK_LEFT: xspeed = -3; break;
                case KeyEvent.VK_RIGHT: xspeed = +3; break;
                case KeyEvent.VK_DOWN: yspeed = +3; break;
            }
           
        };
       
        public void keyTyped(KeyEvent e)
        {};
     
        public void keyReleased(KeyEvent e)
        {
            int code = e.getKeyCode();
           
            switch(code)
            {
                case KeyEvent.VK_UP: yspeed = 0; break;
                case KeyEvent.VK_LEFT: xspeed = 0; break;
                case KeyEvent.VK_RIGHT: xspeed = 0; break;
                case KeyEvent.VK_DOWN: yspeed = 0; break;
            }
 
        };
    };
   
    public void paint(Graphics g)
    {
        super.paint(g);
       
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.RED);
       
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON);
       
        cirkel = new Ellipse2D.Double(xpos - rad, ypos - rad, 2*rad, 2*rad);
            g2.fill(cirkel);
    }
}
Huh
Offline Mr_Light

Senior Member




shiny.


« Reply #1 - Posted 2005-04-23 14:53:58 »

are you using swing or awt?

if you do use swing I sugjest reading up on the painting model at java.sun.com

in awt you create an image of the size of the bit you want to be dubble buffered. ( http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#createImage(int, int) )
get the graphics context of the image ( http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Image.html#getGraphics() ) draw on the returned context as you normaly would. then use g.drawImage() to paint the 'buffer'-image.

init stuff (do once)
1  
2  
buffer = component.createImage( width, height );
gB = buffer.getGraphics();


change your paint:
1  
2  
3  
4  
5  
6  
7  
8  
gB.drawString("hello world",10,10);
// or something like
paintAbleObject.move(x,y);
paintAbleObject.draw(gB);
g.drawImage( buffer,0,0, observer);
// clear the buffer
gB.setColor(background);
gB.fillRect(0,0,width,heigth);


gB.dispose() wenn your done with it.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Jeff

JGO Coder




Got any cats?


« Reply #2 - Posted 2005-04-24 06:48:24 »

OR

You go into full-screen exclusive mode and do it the way real (non-applet) games do by flipping buffers.  Mike martak has an excelletn set of tutorials here:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/

If my "Scroller" exampel is stil lfloating around it shows how you use it in a failry simple isometric game engine.

OR

you do what Cas does and do all your 2D as mono-planar 3D through JOGL or LWJGL.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member




shiny.


« Reply #3 - Posted 2005-04-24 16:54:46 »

ah thats where my non-gaming specialization, is appearend. fun reading material.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline MegaWolt

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-04-24 20:02:31 »

Thanks I'll take a good look at the links 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.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!