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  
  Rotating an image - tower defense  (Read 248 times)
0 Members and 1 Guest are viewing this topic.
Offline sam

Junior Newbie





« Posted 2013-09-22 18:18:54 »

Hi all,  Smiley

I'm new to java2d and so I decided to do a Tower Defense game.
So it's very basic for now, in fact I show one tower and one ennemy, the first thing I wanted to get working is aiming the turret to the ennemy...
The problem is that the turret is spinning, instead of just smoothly following the ennemy.
Here's my code, may look a bit weird since it's bits of code from all over.
I guess the problem is coming from "gameRender()".


Complete 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  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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

public class GamePanel extends JPanel implements Runnable
{
   private static int DEFAULT_FPS = 80;
   private static int period;
   
   private static final int PWIDTH = 500; // size of panel
  private static final int PHEIGHT = 400;
   private Thread animator; // for the animation
  private boolean running = false; // stops the animation

   private Graphics dbg;
   private Image dbImage = null;
   
   BufferedImage img = null;
   AffineTransform at = new AffineTransform();
   
   double x = 300;
   double y = 300;
   
   int x_turret = 250;
   int y_turret = 250;

   public GamePanel()
   {
      setBackground(Color.white); // white background
     setPreferredSize( new Dimension(PWIDTH, PHEIGHT));
     
      setFocusable(true);
      requestFocus();
     
      try {
         img = ImageIO.read(new File("canon.png"));
      } catch (IOException e) {}
     
      /* Active rendering instead. */
      setDoubleBuffered(false);
   }

   public void addNotify()
   {
      super.addNotify();
      startGame();
   }
   private void startGame()
   {
      if (animator == null || !running)
      {
         animator = new Thread(this);
         animator.start();
      }
   }
   
   public void stopGame()
   { running = false; }

   public void run()
   {
      long beforeTime, timeDiff, sleepTime;
     
      beforeTime = System.currentTimeMillis();
     
      running = true;
      while(running)
      {
         gameUpdate();
         gameRender();
         paintScreen();
         
         timeDiff = System.currentTimeMillis() - beforeTime;
         sleepTime = period - timeDiff; // time left in this loop
       
         if (sleepTime <= 0) // update/render took longer than period
           sleepTime = 5; // sleep a bit anyway

         try {
            Thread.sleep(sleepTime);
         }
         catch(InterruptedException ex){}
         
         beforeTime = System.currentTimeMillis();
      }
      System.exit(0); // so enclosing JFrame/JApplet exits
  }
   
   private void
   paintScreen
   ()
   // actively render the buffer image to the screen
  {
      Graphics g;
      try {
         g = this.getGraphics(); // get the panel’s graphic context
        if ((g != null) && (dbImage != null))
            g.drawImage(dbImage, 0, 0, null);
         g.dispose();
      }
      catch (Exception e)
      { System.out.println("Graphics context error: " + e); }
   }
   
   
   private void gameUpdate()
   {
      x += 0.1;
   }
   
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      if (dbImage != null)
         g.drawImage(dbImage, 0, 0, null);
   }
   
   private void gameRender()
   // draw the current frame to an image buffer
  {
      if (dbImage == null)
      {
         dbImage = createImage(PWIDTH, PHEIGHT);
         
         if (dbImage == null)
         {
            System.out.println("dbImage is null");
            return;
         }
         
         dbg = dbImage.getGraphics();
      }
     
      dbg.setColor(Color.gray);
      dbg.fillRect (0, 0, PWIDTH, PHEIGHT);
     
      dbg.setColor(Color.blue);
      dbg.fillRect((int)x, (int)y, 10, 10);

        AffineTransform at1 = new AffineTransform();
       
        double yourAngle = Math.atan2(y-(double)y_turret, x-(double)x_turret) * 180 / Math.PI;
       
        at1.translate(x_turret-img.getWidth(), y_turret-img.getHeight());
        at1.rotate(yourAngle, img.getWidth()/2, img.getHeight()/2);
       
        ((Graphics2D)dbg).drawImage(img, at1, null);

   }
   
      public static void main(String args[])
      {
        period = (int) 1000.0/DEFAULT_FPS;

        GamePanel gPanel = new GamePanel();

        // create a JFrame to hold the timer test JPanel
       JFrame app = new JFrame("Game Panel");
        app.getContentPane().add(gPanel, BorderLayout.CENTER);
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        app.pack();
        app.setResizable(false);  
        app.setVisible(true);
     }
}


I'm new to this forum, so tell me if I did something wrong Smiley

Sam
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #1 - Posted 2013-09-22 19:44:56 »

Line 153, delete the * 180 / Math.Pi, you need to keep the angle in radians (I believe)
Offline sam

Junior Newbie





« Reply #2 - Posted 2013-09-22 19:49:07 »

Damn ! That was the problem...
Thanks alot Smiley
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 (21 views)
2014-09-12 09:08:26

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

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

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

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

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

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

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

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

mitcheeb (37 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!