Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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 / Variables Problem on: 2007-12-23 14:28:07
Hi,
I am having a problem with a networked game I am making, I was unsure if the post should have been in here or networking. Sorry if it is in the wrong place. When a new player connects they are given a set of variables. I am struggling to do this. This is what I came up with:

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  
   public void processdata()
   {
      try
      {
         if(whatinformation != null)
         {
            int isa = whatinformation.indexOf("a");
            int isb = whatinformation.indexOf("b");
            int isc = whatinformation.indexOf("c");
            int isd = whatinformation.indexOf("d");
            what = whatinformation.substring(isa+1,isb);
            if(what.equals("#"))
            {
               square1xpos = whatinformation.substring(isb+1,isc);
               square1ypos = whatinformation.substring(isc+1,isd);
               square1x = Integer.parseInt(square1xpos);
               square1y = Integer.parseInt(square1ypos);
               amountofplayers += 1;
            }
            if(what.equals("~"))
            {
               square1xpos = whatinformation.substring(isb+1,isc);
               square1ypos = whatinformation.substring(isc+1,isd);
               square1x = Integer.parseInt(square1xpos);
               square1y = Integer.parseInt(square1ypos);
            }
         }
      }
      catch ( Exception err )
      {
         err.printStackTrace();
      }
   }


This was a bad mistake when using this code to paint them to screen:

1  
2  
3  
4  
5  
6  
7  
8  
      public void paintComponent(Graphics g)
      {
         g.drawImage(square1, squarex, squarey, this);
         for(int i=0; i < amountofplayers; i++)
         {
            g.drawImage(square2, square1x, square1y, this);
         }
      }


This was causing the players to jump all over the place because they were sharing the same variables. I have tried using an arraylist but couldn't manage to update the variables inside it. This made the players not see eachother move. I have tried an ordinary array but couldn't do it that way either. I would be thankful if anyone could help me using some sort of technique to fix this problem. Each player is suppose to get a different set of variables to manage their position in the world. I think an array would do the trick but when I tried it didn't work. I probably made a silly mistake though.
Thanks.
2  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-12-02 22:10:44
Thanks!!! You solved it!
3  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-30 17:24:34
But the sleep time is set by the user, this will define on how fast the program will do Robot.press()
How else could I do it to stop 100% CPU usage?
4  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-30 16:00:20
But the sleep time is set by the user, this will define on how fast the program will do Robot.press()
How else could I do it to stop 100% CPU usage?
5  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-29 21:17:52
Still 100% CPU usage!
6  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-29 21:09:48
Do I still do this bit?

1  
setIconImage(icon);
7  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-29 19:37:38
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  
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  
import java.awt.*;
import java.awt.Robot.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.applet.AudioClip;
import java.net.*;

public class ronansautotyperuk extends JFrame implements Runnable, ActionListener
{
   JTextField comments = new JTextField("Enter your message to be typed here", 25);
   JTextField delaytime = new JTextField("Enter miliseconds between each letter press", 23);
   JButton starttyping = new JButton("Start Typing");
   JButton stoptyping = new JButton("Stop Typing");
   
   Thread runner;
   Boolean gameover = false;
   Boolean running = true;
   Boolean istyping = false;
   int pausefor;
   int realpausefor = 7;
   int numb = 0;
   String stringOfCharsToType;
   String delayfor;
   int delayforint;
   Image icon;
   
   public ronansautotyperuk()
   {
      super("Ronan's Autotyper UK - Version 1.0");
      setSize(800, 75);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setDefaultLookAndFeelDecorated(true);
      setVisible(true);
      setResizable(false);
      Container contentArea = getContentPane();
      contentArea.setBackground(Color.red);
      FlowLayout flowManager = new FlowLayout();
      contentArea.setLayout(flowManager);
     
      contentArea.add(comments);
      contentArea.add(delaytime);
      contentArea.add(starttyping);
      contentArea.add(stoptyping);
      starttyping.addActionListener(this);
      stoptyping.addActionListener(this);
      setContentPane(contentArea);
     
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      icon = toolkit.getImage( getClass().getResource("icon.gif") );
      MediaTracker mediatracker = new MediaTracker(this);
      mediatracker.addImage(icon, 0);
      try
      {
         mediatracker.waitForID(0);
      }
      catch(InterruptedException interruptedexception)
      {
         System.out.println("Download Error");
      }
      setIconImage(icon);
      start();
   }
   
   public void actionPerformed(ActionEvent event)
   {
      if(event.getSource() == starttyping) starttypingnow();
      if(event.getSource() == stoptyping) stoptypingnow();
   }
   
   public void start()
   {
      setVisible(true);
      requestFocus();
      running = true;
      runner = new Thread(this);
      runner.start();
   }
   
   public void stopgame()
   {
      if(running != null)
      running = null;
   }
   
   public void destroy()
   {
     
   }
   
   public void starttypingnow()
   {
      pausefor = 2000;
      istyping = false;
      stringOfCharsToType = comments.getText();
      delayfor = delaytime.getText();
      delayforint = Integer.parseInt(delayfor);
   }
   
   public void stoptypingnow()
   {
      istyping = false;
      numb = 0;
   }
   
   public void typenow()
   {
      if(numb < stringOfCharsToType.length() && istyping)
      {
         char thing = stringOfCharsToType.charAt(numb);
         numb++;
         if(thing == 'a')
         {
            try
            {
               Robot bot = new Robot();
               bot.keyPress(KeyEvent.VK_A);
               bot.keyRelease(KeyEvent.VK_A);
            }
            catch(Exception exc)
            {
               System.out.println(exc);
            }
         }
      }
      if(numb >= stringOfCharsToType.length())
      {
         try
         {
            Robot bot = new Robot();
            bot.keyPress(KeyEvent.VK_ENTER);
            bot.keyRelease(KeyEvent.VK_ENTER);
         }
         catch(Exception exc)
         {
            System.out.println(exc);
         }
         numb = 0;
      }
   }
   
   public void gameupdate()
   {
      if(!gameover)
      {
         if(istyping)
         {
            typenow();
         }
      }
   }
   
   public void run()
   {
      while (running)
      {
         repaint();
         gameupdate();
         try
         {
            if(pausefor != 2000)
            {
               pausefor = delayforint;
               Thread.sleep(pausefor);
            }
            if(pausefor == 2000)
            {
               Thread.sleep(pausefor);
               pausefor = realpausefor;
               istyping = true;
            }
         }
         catch(InterruptedException e) {}
      }
   }

   public static void main (String[] args)
   {
      ronansautotyperuk eg = new ronansautotyperuk();
   }
}
8  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-29 19:32:44
In the Run() method?
9  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-29 17:29:14
This came up when I pressed Ctrl + Break:


2007-11-29 16:25:08
Full thread dump Java HotSpot(TM) Client VM (1.6.0_03-b05 mixed mode, sharing):

"DestroyJavaVM" prio=6 tid=0x00296000 nid=0x600 waiting on condition [0x00000000
..0x0090fd4c]
   java.lang.Thread.State: RUNNABLE

"Thread-2" prio=6 tid=0x0306cc00 nid=0x9a8 runnable [0x033cf000..0x033cfd14]
   java.lang.Thread.State: RUNNABLE
        at java.awt.EventQueue.postEvent(Unknown Source)
        at javax.swing.SystemEventQueueUtilities.queueComponentWorkRequest(Unkno
wn Source)
        at javax.swing.SystemEventQueueUtilities.queueComponentWorkRequest(Unkno
wn Source)
        at javax.swing.RepaintManager.addDirtyRegion0(Unknown Source)
        at javax.swing.RepaintManager.addDirtyRegion(Unknown Source)
        at javax.swing.JFrame.repaint(Unknown Source)
        at java.awt.Component.repaint(Unknown Source)
        at ronansautotyperuk.run(ronansautotyperuk.java:1477)
        at java.lang.Thread.run(Unknown Source)

"AWT-EventQueue-0" prio=6 tid=0x03056400 nid=0x4b0 runnable [0x02fce000..0x02fcf
a14]
   java.lang.Thread.State: RUNNABLE
        at sun.java2d.loops.DrawGlyphListLCD.DrawGlyphListLCD(Native Method)
        at sun.java2d.pipe.LCDTextRenderer.drawGlyphList(Unknown Source)
        at sun.java2d.pipe.GlyphListPipe.drawString(Unknown Source)
        at sun.java2d.pipe.ValidatePipe.drawString(Unknown Source)
        at sun.java2d.SunGraphics2D.drawString(Unknown Source)
        at sun.swing.SwingUtilities2.drawString(Unknown Source)
        at sun.swing.SwingUtilities2.drawStringUnderlineCharAt(Unknown Source)
        at javax.swing.plaf.metal.MetalButtonUI.paintText(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonUI.paintText(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonUI.paint(Unknown Source)
        at javax.swing.plaf.metal.MetalButtonUI.update(Unknown Source)
        at javax.swing.JComponent.paintComponent(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        - locked <0x22ea14b8> (a java.awt.Component$AWTTreeLock)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        - locked <0x22ea14b8> (a java.awt.Component$AWTTreeLock)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JLayeredPane.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        - locked <0x22ea14b8> (a java.awt.Component$AWTTreeLock)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
        at javax.swing.RepaintManager.paint(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
        at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
        at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
        at java.awt.Container.paint(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

"AWT-Windows" daemon prio=6 tid=0x02a8a800 nid=0xd84 runnable [0x02f0f000..0x02f
0fa94]
   java.lang.Thread.State: RUNNABLE
        at sun.awt.windows.WToolkit.eventLoop(Native Method)
        at sun.awt.windows.WToolkit.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

"AWT-Shutdown" prio=6 tid=0x02a89c00 nid=0xa80 in Object.wait() [0x02ebf000..0x0
2ebfb14]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x22ea1718> (a java.lang.Object)
        at java.lang.Object.wait(Object.java:485)
        at sun.awt.AWTAutoShutdown.run(Unknown Source)
        - locked <0x22ea1718> (a java.lang.Object)
        at java.lang.Thread.run(Unknown Source)

"Java2D Disposer" daemon prio=10 tid=0x02a88400 nid=0xfb0 in Object.wait() [0x02
e6f000..0x02e6fb94]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x22ea17b0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        - locked <0x22ea17b0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        at sun.java2d.Disposer.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

"Low Memory Detector" daemon prio=6 tid=0x02a5c800 nid=0x7dc runnable [0x0000000
0..0x00000000]
   java.lang.Thread.State: RUNNABLE

"CompilerThread0" daemon prio=10 tid=0x02a57c00 nid=0x364 waiting on condition [
0x00000000..0x02d0f61c]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x02a56800 nid=0x98 runnable [0x00000000..0
x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x02a55400 nid=0xad8 waiting on condition
 [0x00000000..0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x02a51400 nid=0x314 in Object.wait() [0x02c1f000.
.0x02c1fa94]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x22ea1a08> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        - locked <0x22ea1a08> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

"Reference Handler" daemon prio=10 tid=0x02a4cc00 nid=0x50c in Object.wait() [0x
02bcf000..0x02bcfb14]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x22ea14b0> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:485)
        at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
        - locked <0x22ea14b0> (a java.lang.ref.Reference$Lock)

"VM Thread" prio=10 tid=0x02a4b800 nid=0xaf4 runnable

"VM Periodic Task Thread" prio=10 tid=0x02a77400 nid=0xeac waiting on condition


JNI global references: 1384

Heap
 def new generation   total 960K, used 262K [0x22960000, 0x22a60000, 0x22e40000)

  eden space 896K,  28% used [0x22960000, 0x229a0ef0, 0x22a40000)
  from space 64K,   4% used [0x22a40000, 0x22a40c58, 0x22a50000)
  to   space 64K,   0% used [0x22a50000, 0x22a50000, 0x22a60000)
 tenured generation   total 4096K, used 569K [0x22e40000, 0x23240000, 0x26960000
)
   the space 4096K,  13% used [0x22e40000, 0x22ece670, 0x22ece800, 0x23240000)
 compacting perm gen  total 12288K, used 211K [0x26960000, 0x27560000, 0x2a96000
0)
   the space 12288K,   1% used [0x26960000, 0x26994ca8, 0x26994e00, 0x27560000)
    ro space 8192K,  62% used [0x2a960000, 0x2ae614a8, 0x2ae61600, 0x2b160000)
    rw space 12288K,  52% used [0x2b160000, 0x2b7a7278, 0x2b7a7400, 0x2bd60000)
10  Game Development / Performance Tuning / Re: 100% CPU Usage!!!!!!! on: 2007-11-28 23:43:54
How do I get rid of this infinite loop?
11  Game Development / Performance Tuning / 100% CPU Usage!!!!!!! on: 2007-11-28 22:50:41
Hi,
When I start my program the CPU Usage shoots up to 100%. I ran the java profiler (java -Xprof myprog).
These were the results:


Flat profile of 0.60 secs (38 total ticks): main

  Interpreted + native   Method
 12.5%     0  +     4    java.io.WinNTFileSystem.canonicalizeWithPrefix0
  6.3%     2  +     0    sun.nio.cs.ISO_8859_1$Decoder.decodeArrayLoop
  6.3%     0  +     2    java.io.FileInputStream.open
  6.3%     2  +     0    java.util.Arrays.copyOfRange
  6.3%     0  +     2    sun.awt.Win32GraphicsEnvironment.initDisplay
  3.1%     0  +     1    sun.awt.windows.WWindowPeer.setFocusableWindow
  3.1%     0  +     1    java.lang.ClassLoader$NativeLibrary.load
  3.1%     0  +     1    java.io.RandomAccessFile.open
  3.1%     0  +     1    sun.nio.ch.FileDispatcher.read0
  3.1%     0  +     1    java.lang.ClassLoader.findBootstrapClass
  3.1%     1  +     0    java.lang.Character.valueOf
  3.1%     1  +     0    java.util.HashSet.add
  3.1%     1  +     0    sun.java2d.loops.GraphicsPrimitive.makeUniqueID
  3.1%     1  +     0    java.awt.Dialog.<clinit>
  3.1%     1  +     0    javax.swing.plaf.basic.BasicTextUI.installKeyboardActio
ns
  3.1%     1  +     0    sun.java2d.loops.GraphicsPrimitiveMgr.registerGeneral
  3.1%     0  +     1    sun.awt.windows.WDesktopProperties.init
  3.1%     1  +     0    javax.swing.JComponent.setUI
  3.1%     1  +     0    javax.swing.plaf.metal.DefaultMetalTheme.getUserTextFon
t
  3.1%     1  +     0    java.io.BufferedReader.readLine
  3.1%     1  +     0    sun.font.CMap$CMapFormat4.<init>
  3.1%     1  +     0    java.lang.String.toLowerCase
  3.1%     1  +     0    java.util.Properties$LineReader.readLine
 93.8%    16  +    14    Total interpreted

  Thread-local ticks:
 15.8%     6             Blocked (of total)
  6.3%     2             Class loader


Flat profile of 6.55 secs (5 total ticks): Image Fetcher 0

  Interpreted + native   Method
 33.3%     1  +     0    java.util.StringTokenizer.nextToken
 33.3%     1  +     0    sun.util.calendar.ZoneInfo.getOffsets
 33.3%     1  +     0    sun.awt.image.GifImageDecoder.parseImage
100.0%     3  +     0    Total interpreted

  Thread-local ticks:
 40.0%     2             Blocked (of total)



After this it just hung, so I had to close it.
Please help me find the problem that is making my program use all of the CPU!
Thanks.
12  Game Development / Networking & Multiplayer / Re: Distributing a java program on: 2007-11-25 20:13:31
Thanks!!! Finally an answer!!!
13  Game Development / Networking & Multiplayer / Re: Distributing a java program on: 2007-11-25 11:32:55
What about swing components? Can you sell your app if text boxes and buttons are in it??
Thanks.
14  Game Development / Networking & Multiplayer / Re: Distributing a java program on: 2007-11-24 19:38:03
So it is completely legal to sell a program programmed in java? There are no limitations???
Thanks.
15  Game Development / Networking & Multiplayer / Distributing a java program on: 2007-11-24 15:39:01
Hi,
Are you allowed to sell your own software you made using java, or do you have to buy a separate license?

What does this mean?

2. LICENSE TO USE. Subject to the terms and conditions of this Agreement, including, but not limited to the Java Technology Restrictions of the Supplemental License Terms, Sun grants you a non-exclusive, non-transferable, limited license without license fees to reproduce and use internally Software complete and unmodified for the sole purpose of running Programs. Additional licenses for developers and/or publishers are granted in the Supplemental License Terms.

Thanks.
16  Game Development / Newbie & Debugging Questions / Re: Active Rendering on: 2007-11-23 17:34:59
Thanks!!! It works now. What are the advantages of using active rendering apart from full controll over the repainting of your app?
Thanks again!
17  Game Development / Newbie & Debugging Questions / Re: Active Rendering on: 2007-11-22 22:18:59
Here is all of 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  
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  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
import java.applet.*;
import java.awt.*;
import java.applet.AudioClip;
import java.awt.geom.*;
import java.awt.event.*;

public class area51 extends Applet implements Runnable, MouseListener, FocusListener
{
   // Ints
  int myfps = 50;
   int pausefor;
   int onesecond = 1000;
   
   // Booleans
  private volatile boolean gameover = true;
   private volatile boolean running = true;
   private volatile boolean loading;
   private volatile boolean ispaused;
   private volatile boolean credits = false;
   private volatile boolean firstlevel = false;
   private volatile boolean firstlevelbegin = false;
   
   // Strings
 
   
   // Floats
  float size = 25f;
   
   // Fonts
  Font titlefont;
   
   // Arrays
 
   
   //Images
  private Image dbImage;
   private Graphics dbg;
   Image backgroundimage;
   Image blankbackground;
   Image firstlevelimage;

   // Sounds
  AudioClip xfiles;
   
   // Threads
  Thread runner;

   public void init()
   {
      pausefor = onesecond/myfps;
      xfiles = getAudioClip(getCodeBase(), "xfiles.mid");
      backgroundimage = getImage(getDocumentBase(), "area51.gif");
      blankbackground = getImage(getDocumentBase(), "blankbackground.gif");
      firstlevelimage = getImage(getDocumentBase(), "firstlevelimage.gif");
      MediaTracker mediatracker = new MediaTracker(this);
      mediatracker.addImage(backgroundimage, 0);
      mediatracker.addImage(blankbackground, 1);
      mediatracker.addImage(firstlevelimage, 2);
      try
      {
         loading = true;
         mediatracker.waitForID(0);
         mediatracker.waitForID(1);
         mediatracker.waitForID(2);
         loading = false;
      }
      catch(InterruptedException interruptedexception)
      {
         System.out.println("Download Error");
      }
   
      try
      {
         ClassLoader cl = this.getClass().getClassLoader();
         titlefont = Font.createFont(Font.TRUETYPE_FONT,
         cl.getResourceAsStream("Vera.ttf"));
         titlefont = titlefont.deriveFont(size);
      }
      catch(Exception e)
      {
         System.out.println(e);
      }
     
      addMouseListener(this);
      addFocusListener(this);
   }
   
   public void start()
   {
      setVisible(true);
      requestFocus();
      running = true;
      runner = new Thread(this);
      runner.start();
      xfiles.loop();
   }
   
   public void stopgame()
   {
      if(running != false)
      running = false;
   }
   
   public void destroy()
   {
      xfiles.stop();
   }
   
   public void focusGained(FocusEvent focusevent)
   {
      ispaused = false;
   }

   public void focusLost(FocusEvent focusevent)
   {
      ispaused = true;
   }
   
   public void mousePressed(MouseEvent event)
   {
      int mousex = event.getX();
      int mousey = event.getY();
     
      if(mousex >= 310 && mousex <= 470 && mousey >= 300 && mousey <= 335 && gameover && !credits && !loading)
      {
         gameover = false;
         firstlevel = true;
      }
      if(mousex >= 310 && mousex <= 470 && mousey >= 336 && mousey <= 370 && gameover && !credits && !loading)
      {
         credits = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 225 && mousey <= 260 && gameover && credits && !loading)
      {
         credits = false;
      }
      if(mousex >= 150 && mousex <= 340 && mousey >= 250 && mousey <= 285 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         firstlevelbegin = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 300 && mousey <= 335 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         gameover = true;
      }
   }
   public void mouseReleased(MouseEvent event)
   {
     
   }
   public void mouseClicked(MouseEvent event)
   {
     
   }
   public void mouseEntered(MouseEvent event)
   {
     
   }
   public void mouseExited(MouseEvent event)
   {
     
   }
   
   public void gameupdate()
   {
     
   }
   
   public void run()
   {
      long  starttime;
      while(running)
      {
         starttime=System.currentTimeMillis();
         gameupdate();
         gamerender();
         paintscreen();
         try
         {
            starttime += pausefor;
            Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
         }
         catch(InterruptedException interruptedexception) { }
      }
   }
   
   public void gamerender()
   {
      if(dbImage == null)
      {
         dbImage = createImage(getSize().width, getSize().height);
         if(dbImage == null)
         {
            System.out.println("dbImage is null");
            return;
         }
         else
         dbg = dbImage.getGraphics();
         dbg.setColor(getBackground());
         dbg.fillRect(0, 0, getSize().width, getSize().height);
         
         if(loading)
         {
            loading(dbg);
         }
         if(gameover && !credits && !loading)
         {
            titlescreen(dbg);
         }
         if(gameover && credits && !loading)
         {
            credits(dbg);
         }
         if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
         {
            firstlevel(dbg);
         }
         if(ispaused)
         {
            paused(dbg);
         }
      }
   }
   
   public void paintscreen()
   {
      Graphics g;
      try
      {
         g = this.getGraphics();
         if((g != null) && (dbImage != null))
         g.drawImage(dbImage, 0, 0, null);
         Toolkit.getDefaultToolkit().sync();
         g.dispose();
      }
      catch (Exception e)
      {
         System.out.println("Graphics context error: " + e);
      }
   }
   
   public void loading(Graphics g)
   {
      g.drawString("Loading...", 200, 200);
   }
   
   public void titlescreen(Graphics g)
   {
      g.drawImage(backgroundimage, 0, 0, this);
      g.setFont(titlefont);
      g.setColor(Color.green);
      g.drawString("Start Game", 320, 325);
      g.drawString("Credits", 320, 360);
      Graphics2D painter2D = (Graphics2D) g;
      Rectangle2D.Float box = new Rectangle2D.Float(310F, 300F, 160F, 35F);
      Rectangle2D.Float box1 = new Rectangle2D.Float(310F, 335F, 160F, 35F);
      painter2D.draw(box);
      painter2D.draw(box1);
   }
   
   public void firstlevel(Graphics g)
   {
      g.drawImage(blankbackground, 0, 0, this);
      g.setColor(Color.green);
      g.drawString("Your mission is to spy on Area 51.", 40, 70);
      g.drawString("To do this you will have to get", 40, 100);
      g.drawString("past the security and pretend you", 40, 130);
      g.drawString("are an engineer for the latest UFO!", 40, 160);
      g.drawString("Move with the arrow keys.", 80, 220);
      g.drawString("Start Mission! ", 160, 275);
      g.drawString("Back", 210, 325);
      Graphics2D painter2D = (Graphics2D) g;
      Rectangle2D.Float box = new Rectangle2D.Float(150F, 250F, 190F, 35F);
      Rectangle2D.Float box1 = new Rectangle2D.Float(200F, 300F, 80F, 35F);
      painter2D.draw(box);
      painter2D.draw(box1);

      if(firstlevelbegin && !firstlevel && !gameover && !loading && !credits)
      {
         g.drawImage(firstlevelimage, -250, -500, this);
      }
   }
   
   public void credits(Graphics g)
   {
      g.drawImage(blankbackground, 0, 0, this);
      g.setColor(Color.green);
      g.drawString("CREDITS", 190, 100);
      g.drawString("Ronan McCann-Jackson", 100, 150);
      g.drawString("Back", 210, 250);
      Graphics2D painter2D = (Graphics2D) g;
      Rectangle2D.Float box = new Rectangle2D.Float(200F, 225F, 80F, 35F);
      painter2D.draw(box);
   }
   
   public void paused(Graphics g)
   {
      g.setColor(Color.yellow);
      g.drawString("The game is paused!", 110, 20);
   }
}

Thanks.
18  Game Development / Newbie & Debugging Questions / Active Rendering on: 2007-11-22 18:39:35
Hi,
I recently made a game using the Repaint() method for my game animation, but then I read a chapter from Killer Game Programming about active rendering. I tried to implement this into my game code. It displays everything ok but my game no longer responds to my action listener. I have no idea what is wrong. There is no runtime or compilation errors.

Here is a snippet of my Active Rendering method.

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  
   public void mousePressed(MouseEvent event)
   {
      int mousex = event.getX();
      int mousey = event.getY();
     
      if(mousex >= 310 && mousex <= 470 && mousey >= 300 && mousey <= 335 && gameover && !credits && !loading)
      {
         gameover = false;
         firstlevel = true;
      }
      if(mousex >= 310 && mousex <= 470 && mousey >= 336 && mousey <= 370 && gameover && !credits && !loading)
      {
         credits = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 225 && mousey <= 260 && gameover && credits && !loading)
      {
         credits = false;
      }
      if(mousex >= 150 && mousex <= 340 && mousey >= 250 && mousey <= 285 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         firstlevelbegin = true;
      }
      if(mousex >= 200 && mousex <= 280 && mousey >= 300 && mousey <= 335 && !gameover && !credits && !loading && firstlevel && !firstlevelbegin)
      {
         firstlevel = false;
         gameover = true;
      }
   }
   public void mouseReleased(MouseEvent event)
   {
     
   }
   public void mouseClicked(MouseEvent event)
   {
     
   }
   public void mouseEntered(MouseEvent event)
   {
     
   }
   public void mouseExited(MouseEvent event)
   {
     
   }
   
   public void gameupdate()
   {
     
   }
   
   public void run()
   {
      long  starttime;
      while(running)
      {
         starttime=System.currentTimeMillis();
         gameupdate();
         gamerender();
         paintscreen();
         try
         {
            starttime += pausefor;
            Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
         }
         catch(InterruptedException interruptedexception) { }
      }
   }
   
   public void gamerender()
   {
      if(dbImage == null)
      {
         dbImage = createImage(getSize().width, getSize().height);
         if(dbImage == null)
         {
            System.out.println("dbImage is null");
            return;
         }
         else
         dbg = dbImage.getGraphics();
         dbg.setColor(getBackground());
         dbg.fillRect(0, 0, getSize().width, getSize().height);
         
         if(loading)
         {
            loading(dbg);
         }
         if(gameover && !credits && !loading)
         {
            titlescreen(dbg);
         }
         if(gameover && credits && !loading)
         {
            credits(dbg);
         }
         if(!gameover && firstlevel && !loading && !credits && !firstlevelbegin)
         {
            firstlevel(dbg);
         }
         if(ispaused)
         {
            paused(dbg);
         }
      }
   }
   
   public void paintscreen()
   {
      Graphics g;
      try
      {
         g = this.getGraphics();
         if((g != null) && (dbImage != null))
         g.drawImage(dbImage, 0, 0, null);
         Toolkit.getDefaultToolkit().sync();
         g.dispose();
      }
      catch (Exception e)
      {
         System.out.println("Graphics context error: " + e);
      }
   }


Thanks.
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.

Dwinin (6 views)
2014-07-29 14:59:34

E.R. Fleming (20 views)
2014-07-29 07:07:13

E.R. Fleming (8 views)
2014-07-29 07:06:25

pw (39 views)
2014-07-24 05:59:36

Riven (39 views)
2014-07-24 01:16:32

Riven (26 views)
2014-07-24 01:07:15

Riven (28 views)
2014-07-24 00:56:16

ctomni231 (59 views)
2014-07-18 10:55:21

Zero Volt (50 views)
2014-07-18 03:47:54

danieldean (42 views)
2014-07-18 03:41:23
HotSpot Options
by dleskov
2014-07-08 07:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 04:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 04:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 15:13:37

HotSpot Options
by Roquen
2014-05-15 13:59:54

HotSpot Options
by Roquen
2014-05-06 19:03:10

Escape Analysis
by Roquen
2014-04-30 02:16:43

Experimental Toys
by Roquen
2014-04-28 17:24:22
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!