<bdo id='z2l0l'></bdo><ul id='z2l0l'></ul>

  1. <small id='z2l0l'></small><noframes id='z2l0l'>

  2. <i id='z2l0l'><tr id='z2l0l'><dt id='z2l0l'><q id='z2l0l'><span id='z2l0l'><b id='z2l0l'><form id='z2l0l'><ins id='z2l0l'></ins><ul id='z2l0l'></ul><sub id='z2l0l'></sub></form><legend id='z2l0l'></legend><bdo id='z2l0l'><pre id='z2l0l'><center id='z2l0l'></center></pre></bdo></b><th id='z2l0l'></th></span></q></dt></tr></i><div id='z2l0l'><tfoot id='z2l0l'></tfoot><dl id='z2l0l'><fieldset id='z2l0l'></fieldset></dl></div>

    <tfoot id='z2l0l'></tfoot>
      <legend id='z2l0l'><style id='z2l0l'><dir id='z2l0l'><q id='z2l0l'></q></dir></style></legend>

      在java中使用Slider旋转对象

      Using Slider to rotate Object In java(在java中使用Slider旋转对象)
    1. <small id='bwu3g'></small><noframes id='bwu3g'>

      <i id='bwu3g'><tr id='bwu3g'><dt id='bwu3g'><q id='bwu3g'><span id='bwu3g'><b id='bwu3g'><form id='bwu3g'><ins id='bwu3g'></ins><ul id='bwu3g'></ul><sub id='bwu3g'></sub></form><legend id='bwu3g'></legend><bdo id='bwu3g'><pre id='bwu3g'><center id='bwu3g'></center></pre></bdo></b><th id='bwu3g'></th></span></q></dt></tr></i><div id='bwu3g'><tfoot id='bwu3g'></tfoot><dl id='bwu3g'><fieldset id='bwu3g'></fieldset></dl></div>
          <tbody id='bwu3g'></tbody>

            1. <legend id='bwu3g'><style id='bwu3g'><dir id='bwu3g'><q id='bwu3g'></q></dir></style></legend>
            2. <tfoot id='bwu3g'></tfoot>

                <bdo id='bwu3g'></bdo><ul id='bwu3g'></ul>
                本文介绍了在java中使用Slider旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我制作了一个 GUI,它使用滑块来放大和缩小对象.(在本例中为矩形).我想知道是否有一种方法也可以使用滑块来指定旋转度数.因此将有 2 个滑块,一个用于控制缩放,另一个用于控制旋转.如果有人可以帮助我做到这一点,那就太好了,这就是我迄今为止仅使用比例滑块所拥有的.

                I have made a GUI that uses a slider to scale an object up and down.(in this case a rectangle). I was wondering if there was a way to also use a slider to specify a degree of rotation. So there would be 2 sliders one to control scale and another to control the rotation. If anyone could help me make this that would be great here is what I have so far with just the scale slider.

                import javax.swing.*;
                
                
                public class Parker
                {
                    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(1280,720);
                        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.cyan); //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 = 20; //this determines the beginning size of the rect. 
                
                    public void paintComponent(Graphics g)//paints obj on the screen
                    {
                        super.paintComponent(g); //prepares graphic object for drawing
                
                       // ImageIcon i = new ImageIcon("A:\Capture.png"); //location of Image
                       // i.paintIcon(this, g, d, d); //paints icon on screen
                
                        int originX = getWidth() / 2; //this is subtracting half of 'd' from the center point to scale it form the center
                        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      
                    }           
                

                推荐答案

                好的,我已经玩了一段时间了,我通常会为此使用 AffineTransform,但它给出了我无法解决的奇怪结果...

                Okay, I've been playing around with this for a while I would normally use a AffineTransform for this, but it was giving me weird results I couldn't resolve...

                import java.awt.BorderLayout;
                import java.awt.Color;
                import java.awt.Dimension;
                import java.awt.EventQueue;
                import java.awt.Graphics;
                import java.awt.Graphics2D;
                import java.awt.geom.AffineTransform;
                import java.awt.geom.Rectangle2D;
                import javax.swing.JFrame;
                import javax.swing.JPanel;
                import javax.swing.JSlider;
                import javax.swing.SwingConstants;
                import javax.swing.UIManager;
                import javax.swing.event.ChangeEvent;
                import javax.swing.event.ChangeListener;
                
                public class Parker {
                
                    public static void main(String[] args) {
                        new Parker();
                    }
                
                    public Parker() {
                        EventQueue.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                } catch (Exception ex) {
                                }
                
                                JFrame frame = new JFrame("Testing");
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                frame.add(new ControlPane());
                                frame.pack();
                                frame.setLocationRelativeTo(null);
                                frame.setVisible(true);
                            }
                        });
                    }
                
                    public class ControlPane extends JPanel {
                
                        private JSlider slider; //declare slider
                        private DrawPane myPanel; 
                        public ControlPane() {
                            setLayout(new BorderLayout());
                            myPanel = new DrawPane();
                            myPanel.setBackground(Color.cyan); //change background color
                
                            slider = new JSlider(SwingConstants.VERTICAL, 0, 400, 100);// 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() {
                                        @Override
                                        public void stateChanged(ChangeEvent e) {
                                            myPanel.setScale(slider.getValue()); //Wherever you set the slider, it will pass that value and that will paint on the screen
                                        }
                                    }
                            );
                
                            JSlider rotate = new JSlider(SwingConstants.VERTICAL, 0, 720, 0);
                            rotate.setMajorTickSpacing(20); //will set tick marks every 10 pixels
                            rotate.setPaintTicks(true); //this actually paints the ticks on the screen
                
                            rotate.addChangeListener(
                                    new ChangeListener() {
                                        @Override
                                        public void stateChanged(ChangeEvent e) {
                                            JSlider slider = (JSlider) e.getSource();
                                            myPanel.setAngle(slider.getValue()); 
                                        }
                                    }
                            );
                            add(slider, BorderLayout.WEST);
                            add(rotate, BorderLayout.EAST);
                
                            add(myPanel);
                
                            myPanel.setScale(400);
                        }
                
                    }
                
                    public class DrawPane extends JPanel {
                
                        private double scale = 1;
                        private double angle = 0;
                
                        private final int rectWidth = 20;
                        private final int rectHeight = 20;
                
                
                        @Override
                        protected void paintComponent(Graphics g)//paints obj on the screen
                        {
                            super.paintComponent(g); //prepares graphic object for drawing
                
                            int originX = getWidth() / 2; 
                            int originY = getHeight() / 2;
                
                            int xOffset = -(rectWidth / 2);
                            int yOffset = -(rectHeight / 2);
                
                            g.setColor(Color.BLACK);
                            Graphics2D g2d = (Graphics2D) g.create();
                
                            g2d.translate(originX, originY);
                            g2d.scale(scale, scale);
                            g2d.rotate(Math.toRadians(angle), 0, 0);
                
                            g2d.fill(new Rectangle2D.Double(xOffset, yOffset, rectWidth, rectHeight));
                            g2d.dispose();
                
                            g.setColor(Color.RED);
                            g.drawRect(originX + xOffset, originY + yOffset, rectWidth, rectWidth);
                        }
                
                        public void setAngle(double angle) {
                            this.angle = angle;
                            repaint();
                        }
                
                        public void setScale(int scale) {
                            // Scaling is normalized so that 1 = 100%
                            this.scale = (scale / 100d);
                            repaint();
                        }
                
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(200, 200);
                        }
                
                    }
                
                }
                

                基本上,这使用 Graphics API 功能,为简单起见(特别是旋转),Graphics 上下文被转换为原点.然后在这个原点周围绘制矩形,以允许它围绕它的中心进行缩放和旋转

                Basically this uses the Graphics APIs capabilities, for simplicity (in particular with the spinning), the Graphics context is translated to the origin point. The rectangle is then paint around this origin point to allow it to be zoomed and rotate about it's center

                这篇关于在java中使用Slider旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                <small id='iHmfU'></small><noframes id='iHmfU'>

              • <tfoot id='iHmfU'></tfoot>
                  <i id='iHmfU'><tr id='iHmfU'><dt id='iHmfU'><q id='iHmfU'><span id='iHmfU'><b id='iHmfU'><form id='iHmfU'><ins id='iHmfU'></ins><ul id='iHmfU'></ul><sub id='iHmfU'></sub></form><legend id='iHmfU'></legend><bdo id='iHmfU'><pre id='iHmfU'><center id='iHmfU'></center></pre></bdo></b><th id='iHmfU'></th></span></q></dt></tr></i><div id='iHmfU'><tfoot id='iHmfU'></tfoot><dl id='iHmfU'><fieldset id='iHmfU'></fieldset></dl></div>

                    • <bdo id='iHmfU'></bdo><ul id='iHmfU'></ul>
                      <legend id='iHmfU'><style id='iHmfU'><dir id='iHmfU'><q id='iHmfU'></q></dir></style></legend>
                            <tbody id='iHmfU'></tbody>