1

I am attempting to create a program that uses a slider too zoom in on an Image. I have got it working with rectangles and circles but when I try to add Images, they display but when I slide the slider, they do not zoom in it only translates or moves it diagonally across the screen, not re sizing it at all. Can someone Help me fix this? Here is my code:

import javax.swing.*;


public class Parker
{
    public static void main(String[] args)
    {

        TheWindow w = new TheWindow();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //X wont close the window with out this line
        w.setSize(375,375);
        w.setVisible(true);
    }

}



import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;


public class TheWindow extends JFrame
{
    private JSlider slider; //declare slider
    private drawRect myPanel; //declare/ create panel


    public TheWindow()
    {
        super("Slider Example"); //make title
        myPanel = new drawRect();
        myPanel.setBackground(Color.cyan); //change background color

        slider = new JSlider(SwingConstants.VERTICAL, 0, 315, 10);// restrains the slider from scaling square to 0-300 pixels
        slider.setMajorTickSpacing(20); //will set tick marks every 10 pixels
        slider.setPaintTicks(true); //this actually paints the ticks on the screen

        slider.addChangeListener
        (
            new ChangeListener()
            {
                public void stateChanged(ChangeEvent e)
                {
                    myPanel.setD(slider.getValue()); //Wherever you set the slider, it will pass that value and that will paint on the screen
                }
            }

        );

        add(slider, BorderLayout.WEST); //similar to init method, adds slider and panel to GUI
        add(myPanel, BorderLayout.CENTER);


    }






}




import java.awt.*;
import javax.swing.*;

public class drawRect extends JPanel
{

    private int d = 20; //this determines the beginning size of the rect. 

    public void paintComponent(Graphics g)//paints obj on the screen
    {
        super.paintComponent(g); //prepares graphic object for drawing

        ImageIcon i = new ImageIcon("A:\\Capture.png"); //location of Image
        i.paintIcon(this, g, d, d); //paints icon on screen

        int originX = getWidth() / 2; //this is subtracting half of 'd' from the center point to scale it form the center
        int originY = getHeight() / 2;

        int x = originX - (d / 2);
        int y = originY - (d / 2);
        System.out.println(x + "x" + y);

       // g.fillRect(x, y, d, d); //paints rectangle on screen
        //x , y, width, height      
    }           

    public void setD(int newD)
    {
        d = (newD >= 0 ? newD : 10); //if number is less than zero it will use 10 for diameter(compressed if statement)
        repaint();

    }

    public Dimension getPrefferedSize()
    {

        return new Dimension(200, 200);     
    }

    public Dimension getMinimumSize()
    {
        return getPrefferedSize();
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
malideux
  • 53
  • 1
  • 9

1 Answers1

2

Here is the method to re-size the Image. Fit it in your code.

public BufferedImage resizeImage(Image originalImage, int newD) throws IOException {
    BufferedImage resizedImage = new BufferedImage(newD, newD,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, newD, newD, null);
    g.dispose();
    return resizedImage;
}

--EDIT--

Favor Composition over Inheritance.

Don't extend any class until and unless you are overriding the existing logic.

complete sample code:

class drawRect {

    private Image image;
    private JLabel label;

    public JLabel getLabel() {
        return label;
    }

    public drawRect() {
        try {
            label = new JLabel();
            image = ImageIO.read(new File("resources/1.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setD(10);
    }

    public void setD(int newD) {
        int d = (newD >= 0 ? newD : 10);

        try {
            label.setIcon(new ImageIcon(resizeImage(image, d)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private BufferedImage resizeImage(Image originalImage, int newD) throws IOException {
        BufferedImage resizedImage = new BufferedImage(newD, newD, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, newD, newD, label);
        g.dispose();
        return resizedImage;
    }

}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Ok, In what class should this be? – malideux May 06 '14 at 22:16
  • That would help :) I am a beginner Im sorry! – malideux May 06 '14 at 22:21
  • Set min value of slider to 1. – Braj May 06 '14 at 22:43
  • Awesome. Works Great. Why Use a JLabel instead? Also can you explain some of the code if it is not too much of a hassle? Thank you so much! – malideux May 06 '14 at 22:43
  • First of all don't extend any class until and unless its extremely needed to override existing logic. Let me update my post first. – Braj May 06 '14 at 22:47
  • Now I have added `JLabel` inside the class. Its a design pattern **Favor Composition over Inheritance**. call `getLabel()` whenever needed to add it in `JFrame`. – Braj May 06 '14 at 22:49
  • I have created a `JLabel` that has a icon. Now on slider change we are just setting the icon again with the re-sized image. – Braj May 06 '14 at 22:51
  • In constructor a Label is created and image is loaded. Now `setD()` method is called every time whenever there is a change in slider value. just pass the new dimention to `resizeImage()` method to get the new re sized image and set back as Label icon. – Braj May 06 '14 at 22:54
  • Awesome thank u! Is there a way that you can add a pan tool of some kind so you can see specific parts of the image? – malideux May 06 '14 at 23:04
  • Sure there a way but you have to ask it in separate question otherwise the purpose of this question will lost and all the things are mixed in single question that will be difficult for others. – Braj May 06 '14 at 23:13
  • Have a look at [How to pan an image using your mouse in Java Swing](http://stackoverflow.com/questions/13341857/how-to-pan-an-image-using-your-mouse-in-java-swing) – Braj May 06 '14 at 23:14