使用滑块处理程序用弧填充椭圆

时间:2014-10-22 00:55:47

标签: java graphics 2d ellipse

我想知道如何在用户调整滑块值时用度数填充椭圆内部的弧。滑块侦听器位于与弧和椭圆不同的文件中。

1 个答案:

答案 0 :(得分:1)

  1. 在进行任何自定义绘制之前,先调用super.paintComponent,这样可以防止生成任何可能的绘制工件,虽然时髦,但并不是很好。有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting
  2. 当滑块状态发生变化时,将当前值传递给用于绘制圆弧的组件并在其上调用reapint
  3. 小心地将组件与自定义绘画与其他组件(弧形窗格上的滑块)混合,因为如果您不希望它被其他组件覆盖,则很难确定如何执行自定义绘画...
  4. Arc

    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 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 FillEllipse {
    
        public static void main(String[] args) {
            new FillEllipse();
        }
    
        public FillEllipse() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    final ArcPane arcPane = new ArcPane();
                    final JSlider slider = new JSlider(0, 360);
                    slider.addChangeListener(new ChangeListener() {
                        @Override
                        public void stateChanged(ChangeEvent e) {
                            int value = slider.getValue();
                            arcPane.setAngle(value);
                        }
                    });
                    slider.setValue(0);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(arcPane);
                    frame.add(slider, BorderLayout.SOUTH);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ArcPane extends JPanel {
    
            private int angel;
    
            public ArcPane() {
            }
    
            public void setAngle(int angel) {
    
                this.angel = angel;
                repaint();
    
            }
    
            public int getAngel() {
                return angel;
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
    
                int diameter = Math.min(getWidth(), getHeight());
                int x = (getWidth() - diameter) / 2;
                int y = (getHeight() - diameter) / 2;
    
                g2d.setColor(Color.RED);
                g2d.fillArc(x, y, diameter, diameter, 0, getAngel());
                g2d.dispose();
            }
    
        }
    
    }