Results 1 to 5 of 5

Thread: 'Graphics' Methods

  1. #1

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228

    Angry '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...

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    I did a multiplayer connect 4 game a year and half ago. I think you can have it just repaint certain coordinates.

  3. #3

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    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...

  4. #4
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    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
    &nbsp;&nbsp;&nbsp;&nbsp;for (int y=0; y < image.getHeight (); y++)
    &nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int x=0; x < image.getWidth (); x++)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//get the pixel
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pixel = raster.getPixel (x, y, pixel);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//if the RGB value equals the RGB of the Color we want to make transparent
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//write new pixel over top
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (pixel[0] == TRANSPARENT_COLOR[0] && pixel[1] == TRANSPARENT_COLOR[1] && pixel[2] == TRANSPARENT_COLOR[2])
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raster.setPixel (x, y, TRANSPARENT_COLOR);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;//return new image	
    &nbsp;&nbsp;&nbsp;&nbsp;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)
    {
    &nbsp;&nbsp;&nbsp;&nbsp;//get images dimenions
    &nbsp;&nbsp;&nbsp;&nbsp;int sourceWidth = source.getWidth(io);
    &nbsp;&nbsp;&nbsp;&nbsp;int sourceHeight = source.getHeight(io);
    &nbsp;&nbsp;&nbsp;&nbsp;//create new BufferedImage
    &nbsp;&nbsp;&nbsp;&nbsp;BufferedImage image = new BufferedImage (sourceWidth, sourceHeight, BufferedImage.TYPE_INT_ARGB);
    &nbsp;&nbsp;&nbsp;&nbsp;//draw old image over new image
    &nbsp;&nbsp;&nbsp;&nbsp;image.getGraphics().drawImage(source, 0, 0, null);
    &nbsp;&nbsp;&nbsp;&nbsp;//return image
    &nbsp;&nbsp;&nbsp;&nbsp;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!

  5. #5

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    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
  •  



Click Here to Expand Forum to Full Width