尝试使用参数中的Java变量绘制圆 [英] Trying to draw a circle in Java using variable in parameters

查看:161
本文介绍了尝试使用参数中的Java变量绘制圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难完成一个相当简单的任务,即取一个圆的直径然后绘制它.到目前为止,这是我的代码.

I am having trouble doing a rather simple task of taking in the diameter of a circle and then drawing it. Here is my code so far.

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

public class Shapes extends JFrame
{
    double diameter;

    public Shapes()
    {
        setSize(600,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public void getDiameter()
    {
        String input = JOptionPane.showInputDialog("What is the diameter of the circle?");
        diameter = Double.parseDouble(input);

        Shapes gui = new Shapes();
        gui.setVisible(true);
    }

    public static void main(String[] args) 
    {       
        Shapes app = new Shapes();
        app.getDiameter();
    }

    public void paint(Graphics canvas)
    {
        canvas.drawOval(50, 50, (int)diameter, (int)diameter);
    }

}


当我运行它时,它会弹出Jframe窗口,但是什么都没画,所以我猜直径的值永远不会传递给paint方法.有人可以帮我这个忙吗?谢谢.


When I run it, it brings up the Jframe window, but nothing is drawn, so I'm guessing the value of diameter is never passed to the paint method. Can someone help me get this to work? Thanks.

推荐答案

您的程序实际上是在创建两个Shapes对象,其中之一的直径字段设置正确但未显示,而另一个保留了直径的默认值的0,并显示出来.

Your program is creating two Shapes objects actually, one of which has the diameter field set correctly but is not being displayed, and the other, which retains diameter's default value of 0 and which is displayed.

建议:

  1. 不直接在JFrame中绘制,而是在paintComponent(Graphics g)方法中重写由JFrame持有并显示在其上的JPanel.造成这种情况的原因很多,但有一个原因,因为paint(...)方法不仅负责绘制组件,而且还负责绘制其边框和子元素,因此,当paint(...)尝试绘制GUI的子元素和子元素时,这将防止您引起问题.边界.鉴于Swing组件默认使用双重缓冲,它还将帮助您的动画(肯定会很快完成)平滑.
  2. 始终在JPanel的paintComponent替代中调用super.paintComponent(g)方法.这将允许Swing删除需要删除的图像.
  3. 不创建两个Shapes对象,而仅创建一个.这将大大简化事情,并允许您设置一个唯一的重要对象的直径值.
  4. 更改直径字段的值后,在GUI上调用repaint,以便调用显示的JPanel的paintComponent.
  1. Don't draw directly in a JFrame, but rather in the paintComponent(Graphics g) method override of a JPanel that is held by and displayed in the JFrame. There are many reasons for this, but for one, since the paint(...) method is not only responsible for painting a component but also its borders and children, this will prevent you from causing problems when paint(...) tries to paint a GUI's children and borders. It also will help your animations (which surely you will be doing soon) to be smooth given Swing component's default use of double buffering.
  2. Always call the super.paintComponent(g) method within your JPanel's paintComponent override. This will allow Swing to erase images that need to be erased.
  3. Don't create two Shapes objects, but rather only one. This will simplify things greatly and will allow you to set the diameter value of the one and only object of importance.
  4. After changing the diameter field's value, call repaint on your GUI so that the displayed JPanel's paintComponent will be called.

这篇关于尝试使用参数中的Java变量绘制圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆