0

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();
    }

}
malideux
  • 53
  • 1
  • 9
  • 1
    That all comes down to you. *"Origin point"* is contextual to what you want it to mean – MadProgrammer May 06 '14 at 02:04
  • Im not sure what you mean by this. But I mean right now, The rectangle is scaling up and down from sort of an "anchor point" In the top left corner. It may be hard to visualize but I am trying to get this "anchor point" In the center of the rectangle or object – malideux May 06 '14 at 02:11
  • An `AffineTransform` can be used for that. – Andrew Thompson May 06 '14 at 02:13

1 Answers1

0

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • If It is not too much to ask, What is the easiest way to rotate the object along with scaling it when the slider is dragged? – malideux May 06 '14 at 02:38
  • The eaist way, would be through the use of an `AffineTransform` – MadProgrammer May 06 '14 at 02:41
  • For [example](http://stackoverflow.com/questions/20275424/rotating-image-with-affinetransform/20280225#20280225) and [example](http://stackoverflow.com/questions/23463174/getting-my-sprite-to-face-in-a-certain-direction-based-on-keyboard-input-java/23463387#23463387) – MadProgrammer May 06 '14 at 02:41
  • Hey man, When I try to add an Image instead of a rectangle or circle object It does not scale or resize it only translates diagonally across the screen. Any ideas on how to make it scale? I asked it in my most recent question but You are a very big help and can probably help me lol! :) – malideux May 06 '14 at 22:15