Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Colors and Pixels  (Read 1817 times)
0 Members and 1 Guest are viewing this topic.
Offline thedracle

Senior Newbie




Java games rock!


« Posted 2003-01-27 14:26:23 »

I'm currently making a program doing very simple 2D graphics. I used to in the past, for pixel by pixel animation, use an array of integers and a MemoryImageSource. The problem with this is the fact that you can't grab the Graphics object of this image, and then use it to draw images, or fill circles normally. BufferedImages supposively allow you to do this now, but every attempt I've made at using it's ability to draw pixels directly has resulted in EXTREMELY low frame rates.

Also, another problem is the fact that the Color object NEEDS to be used when drawing a Oval, or a Rectangle.  If I'm creating 30 Color Objects a second, the garbage collector goes nuts and my frame-rates drop too. The only solution I've found is to make all of the Colors, and store them before drawing to the screen. What I would like to do is to change the Color using an Integer value,  and draw using the same Color object, rather than creating a new one every time.

-Jason Thomas.
Offline zparticle

Senior Member




Thick As A Brick


« Reply #1 - Posted 2003-01-27 15:24:34 »

Decompiling the Color class we see that the value member has package access. Perhaps you could subclass Color so that you can directly access this member variable. This would only help you if you don't want to use floats for the different components of your colors. value is an int containing ARGB.

Of course your new class would have to be in the java.awt package. Just an idea.

It seems very strange that the class doesn't have any setter methods. One would think that is an obvious need.

Offline markuskidd

Junior Member


Medals: 1



« Reply #2 - Posted 2003-01-27 15:28:42 »

#2:
You could subclass Color and override the regular behavior; I think the critical methods are all public -- check the source to be sure. I think your current solution is probably the best course though.

edit: I was looking at the source and I thought value was private. I'll try to remember to check that again later Cheesy until then, listen to zparticle
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zparticle

Senior Member




Thick As A Brick


« Reply #3 - Posted 2003-01-27 15:47:24 »

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  
package java.awt;

import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.io.Serializable;

public class Color
    implements Paint, Serializable
{

    public static final Color white = new Color(255, 255, 255);
    public static final Color lightGray = new Color(192, 192, 192);
    public static final Color gray = new Color(128, 128, 128);
    public static final Color darkGray = new Color(64, 64, 64);
    public static final Color black = new Color(0, 0, 0);
    public static final Color red = new Color(255, 0, 0);
    public static final Color pink = new Color(255, 175, 175);
    public static final Color orange = new Color(255, 200, 0);
    public static final Color yellow = new Color(255, 255, 0);
    public static final Color green = new Color(0, 255, 0);
    public static final Color magenta = new Color(255, 0, 255);
    public static final Color cyan = new Color(0, 255, 255);
    public static final Color blue = new Color(0, 0, 255);
    private transient long pData;
    int value;
    private float frgbvalue[];
    private float fvalue[];
    private float falpha;
    private ColorSpace cs;
    private static final long serialVersionUID = 0x1a51783108f3375L;
    private static final double FACTOR = 0.69999999999999996D;
    private PaintContext theContext;
.
.
.
}


Damn forum software wouldn't take the entire code listing.

Offline thedracle

Senior Newbie




Java games rock!


« Reply #4 - Posted 2003-01-27 19:29:22 »

Alright, you  can't add any new classes to java.awt, because the ClassLoader forbids it. Soooo.. I completely re-impleneted the Color class by extending it, and then overriding ALL of it's private methods and values.

Thanks for the help.

Now , if anybody knows how to do pixel by pixel manipulation efficiently with a buffered Image, I'd really like to see the code.

-Jason Thomas.
Offline thedracle

Senior Newbie




Java games rock!


« Reply #5 - Posted 2003-01-27 20:42:45 »

Also, setColor internally checks the refference of the Color object you're giving it, against the Color Object it already has. So in order to  change the MutableColor on the fly, you need to call setColor on one of the static Colors, and then set the Color to your MutableColor object.

-Jason Thomas.
Offline markuskidd

Junior Member


Medals: 1



« Reply #6 - Posted 2003-01-27 21:09:37 »

I'm just curious, but how many different colors are you working with?
Offline thedracle

Senior Newbie




Java games rock!


« Reply #7 - Posted 2003-01-27 21:29:59 »

All Colors. IIn most programming languages, you can simply send three integers representing red green and blue. This entire Color Object thing isn't a bad idea, because it relates to ColorSpaces, but it's inefficient for Game Programming, or any kind of real-time animation. Yes, I could buffer the Colors I'm using, I'd rather not be limited to doing that, however.

-Jason Thomas.
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #8 - Posted 2003-02-04 05:13:54 »

I seriously doubt that even 100 Color objects per second would affect your framerate too much. Color object is not that expensive to create/dispose of.

As for manipulating the BuffferedImage pixels direclty - indeed, it won't be too efficient as you'd have to have several method calls per pixel. Try to avoid direct pixel manipulation, since it'd really affect our ability to accelerate this image for you (that is, put it into video memory).
Offline Micke

Senior Newbie




Yada-yada


« Reply #9 - Posted 2003-02-04 09:59:33 »

Or, you can just be a bad boy and get the DataBuffer from it, and get the approriate arraytype ( int[] from DataBufferInt etc), and fiddle along at your leasure.
Like:

BufferedImage bi = frame.getGraphicsConfiguration().createCompatibleImage(xs, ys, Transparency.OPAQUE);
WritableRaster wr = bi.getRaster();
DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
int[] pixels = db.getData();

Ofcourse, you should check the type of the DataBuffer, check the ColorModel to see how the data is stored etc etc.


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online EgonOlsen
« Reply #10 - Posted 2003-02-04 14:35:50 »

Maybe this thread helps:

http://www.java-gaming.org/cgi-bin/JGOForums/YaBB.cgi?board=2D;action=display;num=1036556890

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.

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

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

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

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

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

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

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

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

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

Riven (55 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!