簡體   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

我正在嘗試創建一個 java 項目,在其中繪制一個圓圈,然后在其上繪制線條。 我曾經寫過很多 Java,但已經有一段時間了。 我的主文件是

-----------------
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


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

        }
    }

您有一個名為 JP 的類。 該類將 5 個值作為參數。

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

但是當你生成一個對象時,你只給它 4 個參數。

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

但是,當您從主類中的 JP 類生成對象時,您需要為其提供許多參數。 從您的父類創建 JP 對象時,還請添加您的第一個Graphics參數。

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

我希望這個答案對你有幫助。

這應該刷新你的記憶:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM