【问题标题】:How to draw circle using java.awt.Graphics?如何使用 java.awt.Graphics 画圆?
【发布时间】:2016-09-25 02:42:07
【问题描述】:

我只是想用 drawOval() 方法画圆,当我运行程序时它只显示小方块。我试图将构造函数添加到 Surface 类,但效果不佳。 这是我制作的代码:

    package swing22;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;


    public class MyFrame {

        JFrame frame = new JFrame(" Test Frame ");
        JPanel panel = new JPanel();

        JButton button = new JButton("CLICK");
        JLabel label = new JLabel(" 33 ");

        public MyFrame(){

            gui();

        }

        public void gui(){

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

            panel.setBackground(Color.GREEN);
            panel.add(button);
            panel.add(label);
            panel.add(new Surface());

            frame.add(panel, BorderLayout.CENTER);

            button.addActionListener(new Action());

            frame.pack();
            frame.setSize(300,300);
            frame.setVisible(true);
            frame.setResizable(false);

        }


        class Action implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent arg0) {
                label.setText("new value");
            }
        }

    }


    class Surface extends JPanel {


            private void doDrawing(Graphics g) {

                Graphics2D g2d = (Graphics2D) g;

                    g.setColor(Color.red);
                    g.drawOval(80, 80, 30, 30);
                    g.fillArc(140, 140, 30, 30, 0, 90);
            }

            @Override
            public void paintComponent(Graphics g) {

                super.paintComponent(g);
                doDrawing(g);
            }
        }

【问题讨论】:

  • 这是布局和首选尺寸问题。您的 Surface 看起来非常小,例如 [0, 0],因为这是它的首选尺寸。见this similar question and answer
  • 非常感谢,我只是简单地将这一行 setPreferredSize(new Dimension(200, 200)); 添加到 Surface 构造函数中,它可以完美运行!
  • @MyNet,更好的解决方案是覆盖getPreferredSize() 方法以返回正确的维度。
  • 对于example

标签: java swing graphics charts geometry


【解决方案1】:

您没有设置组件大小,因此组件太小而无法显示圆圈。

调整组件大小,圆圈会正确显示

public void gui(){
    ....
    Surface s = new Surface();
    s.setPreferredSize(new Dimension(200, 200));
    panel.add(new Surface());
    ...
}

您可以通过设置或覆盖其getPrefferedSize()-方法来调整组件的抓取

【讨论】:

    【解决方案2】:
    use this class (Graphiic) and it's method (Draw_Circle)
    
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    
    public class Graphiic
    {   
        public Graphics GClass;
        public Graphics2D G2D;
        public  void Draw_Circle(JFrame jf,int radius , int  xLocation, int             yLocation)
        {
            GClass = jf.getGraphics();
            GClass.setPaintMode();
            GClass.setColor(Color.MAGENTA);
            GClass.fillArc(xLocation, yLocation, radius, radius, 0, 360);           
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 2020-03-24
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多