简体   繁体   English

我试图用多条水平线和垂直线绘制一个圆圈,但遇到了障碍。 我怎样才能让这个运行

[英]I'm trying to draw a circle with multiple horizontal and vertical lines and i run into a roadblock. How can i make this run

I'm trying to create a java project where i draw a circle and then draw lines on top of it.我正在尝试创建一个 java 项目,在其中绘制一个圆圈,然后在其上绘制线条。 I used to write java a lot but it's been a while.我曾经写过很多 Java,但已经有一段时间了。 My main file is我的主文件是

-----------------
FirstProject.java
-----------------

    package first.project;

    import java.awt.Graphics;

    public class FirstProject {

        public static void main(String[] args) {
            //
            d = new JP(100, 100, 100, 100);
        }
    }

JP.java JP.java


    package first.project;

    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class JP extends JPanel {

        public void JP(Graphics g, int x, int y, int a, int b) {
            g.drawOval(x, y, a, b);
            JFrame frame = new JFrame("java tutorial");
            frame.getContentPane().add(new JP());
            frame.setSize(300, 300);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);

        }
    }

You have a class named JP.您有一个名为 JP 的类。 This class takes 5 values as a parameters.该类将 5 个值作为参数。

public void JP(Graphics g, int x, int y, int a, int b)

But when you're generating an object, you only give it 4 parameters.但是当你生成一个对象时,你只给它 4 个参数。

d = new JP(100, 100, 100, 100);

But when you're generating an object from your JP class in your main class, you give it a number of parameters.但是,当您从主类中的 JP 类生成对象时,您需要为其提供许多参数。 Please also add your first Graphics parameter when creating a JP object from your parent class.从您的父类创建 JP 对象时,还请添加您的第一个Graphics参数。

new JP(Graphics g, 100, 100, 100, 100);

I hope this answer will help you.我希望这个答案对你有帮助。

This should refresh your memory:这应该刷新你的记忆:

import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class JP extends JComponent {

    public static void main(String[] args) {
        JFrame frame = new JFrame("java tutorial");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        frame.getContentPane().add(new JP(100, 100, 100, 100));

        frame.setVisible(true);
    }

    public JP(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    private int x;
    private int y;
    private int width;
    private int height;

    @Override
    public void paint(Graphics g) {
        g.drawOval(x, y, width, height);
        g.drawLine(x + height / 2, y, x + height / 2, y + width);
        g.drawLine(x, y + width / 2, x + height, y + width / 2);
    }

}

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

相关问题 当我运行代码时,菜单显示为垂直而不是水平,该如何更改? - When i run my code the menus appear vertical not horizontal how do i change this? 我正在尝试使用多行扫描仪并尝试使用另一个字符串 java 更改输入 - I'm trying to use the scanner with multiple lines and trying to make change the input with another string java 我正在尝试制作一个简单的刽子手游戏,但我遇到了这个错误:字符串索引超出范围:第19行为0 - I'm trying to make a simple hangman game, but I run into this error: String index out of range: 0 at line 19 如何使此循环运行? - How can I make this loop run? 如何在Dockerfile中运行多个JAR? - How can I run multiple JARs in Dockerfile? 如何使用 Intellij 以多行作为参数运行 java Main? - How can I run a java Main with multiple lines as its argument using Intellij? 按下按钮时如何绘制垂直线? - How can I draw vertical line when I press the button? 如何为多个 JButton 运行代码? - How can I run a code for multiple JButtons? 如何在不覆盖 paint(...) 或 paintComponent(...) 的情况下在 JFrame 中绘制圆圈 - How can i draw circle in JFrame without overriding paint(...) or paintComponent(...) 如何设置JLabel的垂直/水平高度? (Netbeans的) - How can I set the vertical/horizontal height of a JLabel? (Netbeans)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM