1 Reply Latest reply on Mar 1, 2012 5:47 AM by Maxideon

    Problems on scaling an image


      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);
      public void paint(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                g2d.drawImage(myImage, 0, 0, null);
      Thanks in advance!

        • 1. Re: Problems on scaling an image
          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.