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 (534)
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] 2 3
1  Game Development / Newbie & Debugging Questions / Re: file path that works in both Applets and Applications on: 2012-04-29 21:28:04
I add a top level folder named "res" and then add it to the project build as a class folder.

Then you use this line of code to grab em!

1  
TheClassWhereMainIs.class.getResource(filename)
2  Game Development / Newbie & Debugging Questions / Re: Why is this stuttering? on: 2012-04-29 21:25:19
At line 46, use "Math.round((1e9/FPS - (System.nanoTime() - lastTime)) / 1e6)", 1e9 and 1e6 are doubles, making the calculation more accurate.

thanks. btw why does it render over 60 fps tho???
3  Game Development / Newbie & Debugging Questions / Re: Why is this stuttering? on: 2012-04-29 21:15:53
For anyone curious, this is what ended up working O_o

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  
   private void gameLoop5() {

      if (System.getProperty("os.name").startsWith("Win")) {
         new Thread()
         {

            {
               setDaemon(true);
               start();
            }

            public void run() {
               while (true) {
                  try {
                     Thread.sleep(Long.MAX_VALUE);
                  }
                  catch (Exception exc) {
                  }
               }
            }
         };
      }

      long lastTime = 0;
      long nanosInSecond = 1000L * 1000L * 1000L;
      long updateFrequency = 60;
      long updateInterval = nanosInSecond / updateFrequency;
      long nextUpdateTick = System.nanoTime();
      long now = 0;
      long start = 0;
     
      long FPS = 60;

      while (running) {
         long deltaTime = System.nanoTime() - lastTime;
         lastTime += deltaTime;
         start = System.nanoTime();
         for (now = System.nanoTime(); now > nextUpdateTick; nextUpdateTick += updateInterval) {
            this.update();
         }
         process_time += System.nanoTime() - start;
         
         render();
         Toolkit.getDefaultToolkit().sync();
         
         long sleepTime = Math.round((1000000000 / FPS - (System.nanoTime() - lastTime)) / 1000000);
         if (sleepTime < 0) continue;
         
         // this sleeping method uses Thread.sleep(1) for the first 4/5 of
        // the sleep loop, and Thread.yield() for the rest. This gives me an
        // accuracy of about 3-4 microseconds
        long prev = System.nanoTime(), diff;
         while ((diff = System.nanoTime() - prev) < sleepTime) {
            if (diff < sleepTime * 0.8)
               try {
                  Thread.sleep(1);
               }
               catch (Exception exc) {
               }
            else
               Thread.yield();
         }
      }
   }
4  Game Development / Newbie & Debugging Questions / Re: Why is this stuttering? on: 2012-04-29 06:13:52
Thanks! I really owe it to my artist. :'D
5  Game Development / Newbie & Debugging Questions / Re: Why is this stuttering? on: 2012-04-29 05:52:01
reserved. (how do delete posts)
6  Game Development / Newbie & Debugging Questions / Re: Why is this stuttering? on: 2012-04-29 01:17:50
Windows 7 Pro, , Google Chrome

I just tried adding this
1  
Toolkit.getDefaultToolkit().sync();

and it made it much better O_o

EDIT: spoke too soon! so scratch that, still the same
7  Game Development / Newbie & Debugging Questions / Re: Why is this stuttering? on: 2012-04-29 01:11:27
Using canvas. Its really driving me crazy, considering both the update rate and render rate are well within the target rates.
8  Game Development / Newbie & Debugging Questions / Why is this stuttering? on: 2012-04-29 01:00:21
Here's a demo of the applet

http://www.joeysturgis.com/roguelike

Here's the game loop:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   private void gameLoop3(long ufreq) {
      long nanosInSecond = 1000L * 1000L * 1000L;
      long updateFrequency = ufreq;
      long updateInterval = nanosInSecond / updateFrequency;
      long nextUpdateTick = System.nanoTime();
      while (true) {
         long start = System.nanoTime();
         for (long now = System.nanoTime(); now > nextUpdateTick; nextUpdateTick += updateInterval) {
            this.update();
         }
         process_time += System.nanoTime() - start;
         render();
         Sync.sync(60);
      }
   }


Here's sync:

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  
/*
 * Copyright (c) 2002-2012 LWJGL Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'LWJGL' nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.ingamedev.util;

/**
* A highly accurate sync method that continually adapts to the system
* it runs on to provide reliable results.
*
* @author Riven
* @author kappaOne
*/

public class Sync {

   /** number of nano seconds in a second */
   private static final long NANOS_IN_SECOND = 1000L * 1000L * 1000L;

   /** The time to sleep/yield until the next frame */
   private static long nextFrame = 0;
   
   /** whether the initialisation code has run */
   private static boolean initialised = false;
   
   /** for calculating the averages the previous sleep/yield times are stored */
   private static RunningAvg sleepDurations = new RunningAvg(10);
   private static RunningAvg yieldDurations = new RunningAvg(10);
   
   
   /**
    * An accurate sync method that will attempt to run at a constant frame rate.
    * It should be called once every frame.
    *
    * @param fps - the desired frame rate, in frames per second
    */

   public static void sync(int fps) {
      if (fps <= 0) return;
      if (!initialised) initialise();
     
      try {
         // sleep until the average sleep time is greater than the time remaining till nextFrame
        for (long t0 = getTime(), t1; (nextFrame - t0) > sleepDurations.avg(); t0 = t1) {
            Thread.sleep(1);
            sleepDurations.add((t1 = getTime()) - t0); // update average sleep time
        }
   
         // slowly dampen sleep average if too high to avoid yielding too much
        sleepDurations.dampenForLowResTicker();
   
         // yield until the average yield time is greater than the time remaining till nextFrame
        for (long t0 = getTime(), t1; (nextFrame - t0) > yieldDurations.avg(); t0 = t1) {
            Thread.yield();
            yieldDurations.add((t1 = getTime()) - t0); // update average yield time
        }
      } catch (InterruptedException e) {
         
      }
     
      // schedule next frame, drop frame(s) if already too late for next frame
     nextFrame = Math.max(nextFrame + NANOS_IN_SECOND / fps, getTime());
   }
   
   /**
    * This method will initialise the sync method by setting initial
    * values for sleepDurations/yieldDurations and nextFrame.
    *
    * If running on windows it will start the sleep timer fix.
    */

   private static void initialise() {
      initialised = true;
     
      sleepDurations.init(1000 * 1000);
      yieldDurations.init((int) (-(getTime() - getTime()) * 1.333));
     
      nextFrame = getTime();
     
      String osName = System.getProperty("os.name");
     
      if (osName.startsWith("Win")) {
         // On windows the sleep functions can be highly inaccurate by
        // over 10ms making in unusable. However it can be forced to
        // be a bit more accurate by running a separate sleeping daemon
        // thread.
        Thread timerAccuracyThread = new Thread(new Runnable() {
            public void run() {
               try {
                  Thread.sleep(Long.MAX_VALUE);
               } catch (Exception e) {}
            }
         });
         
         timerAccuracyThread.setDaemon(true);
         timerAccuracyThread.start();
      }
   }

   /**
    * Get the system time in nano seconds
    *
    * @return will return the current time in nano's
    */

   private static long getTime() {
      //return (Sys.getTime() * NANOS_IN_SECOND) / Sys.getTimerResolution();
     return System.nanoTime();
   }

   private static class RunningAvg {
      private final long[] slots;
      private int offset;
     
      private static final long DAMPEN_THRESHOLD = 10 * 1000L * 1000L; // 10ms
     private static final float DAMPEN_FACTOR = 0.9f; // don't change: 0.9f is exactly right!

      public RunningAvg(int slotCount) {
         this.slots = new long[slotCount];
         this.offset = 0;
      }

      public void init(long value) {
         while (this.offset < this.slots.length) {
            this.slots[this.offset++] = value;
         }
      }

      public void add(long value) {
         this.slots[this.offset++ % this.slots.length] = value;
         this.offset %= this.slots.length;
      }

      public long avg() {
         long sum = 0;
         for (int i = 0; i < this.slots.length; i++) {
            sum += this.slots[i];
         }
         return sum / this.slots.length;
      }
     
      public void dampenForLowResTicker() {
         if (this.avg() > DAMPEN_THRESHOLD) {
            for (int i = 0; i < this.slots.length; i++) {
               this.slots[i] *= DAMPEN_FACTOR;
            }
         }
      }
   }
}


Here's render:

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  
   public void render() {
      frames++;
      frame_count++;
     
      long start = System.nanoTime();

      Graphics2D g = tmp.createGraphics();
      g.setColor(Color.black);
      g.fillRect(0, 0, WIDTH, HEIGHT);
     
      level.draw_edge_persepctive(g, SpriteType.ranger, x, y, pixel_x, pixel_y, 13, 9);
     
      for(Entity e : entities) {
         e.render(g, level.p_screen_x, level.p_screen_y);
      }
     
      Font.printString(tmp, "Rogu3", 4, 4, 5, 0x65DE31, 0x454545);
      Font.printString(tmp, "Power: " + score, 4, Font.h * 2 + 4, 24, 0xFA1122, 0x911122);
      Font.printString(tmp, "Entities: " + Level.ent_count, 4, Game.HEIGHT - (Font.h * 2) - 4, 24, 0xFFFFFF, 0x000000);
      Font.printString(tmp, "FPS: " + fps, 800 - 80 - 16, 4, 50, 0xABABAB, 0x232323);
     
      announce(g);
     
      g.drawImage(Art.border, 0, 0, 8, 8, 0, 0, 8, 8, null);
      g.drawImage(Art.border, Game.WIDTH - 8, 0, Game.WIDTH, 8, 24, 0, 32, 8, null);
      g.drawImage(Art.border, 0, Game.HEIGHT - 8, 8, Game.HEIGHT, 0, 24, 8, 32, null);
      g.drawImage(Art.border, Game.WIDTH - 8, Game.HEIGHT - 8, Game.WIDTH, Game.HEIGHT, 24, 24, 32, 32, null);
     
      for(int ix = 0; ix <= 50; ix ++) {
         g.drawImage(Art.border, 8 + (ix * 16), 0, 8 + (ix * 16) + 16, 8, 8, 0, 24, 8, null);
      }
      for(int ix = 0; ix <= 50; ix ++) {
         g.drawImage(Art.border, 8 + (ix * 16), Game.HEIGHT - 8, 8 + (ix * 16) + 16, Game.HEIGHT, 8, 24, 24, 32, null);
      }
      for(int iy = 0; iy <= 34; iy ++) {
         g.drawImage(Art.border, 0, 8 + (iy * 16), 8, 8 + (iy * 16) + 16, 0, 8, 8, 24, null);
      }
      for(int iy = 0; iy <= 34; iy ++) {
         g.drawImage(Art.border, Game.WIDTH - 8, 8 + (iy * 16), Game.WIDTH, 8 + (iy * 16) + 16, 24, 8, 32, 24, null);
      }

      g.dispose();
      g = getGraphics();
      if(g != null) {
         g.drawImage(tmp, 0, 0, null);
         g.dispose();
         flip();//show's the bufferstrategy
     }
     
      render_time = System.nanoTime() - start;
     
      if (System.nanoTime() > next_frame_count) {
         next_frame_count = System.nanoTime() + 1000000000;
         fps = frame_count;
         frame_count = 0;
         System.out.println("FPS: " + fps + ", Process Time: " + process_time / 60 + " Render Time: " + render_time);
         process_time = 0;
      }
   }


A few seconds of running...

FPS: 1, Process Time: 7992 Render Time: 138639760
FPS: 57, Process Time: 161622 Render Time: 9624389
FPS: 60, Process Time: 104913 Render Time: 11918064
FPS: 61, Process Time: 103605 Render Time: 8334624
FPS: 60, Process Time: 101535 Render Time: 12660010
FPS: 61, Process Time: 107089 Render Time: 8015489
FPS: 42, Process Time: 142956 Render Time: 18470568
FPS: 62, Process Time: 129872 Render Time: 8149031
FPS: 60, Process Time: 93706 Render Time: 10992659
FPS: 60, Process Time: 93977 Render Time: 11397550
FPS: 60, Process Time: 89091 Render Time: 12375007
FPS: 60, Process Time: 88103 Render Time: 12378847
FPS: 61, Process Time: 90599 Render Time: 10045067
FPS: 60, Process Time: 105866 Render Time: 11264862
FPS: 61, Process Time: 79250 Render Time: 11252489
FPS: 59, Process Time: 96743 Render Time: 16203345
FPS: 61, Process Time: 104095 Render Time: 13690799
FPS: 61, Process Time: 107302 Render Time: 10396627
FPS: 60, Process Time: 95676 Render Time: 12034540
FPS: 61, Process Time: 95185 Render Time: 12393353
FPS: 61, Process Time: 88039 Render Time: 12382261
FPS: 61, Process Time: 86411 Render Time: 12071659
FPS: 60, Process Time: 91651 Render Time: 13107567
FPS: 61, Process Time: 96309 Render Time: 10673097
9  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 23:14:13
Just to make sure - I assume you're rendering text in lots (hundreds maybe?) of different colors, maybe cycling through them for effect?  Because if you're only writing text in a few different colors, why not simply create a Map<Color, Font>, where each value is created as a copy of the original font image with the original color (e.g. white or black) changed to the new one, and lazily create entries per color?
Just wanted a system that was "smart enough" to do any color without having a version of it in memory. Considering we're just putting pixels on the screen, it should be doable via mathematical equation.
10  Game Development / Newbie & Debugging Questions / Re: Render tiles for RPG game on: 2012-04-27 22:36:04
I managed to draw the tiles and now I'm working on the player.

And I have a green bg in the tiles file can I remove it when it renders or is it better to remove the bg from the image file?

Just make it transparent in the image, as J2D and OGL both fully support transparent image formats (like PNG).
11  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 22:25:23
Of course, that's fine. Why would you think it wouldn't work? The only "slow" part is actually drawing the bitmap images and the color, but that should be much faster than copying each pixel one by one, unmanaging the image in the process.
Ok, cool. I think I will experiment a lot with this. This could perhaps lead to dynamic lighting of some sort as well by maybe rendering a light map image to composite over the scene.
12  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 20:21:08
The original font image is never modified, so you can simply redraw the white letters with a different color rect later Tongue

I was never under the impression or assumption that the original font image is modified.

I am asking if I can do this (and if it is fast, and if its the right way to go about it):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
...//font class
public void drawString(String s, Color r) {
    blah blah blah
    draw bitmap images / letters to font buffer image
    change composite
    set color, draw color rect
    reset composite back
}
...//gui code
Font.drawString("hello", red); //composite is changed, color is drawn
//later in code
Font.drawString("cool", orange); //composite is changed again, another color is drawn
...
//game loop
Font.clear();
GuiCode();
Font.render(g);//draws the font buffer image to the screen image
...


If the above is slow or not the proper way to do it, then the only other way to do it with one composite change and multiple colors would be some sort of batch buffer.
13  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 09:41:09
Thanks so much for this. The reason I am really forcing the J2D is because I don't want to go down that spiral of signed jar's for browser applets etc. etc.
If there's a really easy way to get OGL in a Java applet without asking the user anything... and I can use a pixel-level screen format (1 + 1 = 2 pixels) I'M IN!!
14  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 07:01:54
Supporting ogl is over kill for this project

perhaps i'll do what minicraft did by just modifying the array's my self
15  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 07:00:00
Perhaps I did not explain my example very well...

Let's say I want to draw a string of information in red, and then later (somewhere else, in some other class or on some other GUI window) in a completely separate function call, I want to draw a string of information in orange.
16  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 06:28:40
er...
17  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 06:11:01
oh are you saying to do all of that within one frame, per text && text_color?

so if i wanna draw something in red, then something in orange, i'm "drawing the letters, setting the composite, setting the color, filling the image, drawing the image, clearing the image" once for red, and once again for orange (all in the same rendering frame)
18  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 05:56:55
the stretch method won't allow multiple colors?
19  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 05:49:45
There's no way to get a single rect to fit the shape of the text exactly if there's a line wrap.
20  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 05:45:36
Would it be stupid to keep switching the composite mode for each character being drawn, so the color rect that goes over it EXACTLY fits each letter drawn?
21  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 05:02:31
I get 47 FPS @ rendering 1872 characters (the most that will fit on the screen, at once) with both methods (setRGB, and the AlphaComposite)

I'm not recreating BufferedImages every frame or anything like that, so when you say re-use objects I'm not sure what you mean. I am already re-using objects.
22  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 04:35:43
I read that setRGB doesn't unmanage the BufferedImage
23  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 03:44:33
BLAH! its not any faster than setRGB
24  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 03:40:54
oh my god it works

:evil laugh:
25  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 03:26:40
How do I clear the BufferedImage back to transparent each frame, to prevent dirty-ness Cheesy
26  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 03:20:30
You're supposed to set the Composite before you draw the rect Tongue

Also, make sure you reset the AlphaComposite to what it was previously after drawing the rect.
Oh wait, you're assuming that I'm rendering text to a image which gets drawn to another image, I think... which would explain why none of this is working because I'm just drawing the text (bitmapped font) directly onto the canvas.

So I'm guessing this would work if I was using a BufferedImage just for text rendering, clearing it to black every frame (Huh) and then using alpha composite to draw that to the canvas.
27  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-27 03:03:09
did you read my code? because i did that.
28  Game Development / Newbie & Debugging Questions / Re: How to make a copy of an object and not just it´s reference? on: 2012-04-26 19:59:08
I just make a getCopy() function in the class, if you want a true copy (you will need a "new" somewhere!).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class something {
    float x = 0;
    float y = 0;

    public something(float x, float y) {
        this.x = x;
        this.y = y;
    }
    public something getCopy() {
        something s = new something(this.x, this.y);
        //set additional values / variables here
       //this is where you "control" the copy / clone logic
       return s;
    }
}
29  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-26 17:50:41
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  
   public static void test(Graphics2D g, String string, int x, int y, int cwidth, int color, int bgcolor) {
      int px = 0;
      int py = 0;
      char c;
      int sx;
      int sy;
      int pixel_x = 0;
      int pixel_y = 0;
      int ix = 0;
      int iy = 0;
      int i = 0;
      int src_color = 0;
     
      for(i = 0; i < string.length(); i++) {
         c = string.charAt(i);
         if(c == 32) {
            px += w * 2;
            if(px / (w * 2) > cwidth) {
               px = 0;
               py += h * 2;
            }
            continue;
         }
         sx = c;
         sy = 0;
         while(sx > 15) {
            sx -= 16;
            sy++;
         }
         sx *= w;
         sy *= h;
         
         pixel_x = x + px;
         pixel_y = y + py;
         
         g.drawImage(Art.font_white, pixel_x, pixel_y, pixel_x + w * 2, pixel_y + h * 2, sx, sy, sx + w, sy + h, null);
         
         
         px += w * 2;
         if(px / (w * 2) > cwidth) {
            px = 0;
            py += h * 2;
         }
      }
     
      Composite comp = g.getComposite();
     
      g.setComposite(AlphaComposite.SrcIn);
      g.setColor(Color.red);
      g.fillRect(x, y, cwidth * 16, 32);
      g.setComposite(comp);
   }




What am I doing wrong?

Here's what happens if I comment out the composite / fillrect section

30  Game Development / Newbie & Debugging Questions / Re: Java2D drawing colored bitmap fonts on: 2012-04-26 05:44:05
i have an image of a font my artist drew. its black and transparent

wherever there is a black pixel, i want to put a color of my choice in place of where that black pixel would go
Pages: [1] 2 3
 

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

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

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

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

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

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

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

Escape Analysis
by Roquen
2014-04-29 22:16:43

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