繁体   English   中英

Java JPanel缩放

[英]Java JPanel Scaling

嗨,我成功地用Java制作了一个GUI,该GUI将使用滑块缩放多边形和圆形。 一切正常,但我想知道是否有办法更改原点(从何处缩放)。 现在,它从角落缩放,我希望它从中间缩放,因此我可以从中间开始,然后均匀地缩放。 另外,如果有人能告诉我用某种图像替换我拥有的Rectangle的简便方法,以便您可以放大和缩小图片,那就太好了! 谢谢! 这是我的代码:

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

}

更改“原点”,使其成为“缩放”的中心,基本上只是从中心点减去d一半的过程。

因此,假设中心点为28(25 / 2) + 15 ),则只需从该点减去d / 2 (25/2),即28 - (25 / 2) = 15或足够接近。 。

我修改了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之间的双精度值,并将该值乘以各个元素,以获得所需的绝对值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM