This discussion is archived
1 Reply Latest reply: Feb 29, 2012 9:47 PM by Maxideon RSS

Problems on scaling an image

910151 Newbie
Currently Being Moderated
Hi,

I've a grid (GridLayout) composed by panel (JPanel). I don't understand because if I don't scale the background image every panel is displayed fine but if I scale the image I don't visualize properly the panels and resizing the main window (JForm) containing the grid the images change positions. The following is the code of JPanel:
public void setImg(ImageIcon icon, int fade){
          Image image = icon.getImage();
          myImage = image;
          //myImage = image.getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_DEFAULT);
          this.paint(getGraphics());
}

public void paint(Graphics g) {
          super.paint(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
          g2d.drawImage(myImage, 0, 0, null);
}
Thanks in advance!

Paolo
  • 1. Re: Problems on scaling an image
    Maxideon Explorer
    Currently Being Moderated
    You're doing a couple things wrong in the small amount of code you posted. I can't think of an instance where you would need to call getGraphics() on a Component. If you use that method, chances are high you're doing it wrong. Try replacing that whole statement with repaint(). Second, when doing custom painting in swing you should override the paintComponent() method instead of paint() (also change the super call to paintComponent()).

    Asside from that, you'll need to provide an SSCCE if you need more help.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points