Advertisement

[java] Java Graphics

Started by March 10, 2000 10:38 AM
8 comments, last by Kosh 24 years, 7 months ago
Hi everyone, I am trying to program a little 2d application in java, just something that plots pixels to a screen. I want to recreate my good ol days with the spinning cubes, but I am unsure about how I should plot pixels in an efficient manner, I know about the Point2D class, but I would like to know, is there a better way of doing this. If you know anything that can help me please do tell me thanks Kosh
if understanding it made and born with time, then time, is understandings greatest enemy.....
You may want to check out the following book:

Java 2d API Graphics with Cdrom
Vincent J. Hardy
ISBN: 0130142662
Publisher: Prentice Hall
Pub. Date: August 1999
Edition Desc: BOOK & CD

Its one of the few decent books for Java and Graphics that I have found.

Also, I know you said you wanted to use 2D, but you may want to consider Java 3D as well. The example you quoted - creating spinning cubes - could be accomplished in just a few lines of code (in fact, one of the demo programs that comes with Java 3D is a mouse controlled spinning cube).
Advertisement
I know perhaps Java3D is a better choice, but I am trying to create a rendering engine, so using someone else''s 3d engine isnt a way that I would like to proceed, therefore, 2d is only open to me

do you have any online links ? cause I dont want to buy ANOTHER java book only to find that I didnt really need it.
spent too much money to do that mistake again....

thanks
if understanding it made and born with time, then time, is understandings greatest enemy.....
Buy The Black Art of Java Programming! It uses java 1.0 but all out-of-date code is very easy to change to java 2. Yes it explains how to create 3d engine, which had constantly 60 fps, but in 300x400 applet

Time comes, time goes and I only am.
ok, sorry to say this, but saying "buy this book" and "buy that book" is not what I want help with, I know I can buy this and that, but I dont want to buy another java book, I have four already, I want somewhere on the web that can help me, I have followed the advice of people before, spent over £150 on books in java and all of them have been a disappointment so therefore I wont buy another java book, cause I dont think they are worth it.

Can someone please suggest anything else other than "buy this book"

thanks.

kosh
if understanding it made and born with time, then time, is understandings greatest enemy.....
ok, sorry to say this, but saying "buy this book" and "buy that book" is not what I want help with, I know I can buy this and that, but I dont want to buy another java book, I have four already, I want somewhere on the web that can help me, I have followed the advice of people before, spent over £150 on books in java and all of them have been a disappointment so therefore I wont buy another java book, cause I dont think they are worth it.

Can someone please suggest anything else other than "buy this book"

thanks.

kosh
Advertisement
I have bought 4 books too, and not even single one of them were disappointment. I have Sams book 21 hours or 24 days it was the first one. Then I have little manual for Java 2 which is extremely handy because it's so quick to recollect and cheat things out from it. Then I have this Black Art book and some kind of book which is written in my mother tongue. I'm sorry, but what I have looked there are very little things on we from Java Game programming. There are ready made engines, but that wasn't what you were looking for, but if you want see one go to the JGDC home page (www.jgdc.com) and check code section, if you look enough those examples and then put all you have found together I'll bet that you have ready 3d engine ready before you can say cheese! At least that would be the way that I'd do it

Time comes, time goes and I only am.

Edited by - Arch@on on 3/12/00 12:27:19 PM
lovely, I''ll check that out, thanks
if understanding it made and born with time, then time, is understandings greatest enemy.....
Let me get this straight, you want to plot pixels to a screen? If by a pixel you mean a real pixel of size 1x1, then maybe you should familiarize yourself to the usage of an animated MemoryImage source. That technique is largely used in Java demos and should be quite fast (at least it is much faster than plotting hundreds/thousands of points through Graphics object).

The downside is that you''ll have to calculate all the transformations by yourself (calculate the screen x,y coordinates from world coordinates).

The technique is simply:
1) During initialisation create an integer array of size (screen width x height) and use that array to create an java.awt.image.MemoryImageSource and create an image from that. Set the memory image to be animated by calling the setAnimated(true) on it.


int[] pixels = new int[ width * height ];
MemoryImageSource memImageSrc =
new MemoryImageSource(
width,
height,
ColorModel.getRGBdefault(),
pixels,
0,
width );
memImageSrc.setAnimated( true );
Image theWholeScreen =
Toolkit.getDefaultToolkit().createImage(
memImageSrc );


2) You modify the individual pixels by setting the integer arrays values to wanted pixel values.


// Set pixel at x,y coordinates to red
pixels[ x + y * width ] = 0x00FF0000;


3) You tell the memory image source you have changed the pixels in the memory. Note: This should really just update the changed area, not the whole area as I have done here.


memImageSrc.newPixels( 0, 0, width, height );


4) You paint the image representing the whole screen to the graphics (in paint(Graphics g) or some other suitable way).


grfx.drawImage(
theWholeScreen,
0,
0,
null );


Hopefully this helps you getting started...
-Pasi Keranen
There is also a good technique in "The Black Art of Java Game Programming" in which you use Shapes to plot "3D" images to the screen. Your spinning cube would just be 6 Shape objects and you would do the transformations and surface hiding manually. It is a good book, if you don't want to buy it, just go to the book store and thumb through it.

Also Kosh, your post about "spinning cubes" and "2d application" seem to be a bit contrary.

Edited by - Jim_Ross on 3/14/00 10:09:44 AM

This topic is closed to new replies.

Advertisement