DV-industries
DV-industries

Reputation: 35

Transparency not working in java

I have an image, with a complete transparent background. However when I draw this image, ingame, it has a kind of shade to it, and I have no clue why. I would like to get that out of there. Does anyone have an idea? I don't have the reputation to post images of it apparently... So I'll try to give some more information. I have the Color.DARK_GRAY as background, and when I draw the image, you see a lighter gray square around it. Then when I draw a couple of these images ontop of eachother, that square gets lighter and lighter.

If I draw the image ontop of another image however, this effect does not occur.

Here I load the image

public BlackChip() {
    this.value = 500;
    this.url = "res/images/poker/blackchip.png";
    this.file = new File(url);
    BufferedImage bi;
    try {
        bi = ImageIO.read(file);
        this.image = bi;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Here I draw the image

public void renderChip(Chip chip, int x, int y) {
    g.drawImage(chip.getImage(), x, y, null);
}

Here I call that method

public void render() {
    screen.renderBackground(Color.DARK_GRAY);
    pokertable.render(Game.width / 2 - pokertable.getImage().getWidth(null) / 2, 50);
    screen.renderChip(cs.getWhiteChip(), 380, 310);
    screen.renderChip(cs.getRedChip(), 430, 310);
    screen.renderChip(cs.getGreenChip(), 480, 310);
    screen.renderChip(cs.getBlueChip(), 530, 310);
    screen.renderChip(cs.getBlackChip(), 580, 310); //this one is it
}

link to the images:

https://drive.google.com/file/d/0Bz-4pfUssUeHRWkxaUhodWNILWc/edit?usp=sharing

Well... this doesn't work either because i need 10 reputation to post more then 1 link

you can see the effect on this link, it's the image with full transparent background, drawn multiple times.

Upvotes: 0

Views: 253

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208994

I can't tell if this is the exact cause of the problem, because you haven't provided a MCVE but this method

public void renderChip(Chip chip, int x, int y) {
    g.drawImage(chip.getImage(), x, y, null);
}

Just looks wrong. All custom painting should be done within the context of the provided Graphics object in the overridden paintComponent method. If you have not overriden paintComponent in a JPanel or a JComponent then you are likely not painting correctly. You may be doing something like

public class SomePanel extends JPanel {
    private Graphics g;

    public SomePanel() {
        g = getGraphics();
    }
}

Which is completely wrong. You should instead be doing something like

public class SomePanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // do painting here
    }
}

You Classes can then have it's own render method that takes a Graphics object as an argument. Then can be called in the paintComponent method. Maybe something like

public class Chip {
    private JComponent imageObserver;
    private BufferedImage chipImage;
    int x, y;

    public Chip(BufferedImage chipImage, int x, int y, JComponent imageObserver){
        this.chipImage;
        this.x = x;
        this.y = y;
        this.imageObserver = imageObserver;
    }
    public void renderChip(Graphics g) {
        g.getImage(chipImage, x, y, imageObserver);
    }
}

And your panel

public class SomePanel extends JPanel {
    private List<Chip> chips;

    public SomePanel() {
        chips = new ArrayList<Chip>();
        // add new Chips
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Chip chip: chips) {
            chip.renderChip(g);
        }
    }
}

Upvotes: 1

Related Questions