如何使用滑块更改Java Graphics2D中的图形大小?

时间:2015-03-15 23:56:24

标签: java swing polygon graphics2d jslider

我正在尝试用java绘制程序,使用Graphics 2D绘制3到8边之间的多边形,并且我可以使用滑块重新调整大小,但我不知道如何使用滑块均匀地改变它的大小。< / p>

这是我画的五边形的一个例子

if (sides == 5){
        g.drawLine(110+x,135-y, 10+x,205-y);
        g.drawLine(10+x,205-y, 48+x, 320-y);
        g.drawLine(48+x,320-y, 170+x,320-y);
        g.drawLine(170+x,320-y, 205+x,205-y);
        g.drawLine(205+x,205-y, 110+x,135-y);
    }

现在我想要均匀地改变它的大小。 (我可以在X和Y轴上移动多边形。)

提前致谢。

1 个答案:

答案 0 :(得分:2)

你可以......

使用AffineTransformation缩放Graphics上下文

Scale

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                TestPane tp = new TestPane();

                JSlider slider = new JSlider(10, 200);
                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        tp.setScale(slider.getValue());
                    }
                });
                slider.setValue(100);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tp);
                frame.add(slider, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int scale = 100;

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(250, 350);
        }

        public void setScale(int value) {
            if (value != scale) {
                int old = scale;
                this.scale = value;
                firePropertyChange("scale", old, scale);
                repaint();
            }
        }

        public int getScale() {
            return scale;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            double scaleValue = getScale() / 100d;
            System.out.println(scaleValue);
            AffineTransform at = AffineTransform.getScaleInstance(scaleValue, scaleValue);
            g2d.setTransform(at);

            int x = 10;
            int y = 10;

            g2d.drawLine(110 + x, 135 - y, 10 + x, 205 - y);
            g2d.drawLine(10 + x, 205 - y, 48 + x, 320 - y);
            g2d.drawLine(48 + x, 320 - y, 170 + x, 320 - y);
            g2d.drawLine(170 + x, 320 - y, 205 + x, 205 - y);
            g2d.drawLine(205 + x, 205 - y, 110 + x, 135 - y);
            g2d.dispose();
        }

    }

}

这会扩大尺寸和位置,但这可能并不理想......

你可以......

请改用2D图形形状API。这将允许您独立定义形状,并根据需要简单地绘制它。这样做的好处是,您可以更好地控制转换,转换比例而不转换位置

Scaled Shape

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                TestPane tp = new TestPane();

                JSlider slider = new JSlider(10, 200);
                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        tp.setScale(slider.getValue());
                    }
                });
                slider.setValue(100);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tp);
                frame.add(slider, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int scale = 100;
        private PentegonShape pentegonShape;

        public TestPane() {
            pentegonShape = new PentegonShape(100, 100);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(250, 350);
        }

        public void setScale(int value) {
            if (value != scale) {
                int old = scale;
                this.scale = value;
                firePropertyChange("scale", old, scale);
                repaint();
            }
        }

        public int getScale() {
            return scale;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            int x = 10;
            int y = 10;

            double scaleValue = getScale() / 100d;
            Shape shape = AffineTransform.getScaleInstance(scaleValue, scaleValue).createTransformedShape(pentegonShape);
            g2d.setTransform(AffineTransform.getTranslateInstance(x, y));
            g2d.draw(shape);
            g2d.dispose();
        }

    }

    public class PentegonShape extends Path2D.Double {

        public PentegonShape(double width, double height) {
            moveTo(width / 2, 0);
            lineTo(width, height / 3d);
            lineTo((width / 5d) * 4, height);
            lineTo((width / 5d), height);
            lineTo(0, height / 3d);
            closePath();
        }

    }

}

特别关注2D GraphicsWorking with Geometry