Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: How to show the bullets on: 2013-06-19 14:17:24
Read the links on game loops for a start. It's been linked twice now.

Bad example:

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  
class Game {
bulletsArray = new ArrayList <Bullet>();

public void gameLoop() {
   while (true) {

      update();
      render();

      // sleep
      try {
         Thread.sleep(1);
      } catch() {}
   }
}

public void update() {
   for (Bullet b : bulletsArray) {
      b.update();
   }
}

public void render(Graphics g) {
   // draw things
   for (Bullet b : bulletsArray) {
       b.draw(g);
   }
}
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
class Bullet {
   int x, y, xspeed, yspeed;
   update() {
      x += xspeed;
      y += yspeed;
   }
   draw(Graphics g) {
      g.drawImage(bulletImage, x, y, null);
   }
}



Hi i think i am confuse on this, can you please tell why is not firing the bullet,sorry.. i am still learning on this game,
i hope you can help me.

Thank you in advance.

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  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
package mygame.airforce;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.*;
import java.util.List;


public class Mygame implements Runnable{
    private Image ship;
    private boolean up;
    private boolean down;
    private boolean right;
    private boolean left;
    private int posX = 300;
    private int posY = 560;
    Graphics2D g2d;
    Thread gameloop;
    Bullet bb = new Bullet();
    Mypanel p = new Mypanel();
    public Mygame(){

        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Mypanel panel = new Mypanel();

        panel.setPreferredSize(new Dimension(600,600));
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);




    }

    class Mypanel extends JPanel{

        private List <Bullet> bullets = new ArrayList <Bullet>();

        public Mypanel() {

            try {
                ship = ImageIO.read(getClass().getResource("/mygame/airforce/images/spaceship.png"));
            } catch (IOException exc) {
                exc.printStackTrace();
            }

            bullets = new ArrayList <Bullet>();

            bullets.add(new Bullet(posX,posY));

            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false),
                    "arrowUP");
            getActionMap().put("arrowUP",
                    arrowUP);


            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true),
                    "arrowUPrelease");
            getActionMap().put("arrowUPrelease",
                    arrowUPrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false),
                    "arrowRight");
            getActionMap().put("arrowRight",
                    arrowRight);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true),
                    "arrowRightrelease");
            getActionMap().put("arrowRightrelease",
                    arrowRightrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false),
                    "arrowLeft");
            getActionMap().put("arrowLeft",
                    arrowLeft);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true),
                    "arrowLeftrelease");
            getActionMap().put("arrowLeftrelease",
                    arrowLeftrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false),
                    "arrowDown");
            getActionMap().put("arrowDown",
                    arrowDown);

            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true),
                    "arrowDownrelease");
            getActionMap().put("arrowDownrelease",
                    arrowDownrelease);

        }


        Action arrowUP = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = true;
                posY-=5;
                repaint();

            }
        };

        Action arrowUPrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = false;

            }
        };


        Action arrowDown = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = true;
                posY+=5;
                repaint();

            }
        };

        Action arrowDownrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = false;
            }
        };

        Action arrowRight = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = true;
                posX+=5;
                repaint();

            }
        };

        Action arrowRightrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = false;



            }
        };


        Action arrowLeft = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = true;
                posX-=5;
                repaint();

            }
        };


        Action arrowLeftrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = false;



            }
        };



        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.fillRect(0,0,600,600);
            g2d.setColor(Color.BLACK);
            g2d.drawImage(ship, posX, posY, null);

        }


          public void gameUpdate(){
               for(Bullet b:bullets){
                  new Bullet(getX(),getY());
               }


          }






    }


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


    public void run(){
        Thread t = Thread.currentThread();

        while(t==gameloop){
            try{
                Thread.sleep(5);
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
            p.gameUpdate();
            bb.draw(g2d);
            p.repaint();
        }

    }

    class Bullet{
        final double speed = 2;
        int x,y;
        public Bullet(){}
         Bullet(int x, int y){
           this.x = x;
           this.y = y;

         }


        public void setX(int x){
            this.x = x;
        }


        public void setY(int y){
            this.y=y;
        }

        public int getX(){
            return x;
        }

       public int getY(){
           return y;
       }



       public void moveForward(){
           y-=speed;

       }

       public void draw(Graphics g){
           Graphics2D gg = (Graphics2D)g;
           gg.drawRect(0,0,1,1);
           gg.setColor(Color.orange);

       }

    }



    public static void main(String []args) {

          new Mygame();


    }
}
2  Game Development / Newbie & Debugging Questions / Re: How to show the bullets on: 2013-06-18 18:41:30
You should have a game loop that is calling the repaint() method every frame.

See here: http://www.java-gaming.org/topics/game-loops/24220/view.html


Okay but i have not yet draw my bullet, or how do i make my bullet color orange or red?just like my ship i draw in the paintComponent...can you please help me.I am confuse.
3  Game Development / Newbie & Debugging Questions / Re: How to show the bullets on: 2013-06-18 14:02:23
Hi, This is my code i am confuse how to draw the bullet image, can you please help me on this.I get trouble on this.

Thank you Smiley

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  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.*;
import java.util.List;


public class Mygame{
    private Image ship;
    private boolean up;
    private boolean down;
    private boolean right;
    private boolean left;
    private int posX = 300;
    private int posY = 560;
    Graphics2D g2d;

    public Mygame(){

        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Mypanel panel = new Mypanel();
        panel.setPreferredSize(new Dimension(600,600));
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);




    }

    class Mypanel extends JPanel{

        private List <Bullet> bullets;
        public Mypanel() {

            try {
                ship = ImageIO.read(getClass().getResource("/images/spaceship.png"));
            } catch (IOException exc) {
                exc.printStackTrace();
            }

            bullets = new ArrayList <Bullet>();

            bullets.add(new Bullet(posX,posY));

            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false),
                    "arrowUP");
            getActionMap().put("arrowUP",
                    arrowUP);


            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true),
                    "arrowUPrelease");
            getActionMap().put("arrowUPrelease",
                    arrowUPrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false),
                    "arrowRight");
            getActionMap().put("arrowRight",
                    arrowRight);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true),
                    "arrowRightrelease");
            getActionMap().put("arrowRightrelease",
                    arrowRightrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false),
                    "arrowLeft");
            getActionMap().put("arrowLeft",
                    arrowLeft);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true),
                    "arrowLeftrelease");
            getActionMap().put("arrowLeftrelease",
                    arrowLeftrelease);


            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false),
                    "arrowDown");
            getActionMap().put("arrowDown",
                    arrowDown);

            getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true),
                    "arrowDownrelease");
            getActionMap().put("arrowDownrelease",
                    arrowDownrelease);

        }


        Action arrowUP = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = true;
                posY-=5;
                repaint();

            }
        };

        Action arrowUPrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = false;

            }
        };


        Action arrowDown = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = true;
                posY+=5;
                repaint();

            }
        };

        Action arrowDownrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                down = false;
            }
        };

        Action arrowRight = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = true;
                posX+=5;
                repaint();

            }
        };

        Action arrowRightrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                right = false;



            }
        };


        Action arrowLeft = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = true;
                posX-=5;
                repaint();

            }
        };


        Action arrowLeftrelease = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                left = false;



            }
        };



        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.fillRect(0,0,600,600);
            g2d.setColor(Color.BLACK);
            g2d.drawImage(ship, posX, posY, null);

        }


   

    } //End of Class Mypanel



    class Bullet{
        final double speed = 2;
        int x,y;
         Bullet(int x, int y){
           this.x = x;
           this.y = y;

         }


        public void setX(int x){
            this.x = x;
        }


        public void setY(int y){
            this.y=y;
        }

        public int getX(){
            return x;
        }

       public int getY(){
           return y;
       }



       public void moveForward(){
           y-=speed;

       }

    }


    public static void main(String []args) {
            new Mygame();

    }
}
4  Game Development / Newbie & Debugging Questions / Re: How to show the bullets on: 2013-06-16 17:32:13
@JonJova, Thank you for this links this might help me to improve my game development skill.
5  Game Development / Newbie & Debugging Questions / Re: How to show the bullets on: 2013-06-16 17:30:26
Hello  Smiley
I'm quite a beginner myself so the way I show you might not be perfect.
You should make a Bullet class that holds the bullet information, like speed, position,image etc. and then in the player/spaceship you have a list of those bullets (List<Bullet> bulList) that you will add a new bullet to every time you fire using the bulList.add(new Bullet(param)). Now if I were you I'd make the class something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
class Bullet{
    final double SPEED = 2;
    double x,y;

    Bullet(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void move(){
        //bullet goes up
        y -= SPEED;
    }

}


Now that is assuming that you are making a game like space invaders where the bullet goes up. Also you will need to draw the elements in that list every frame. Hope that helps  Grin

Hi thank you for your quick reply and giving some points,okay i will let you know if this will solve my problem. Smiley
6  Game Development / Newbie & Debugging Questions / How to show the bullets on: 2013-06-16 16:16:17
Hi, Can i ask some help,this is the first time that i make my simple game...it's like the spaceship flying i am just using png file for this,I already know how to move my spaceship using keybindings, my problem is that i don't have idea how to draw simple bullet when i pressed the forward key in the keyboard,and also i don't know the logic how the bullet will go forward when fired up...I apologize for this silly question but i am just beginner in developing simple game...I hope someone will help me to achieve this...


I really appreciated those will help, Thank you in advance.
7  Game Development / Newbie & Debugging Questions / Re: How to load my image on: 2013-06-13 19:12:06
For smooth movement you should just use a Game Loop (take a look at this article about them) instead of only drawing when you receive user input. Just update the ship location when you get input and only draw in your game loop.


Hi, i think this is not correct...because my code is useless


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
 public void gameUpdate(){
       

           if(up==true){
                g2d.drawImage(ship, posX, posY, null);
           }


    }


    public void run() {
        Thread t = Thread.currentThread();
        while (t == gameloop) {
            try {
                Thread.sleep(5);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            gameUpdate();
        }
    }




I have this code on top of it,and this is the reason why it moves...and my spaceship moves okay,but i want to use the gameloop what you have said and the link you provided but it seems like i am confuse of it...

1  
2  
3  
4  
5  
6  
7  
 Action arrowUP = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up = true;
                posY-=5; // I just put this line here
                repaint();
            }
        };
8  Game Development / Newbie & Debugging Questions / Re: How to load my image on: 2013-06-13 18:52:39
For smooth movement you should just use a Game Loop (take a look at this article about them) instead of only drawing when you receive user input. Just update the ship location when you get input and only draw in your game loop.


Hi,  I tried but  i am confuse how to update my ship location when press the arrows key


I put this outside in  Mypanel class ,but how can i draw the image,i am confuse...please help me....

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
 public void gameUpdate(){
       

           if(up==true){

           }


    }


    public void run() {
        Thread t = Thread.currentThread();
        while (t == gameloop) {
            try {
                Thread.sleep(5);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            gameUpdate();
        }
    }
9  Game Development / Newbie & Debugging Questions / Re: How to load my image on: 2013-06-11 19:57:25


Hi, Thank you again Smiley
10  Game Development / Newbie & Debugging Questions / Re: How to load my image on: 2013-06-10 19:12:52
Stop sending a width and height of 5 to the drawImage() method. You're using drawImage(Image img, int x, int y, int width, int height, ImageObserver io) when all you really need is drawImage(Image img, int x, int y, ImageObserver io). Again, check the JavaDocs for stuff like this or when you aren't sure how something works.

Hi, Can i ask last question, is this normal that when i move my image or pressing arrow up my image has a shadow something like the image is delaying to display when i pressing the arrow up.how can i run my image very smooth like no shadow.

I hope you understand what i mean.

Thank you in advance.
11  Game Development / Newbie & Debugging Questions / Re: How to load my image on: 2013-06-10 18:43:23



Thank you so much i got it now Smiley
12  Game Development / Newbie & Debugging Questions / Re: How to load my image on: 2013-06-10 18:34:41
I think it might have to do with the way you're loading your image. Using ImageIO instead of the Toolkit's getImage() fixed it for me. Try this instead:
1  
2  
3  
4  
5  
try {
   ship = ImageIO.read(getClass().getResource("images/spaceship.png");
} catch(IOException exc) {
   exc.printStackTrace();
}


EDIT: Also there isn't really a point in overriding a JFrame and a JPanel. You're better off only overriding a JPanel and then adding it to a frame. Something like this:
1  
2  
3  
4  
5  
6  
7  
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel panel = new MyPanel();
panel.setPreferredSize(new Dimension(600,600));
frame.add(panel);
frame.pack();
frame.setVisible(true);



Hi, it's working now, but the problem is my image is too small.because of this
1  
g2d.drawImage(ship,posX,posY,5,5,this);


how can i get the default width and height of my image,,..I am using this.getWidth(),this.getHeight()..but my image is very big.  Emo
13  Game Development / Newbie & Debugging Questions / Re: Cannot run my program on: 2013-06-10 18:24:15
it does not seem like you have a jframe to put the panel on. a jpanel is like a layer that goes ontop of a jframe while the jframe is the actual window that pops up. just read up on jframes and jpanels. btw just extending jframe wont work
His code works just fine (besides not opening), although it might not be the optimal way.

You're overriding addNotfiy() (which is kind of an important method, check the java docs) and having it to do nothing. This should fix it:
1  
2  
3  
public void addNotify() {
   super.addNotify();
}



Hi thank you for the spot, ahm okay i will update my code...can you please help me why my image ship is not displaying ?
14  Game Development / Newbie & Debugging Questions / Re: Cannot run my program on: 2013-06-10 18:17:25
it does not seem like you have a jframe to put the panel on. a jpanel is like a layer that goes ontop of a jframe while the jframe is the actual window that pops up. just read up on jframes and jpanels. btw just extending jframe wont work

Hi my frame is now loading but the image of my ship won't display,how can i display the image ?

Thank you in advance

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  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;



public class Mygame extends JFrame{
    private Image ship;
    private Boolean running;
    private Mypanel mypanel;
    private boolean up =false;
    private boolean down = false;
    private boolean right =false;
    private boolean left = false;
    private int posX = 10;
    private int posY = 10;

    public Mygame(){
        setTitle("Demo Game");

        mypanel = new Mypanel();

        add(mypanel);

    }

    class Mypanel extends JPanel {

          public Mypanel(){

             ship = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/spaceship.png"));

              getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0),
                      "arrowUP");
              getActionMap().put("arrowUP",
                      arrowUP);


              getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),
                      "arrowRight");
              getActionMap().put("arrowRight",
                      arrowRight);


              getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),
                      "arrowLeft");
              getActionMap().put("arrowLeft",
                      arrowLeft);


              getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0),
                      "arrowDown");
              getActionMap().put("arrowDown",
                      arrowDown);






          }


        Action arrowUP = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up=true;
                repaint();


            }
        };



        Action arrowDown = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {

                down = true;
                repaint();


            }
        };

        Action arrowRight = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {

            }
        };

        Action arrowLeft = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {

            }
        };


        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

           g2d.drawImage(ship,posX,posY,5,5,this);

            if(up==true){
               posX+=5;
            }

            if(left=true){
                posY-=5;
            }
        }

        @Override
        public Dimension getPreferredSize(){
            return new Dimension(400,400);
        }

    } //End of Class Mypanel





    public static void main(String []a){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                 Mygame mygame = new Mygame();

                 mygame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                 mygame.pack();
                 mygame.setVisible(true);


            }
        });
    }
}
15  Game Development / Newbie & Debugging Questions / Re: Cannot run my program on: 2013-06-10 18:15:20
Hi, can i ask why you said that just extending JFrame wont work? why?
16  Game Development / Newbie & Debugging Questions / How to load my image on: 2013-06-10 17:36:38
Hi, I need some help, why is it my code will exit directly? it will not show the frame,can you help me please how to fix this.

Thank you in advance.


here is my 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  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;



public class Mygame extends JFrame{
    private Image ship;
    private Boolean running;
    private Mypanel mypanel;
    private boolean up =false;
    private boolean down = false;
    private boolean right =false;
    private boolean left = false;
    private int posX = 10;
    private int posY = 100;

    public Mygame(){
        setTitle("Demo Game");

        mypanel = new Mypanel();
        getContentPane().add(mypanel);

    }

    class Mypanel extends JPanel {

          public Mypanel(){

              ship = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/spaceship.png"));

              getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0),
                      "arrowUP");
              getActionMap().put("arrowUP",
                      arrowUP);


              getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),
                      "arrowRight");
              getActionMap().put("arrowRight",
                      arrowRight);


              getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),
                      "arrowLeft");
              getActionMap().put("arrowLeft",
                      arrowLeft);


              getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0),
                      "arrowDown");
              getActionMap().put("arrowDown",
                      arrowDown);






          }


        Action arrowUP = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                up=true;
                repaint();


            }
        };



        Action arrowDown = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {

                down = true;
                repaint();


            }
        };

        Action arrowRight = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {

            }
        };

        Action arrowLeft = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {

            }
        };


        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);



            if(up==true){
               posX+=5;
            }

            if(left=true){
                posY-=5;
            }
        }



    } //End of Class Mypanel


        public void addNotify(){

        }

        public void startGame(){

        }

        public void stopRun(){
            running = false;
        }

        public void run(){
            running = true;

            while(running==true){

                    repaint();


                try {
                    Thread.sleep(5);
                }
                catch(InterruptedException ex){}

            }

            System.exit(0);
        }



    public static void main(String []a){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                 Mygame mygame = new Mygame();

                 mygame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                 mygame.setVisible(true);
                 mygame.pack();

            }
        });
    }
}
17  Discussions / General Discussions / Re: How to make firing ball in the tank on: 2013-05-26 06:00:49
How is the firing performed? Does the ball only shoot forward, or should it shoot to where the mouse is?


Yes,i will start first in forward mode shooting. Smiley
18  Discussions / General Discussions / Re: How to make firing ball in the tank on: 2013-05-26 05:59:38
Hi, i hope this can help


Hi, Thank you for the example. Smiley
19  Discussions / General Discussions / Re: Need help for reference book on: 2013-05-25 14:36:57
@gmbal, Thank you Smiley
20  Discussions / General Discussions / How to make firing ball in the tank on: 2013-05-25 14:36:07
Hi to everyone,...I hope someone will guide or give some tips and ideas...How do you guys make the firing ball of the tank ? I mean when the tank fires to his opponent...is that firing ball created in graphic2D?,I want to make battle tank just as simple as i can...

Thank you in advance.  Smiley
21  Discussions / General Discussions / Re: Need help for reference book on: 2013-05-24 03:55:08
Hi thank you for the quick reply...Okay i will read this,thank you again. Smiley
22  Discussions / General Discussions / Need help for reference book on: 2013-05-24 03:41:31
Hi, To all java-gaming masters,..This is my first time to post here,..Can i ask some help what good reference book for developing games in java.because i want to develop a java game but i have no idea where to start, My plan is to make a battle tank,I know lots of here already achieve this,but i want to make my own in order to explore or expand my knowledge in developing java games...

Thank you in advance and more power to you always.  Smiley
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!