Java 制作一个 drawCircle 方法调用 drawOval

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3348856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:29:57  来源:igfitidea点击:

Java making a drawCircle method calling drawOval

javauser-interfacegraphics

提问by Raptrex

I need to make a drawCircle method that looks like

我需要制作一个看起来像的 drawCircle 方法

public void drawCircle(int x, int y, int radius)

that draws a circle with that center and radius. The drawCircle method needs to call drawOval. I am not sure how I can call drawOval from my drawCircle method without passing Graphics to it. Is this possible?

以该中心和半径绘制一个圆。drawCircle 方法需要调用 drawOval。我不确定如何从我的 drawCircle 方法调用 drawOval 而不将 Graphics 传递给它。这可能吗?

Heres what I have:

这是我所拥有的:

import java.awt.*;   
import javax.swing.*;

class test
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(new MyPanel());
        frame.pack();
        frame.setVisible(true);
    }
}
class MyPanel extends JPanel
{

    MyPanel()
    {
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(250,250));
    }

    public void paintComponent(Graphics page)
    {
        super.paintComponent(page);
        drawCircle(50,50,20);
    }

    private void drawCircle(int x, int y, int radius)
    {
        drawOval(x - radius, y - radius, radius*2, radius*2);
    }
}

采纳答案by S73417H

You can get the graphics context by calling getGraphics()on a swing component. But i would still create my drawing methods to accept the graphics context.

您可以通过调用getGraphics()Swing 组件来获取图形上下文。但我仍然会创建我的绘图方法来接受图形上下文。

For instance

例如

private void drawCircle(Graphics g, int x, int y, int radius) {
   g.fillOval(x-radius, y-radius, radius*2, radius*2)
}

Alternatively,

或者,

private void drawCircle(int x, int y, int radius) {
  getGraphics().fillOval(x-radius, y-radius, radius*2, radius*2)
}

Be aware of the fact that getGraphics()can return nullhowever. You are much better off calling your drawCircle() method from within the paint() method and passing it the Graphics context.

但是要注意getGraphics()可以返回的事实null。最好从paint() 方法中调用drawCircle() 方法并将其传递给Graphics 上下文。

E.g.

例如

public void paint(Graphics g) {
  super.paint(g);
  drawCircle(g, 10, 10, 5, 5);
}

回答by Dreamspace President

import java.awt.Shape;
import java.awt.geom.Ellipse2D;

public static Shape getCircle(
                        final double x,
                        final double y,
                        final double r
                             ) {

    return new Ellipse2D.Double(x - r, y - r, r * 2, r * 2);
}

Advantages:

好处:

  • You don't have to pass or get the Graphics context.
  • You can choose whether to draw or fill the circle without changing the Method (e.g. do g.draw(getCircle(x, y, r));or g.fill...in your paint() Method).
  • You use AWT draw Method syntax to actually draw/fill the circle.
  • You get a circle in double precision coordinates.
  • 您不必传递或获取 Graphics 上下文。
  • 您可以在不更改方法的情况下选择是绘制还是填充圆(例如 dog.draw(getCircle(x, y, r));g.fill...在您的paint() 方法中)。
  • 您使用 AWT draw Method 语法来实际绘制/填充圆。
  • 你得到一个双精度坐标的圆。

You said that "The drawCircle method needs to call drawOval.", but maybe you were just not aware of the alternative.

您说“drawCircle 方法需要调用 drawOval。”,但也许您只是不知道替代方法。