简体   繁体   中英

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. I used to write java a lot but it's been a while. 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


    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. This class takes 5 values as a parameters.

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.

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. Please also add your first Graphics parameter when creating a JP object from your parent class.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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