Question

Hey Guys I have succesfully made a GUI in java that will scale polygons and circles using a slider. Everything works but I was wondering if there is a way to change the Origin point(Where it scales from). Right now it scales from the corner and I would like it to scale from the middle so I can start it in the middle and it scales out evenly. Also, If anyone could tell me an easy way to replace the Rectangle I have with an Image of some kind so you can scale the Picture up and down would be great! Thank you! Here is my code:

import javax.swing.*;


public class Fred
{
    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.green); //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 = 25; //this determines the beginning length of the rect. 

    public void paintComponent(Graphics g)//paints circle on the screen
    {
        super.paintComponent(g); //prepares graphic object for drawing
        g.fillRect(15,15, 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();
    }

}
Was it helpful?

Solution

Changing the "origin point" so it becomes the center of the "zoom" is basically just the process of subtract half of d from the center point.

So, assuming the the center point is 28 ((25 / 2) + 15), you would simply then subtract d / 2 (25 / 2) from this point, 28 - (25 / 2) = 15 or near enough...

I modified the paintComponent method for testing, so the rectangle is always at the center of the panel, but you can supply arbitrary values in place of the originX and originY

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

    int originX = getWidth() / 2;
    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

}

As for scaling an image, you should look at Graphics#drawImage(Image img, int x, int y, int width, int height, ImageObserver observer), beware though, this will scaling the image to the absolute size, it won't keep the image ratio.

A better solution might be to use a double value of between 0 and 1 and multiple the various elements by this value to get the absolute values you want

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top