Java JPanel Scaling

时间:2014-05-06 02:02:36

标签: java swing user-interface slider jpanel

Hey Guys我已经成功地在java中创建了一个GUI,它将使用滑块缩放多边形和圆形。一切正常但我想知道是否有办法改变原点(它从哪里缩放)。现在它从角落缩放,我希望它从中间缩放,所以我可以从中间开始,它可以均匀地缩小。此外,如果有人能告诉我一个简单的方法来替换我有一个图像的矩形,所以你可以上下缩放图片将是伟大的!谢谢!这是我的代码:

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

}

1 个答案:

答案 0 :(得分:0)

更改"原点"所以它成为" zoom"的中心。基本上只是从中心点减去d的一半的过程。

因此,假设中心点为28(25 / 2) + 15),您只需从此点d / 2或附近减去28 - (25 / 2) = 15(25/2)够...

我修改了paintComponent方法进行测试,因此矩形始终位于面板的中心,但您可以提供任意值来代替originXoriginY

@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

}

至于缩放图像,你应该看Graphics#drawImage(Image img, int x, int y, int width, int height, ImageObserver observer),但要注意,这会将图像缩放到绝对大小,不会保持图像比例。

更好的解决方案可能是使用介于0和1之间的double值,并使用此值多个元素来获取所需的绝对值