简体   繁体   English

如何使用 java.awt.Graphics 画圆?

[英]How to draw circle using java.awt.Graphics?

I'm just trying to draw circle using the drawOval() method and it shows only small square when I run the program.我只是想用 drawOval() 方法画圆,当我运行程序时它只显示小方块。 I was trying to add the constructor to the Surface class but it didn't work as well.我试图将构造函数添加到 Surface 类,但效果不佳。 Here is the code that I've been made:这是我制作的代码:

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

you didn't set the components size, hence the component is too small to display the circle.您没有设置组件大小,因此组件太小而无法显示圆圈。

adjust the components size and the circle will be shown properly调整组件大小,圆圈将正确显示

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

you can djust the compoinent's seize by either setting it or override its getPrefferedSize() -methode您可以通过设置它或覆盖它的getPrefferedSize() -方法来调整组件的抓取

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

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

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