|
-
Oct 2nd, 2002, 11:21 PM
#1
Thread Starter
Addicted Member
'Graphics' Methods
I'm trying to make a fun little game with java, because java make it so easy to manipulate graphics, except that i am hitting a road block!! for example: when you use the
Code:
public void paint( Graphics g )
{
Image firstImage = createImage( 500, 400 );
anImage = getImage( getCodeBase(), "theImage.bmp" );
offScreen.getGraphics( firstImage )
offScreen.drawImage( anImage, 10, 30, this );
g.clearRect( 500, 400 );
g.drawImage( firstImage, 0, 0, this );
}
this draws an image ("theImage.bmp") just fine to the screen, BUT... if i were to draw two images, the background of the second image covers up the first image. Understand what i mean? How do i get it so that i can only paint just the 'Image' and not the surrounding black background which is required cuz you have to do something with that space in a .bmp file. There has to be a way to so this, no in fact i know there is a way to do it!!! Any Ideas???? Please Help!! Thanx in advance.
To protect time is to protect everything...
-
Oct 3rd, 2002, 08:09 AM
#2
Hyperactive Member
I did a multiplayer connect 4 game a year and half ago. I think you can have it just repaint certain coordinates.
-
Oct 3rd, 2002, 03:40 PM
#3
Thread Starter
Addicted Member
yeah, sure you can... thats not what I mean at all (sorry)... Lets say that you draw a circle that fills up most of the bitmap in paint. then you use paint bucket to make the rest of the area black that the circle doesn't take up. Thwn, when i get into my program, i want to put the circle on the screen, say for an animation. BUT... i dont want the computer to paint the BLACK part ont the screen, just the circle. I know this is possible because people have done it! Does anyone know how???
To protect time is to protect everything...
-
Oct 13th, 2002, 07:11 AM
#4
Addicted Member
Are you trying to make some part of your image transparent?
if so then this code should help...
Code:
public static BufferedImage makeTransparent(BufferedImage image, Color c)
{
//create the pixel but make the alpha channel 0 for transparent
int[] TRANSPARENT_COLOR = {c.getRed(), c.getGreen(), c.getBlue(), 0};
WritableRaster raster = image.getRaster ();
int[] pixel = new int [4];
//go through every pixel
for (int y=0; y < image.getHeight (); y++)
{
for (int x=0; x < image.getWidth (); x++)
{
//get the pixel
pixel = raster.getPixel (x, y, pixel);
//if the RGB value equals the RGB of the Color we want to make transparent
//write new pixel over top
if (pixel[0] == TRANSPARENT_COLOR[0] && pixel[1] == TRANSPARENT_COLOR[1] && pixel[2] == TRANSPARENT_COLOR[2])
{
raster.setPixel (x, y, TRANSPARENT_COLOR);
}
}
}
//return new image
return image;
}
as you see you need a BufferedImage. To create one from a normal Image use...
Code:
public static BufferedImage createBufferedImage(Image source, ImageObserver io)
{
//get images dimenions
int sourceWidth = source.getWidth(io);
int sourceHeight = source.getHeight(io);
//create new BufferedImage
BufferedImage image = new BufferedImage (sourceWidth, sourceHeight, BufferedImage.TYPE_INT_ARGB);
//draw old image over new image
image.getGraphics().drawImage(source, 0, 0, null);
//return image
return image;
}
for the ImageObserver you can just use a Component.
Hope this answers your question
Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!
-
Oct 16th, 2002, 07:54 PM
#5
Thread Starter
Addicted Member
Well it turns out i dont need any of that, sorry thanx for the info anyway. I figured out that if i use a .gif file with a transparent background, then java does all the rest.
To protect time is to protect everything...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|