0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

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);
        }
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I don't extend JPanel, i extend Canvas so i cannot do this – DV-industries Jul 02 '14 at 14:29
  • Regardless, if you are extending Canvas, then you should be overriding `paint` of the Canvas class. All custom painting should be done in the context of the overridden method. As an aside, you _should_ be using JPanel instead of heavyweight Canvas, and Swing rather than AWT (if that's what you're using) – Paul Samsotha Jul 02 '14 at 14:34
  • Yes i am, you see i'm not a good programmer, i just started to try stuff out myself. I would like to thank you allready for your help, but now i have the problem of changing code, so it works with JPanel XD. I have a problem with" BufferStrategy bs = getBufferStrategy();" – DV-industries Jul 02 '14 at 14:36
  • You don't need that. You would need to do that for double buffering, but JPanel as well as all JComponents are double buffered by default – Paul Samsotha Jul 02 '14 at 14:37
  • Also see [Custom Painting With Swing](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and in general the [Swing Tutorials](http://docs.oracle.com/javase/tutorial/uiswing/) – Paul Samsotha Jul 02 '14 at 14:38
  • By the way, i was setting my graphics i was using before with "Graphics g = bs.getDrawGraphics(); – DV-industries Jul 02 '14 at 14:40
  • I don't know I'm going to delete my answer if it doesn't help you. You may be doing something totally different than what I'm thinking. Let me know. Also for better help, you should provide a short runnable program people can test out. Currently you have not provided enough information. That's why you aren't getting any answers – Paul Samsotha Jul 02 '14 at 14:43
  • You did help, but my problem is within the photo editor, your extra information about JPanel was very helpfull though. But how does this paintComponent method get called, because i don't even call it and it allready draws to the screen. – DV-industries Jul 02 '14 at 14:45
  • You don't explicitly call it. It get's called implicitly. You should draw everything in that method. If you want to change some variables, and then repaint the screen, just call `repaint()`, and the `paintComponent` method will be called again. See [this example](http://stackoverflow.com/a/22123304/2587435). If you look in the timer code, you will see that I change some state of each Car object, then call repaint for an animation effect – Paul Samsotha Jul 02 '14 at 14:50
  • Thank you very much! it works, but it didn't solve the image problem, i guess that really is within the photo editor, i'm going to click answer this question in about 10 minutes. Once again thank you very much. With JPanel i get like 10000 more fps aswell haha :) – DV-industries Jul 02 '14 at 14:52