[英]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.