Results 1 to 1 of 1
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default Problem with zooming background image

    Hi!

    Let's say there is a background image and some JLabel components on the top of this image. I'm facing a problem with zooming the background image, while on-top JLabel components are zooming perfectly. So, the background image is just duplicated, but not zoomed. Why this could happen? Please help.

    Thanks for any advice.

    PHP Code:
        private void doZoom(int scaleFactor) {
            try {
                // This piece of code works correctly
                Component[] c = MainClass.getSelectablePanel().getComponents();
                for(int i = 0; i < c.length; i++) {
                    if (c[i] instanceof MyLabel) {
                        MyLabel selectedLabel = (MyLabel)c[i];
                        BufferedImage source = (BufferedImage) selectedLabel.getOriginalImage();
                        int w = selectedLabel.getWidth() + scaleFactor;
                        int h = selectedLabel.getHeight() + scaleFactor;
                        BufferedImage result = copyImage(source, w, h);
                        selectedLabel.setSize(new Dimension(w, h));
                        selectedLabel.setIcon(scale(result, w, h));
                    }
                }
    
                // Here is a problem: zooming a background image
                SelectablePanel selectedPanel= MainClass.getSelectablePanel();
                ImageIcon sourceIcon = selectedPanel.getOriginalBackgroundImage();
                Image source = sourceIcon.getImage();
                int w = selectedPanel.getWidth() + scaleFactor;
                int h = selectedPanel.getHeight() + scaleFactor;
                source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
                //selectedPanel.clear();
                selectedPanel.setBackgroundImage(scale(source, w, h));
                repaint();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    ...
        class MyJPanel extends JPanel {
            private Image backgroundImage = null;
              
            public void setBackgroundImage(Image backgroundImage) {
                this.backgroundImage = backgroundImage;
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                if (isOpaque()) {
                    g.setColor(getBackground());
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                if (backgroundImage != null) {
                    g.drawImage(backgroundImage,0,0,getWidth(), getHeight(),this);
                }
            }
          }
    Last edited by LianaN; 01-28-2011 at 09:52 PM.

Similar Threads

  1. [Help] W/ background image
    By gundum584 in forum New To Java
    Replies: 9
    Last Post: 01-10-2011, 05:48 AM
  2. JApplet background image
    By footballHunter in forum New To Java
    Replies: 1
    Last Post: 11-24-2010, 02:46 AM
  3. Replies: 6
    Last Post: 07-21-2010, 06:15 AM
  4. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •