Archived

This topic is now archived and is closed to further replies.

RobAU78

[java] Still Need Help!

Recommended Posts

Hey everyone, I used the code provided by Tortoise for buffered image transparency, but it doesn''t seem to work. Here is my program so far: // Test Panel for Space Game import java.awt.*; import java.awt.image.*; import java.net.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class TestPanel extends JPanel { BufferedImage background; BufferedImage star; BufferedImage img; public TestPanel() { setPreferredSize(new Dimension(640, 480)); img = createBuffImage(640, 480, true); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.paint(g2); setBackground(Color.black); g2.setColor(Color.white); Graphics2D gbi = img.createGraphics(); Composite originalComp = gbi.getComposite(); gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); gbi.setColor(new Color(0, 0, 0, 0)); gbi.fillRect(0, 0, img.getWidth(), img.getHeight()); gbi.setComposite(originalComp); try { URL u1 = getClass().getClassLoader().getResource("Space Game/trifid_hst.jpg"); URL u2 = getClass().getClassLoader().getResource("Space Game/star_white_3.gif"); background = ImageIO.read(u1); star = ImageIO.read(u2); gbi.drawImage(background, null, 0, 0); gbi.drawImage(star, null, 50, 50); } catch(IOException ioe) { gbi.drawString("Could not load image.", 50, 50); } gbi.dispose(); g2.drawImage(img, null, 0, 0); } BufferedImage createBuffImage(int w, int h, boolean transparent) { // setup graphics config GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = genv.getDefaultScreenDevice(); GraphicsConfiguration graphicsConfig = device.getDefaultConfiguration(); if (transparent) return graphicsConfig.createCompatibleImage(w, h, Transparency.TRANSLUCENT); else return graphicsConfig.createCompatibleImage(w,h,Transparency.OPAQUE); } public static void main(String[] args) { JFrame frame = new JFrame("Test Panel Program"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new TestPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Tortoise, or anyone else, do you know why it doesn''t work like it''s supposed to? Thanks, Rob

Share this post


Link to post
Share on other sites
What exactly doesn''t work?

paint() is called by the system whenever it feels the jpanel needs to update itself on the screen. So paint() can get called often. You''re likely loading those images from disk over and over again, that can''t be good. Load them in your constructor or somewhere else.

You should also be using paintComponent() instead of paint().

Why are you using img? You can just draw onto the JPanel itself. Swing''s double buffered by default.

Share this post


Link to post
Share on other sites
Hey Tortoise,

Thanks for your reply. I took your advice and amended my code, but it still doesn''t work properly. The top image (the star) still has its black background showing over the bottom image (the planet). Why is this? Here is the amended code:

// Test Panel for Space Game

import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class TestPanel extends JPanel
{
BufferedImage background;
BufferedImage star;

public TestPanel()
{
setPreferredSize(new Dimension(640, 480));

try
{
URL u1 = getClass().getClassLoader().getResource("Space Game/mercury2.gif");
URL u2 = getClass().getClassLoader().getResource("Space Game/star_white_3.gif");

background = ImageIO.read(u1);
star = ImageIO.read(u2);
}
catch(IOException ioe)
{
Graphics g = getGraphics();
g.setColor(Color.white);
g.drawString("Could not load image.", 50, 50);
}
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
setBackground(Color.black);
Composite originalComp = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g2.setColor(new Color(0, 0, 0, 0));
g2.fillRect(0, 0, getWidth(), getHeight());

g2.drawImage(background, 25, 25, null);
g2.drawImage(star, 40, 40, null);

g2.setComposite(originalComp);
g2.dispose();
}

/* BufferedImage createBuffImage(int w, int h, boolean transparent)
{
// setup graphics config
GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = genv.getDefaultScreenDevice();
GraphicsConfiguration graphicsConfig = device.getDefaultConfiguration();

if (transparent)
return graphicsConfig.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
else
return graphicsConfig.createCompatibleImage(w,h,Transparency.OPAQUE);
} */

public static void main(String[] args)
{
JFrame frame = new JFrame("Test Panel Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new TestPanel();

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

- Rob

Share this post


Link to post
Share on other sites
You''re confused on what transparent means and what a composite does. The AlphaComposite isn''t a magic pill You want all black pixels in your star image to end up transparent, but the AlphaComposite nore anything in your code knows that. As far as they''re concerned, the black pixels are just as much a part of the picture as anything else.

You don''t need any kind of special compositing here at all, that example I gave was for a different effect. The easiest thing to do is to make the star transparent to begin with. Get a program like photoshop, paint shop pro, the gimp (which is free), and edit your star image so all areas you don''t want to show up are transparent, and preferably save it as a png. After you''ve done that, this new transparency information is a part of the image, and Java will do the right thing automatically.

Then you just have to draw the background, and then draw the star. No fancy compositing or anything, and where ever you said the star should be transparent in the paint program, it will be transparent in your Java program too.

It is possible to do what you want without editing the image. But it''s much more difficult, and less flexible. I really recommend using a paint program for things like this as much as possible.

Share this post


Link to post
Share on other sites