在JFRAME或JPANEL JAVA上绘制功能
我想知道是否可以在其他类中使用/制作功能来绘制图像/椭圆形,然后在我们的主要班级中的Paint Public Void中调用它。
如果我有
public class Trydraw{
public void drawrcircle(Graphics g){
g.setColor(Color.RED);
g.drawOval(0, 0, 20,20);
g.fillOval(0,0,20,20);
}
}
,然后以这种方式称呼它,
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.*;
public class Display extends JPanel{
public static void main(String[]haha){
JFrame frame = new JFrame();
frame.setSize(800, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
Trydraw l = new Trydraw();
l.drawrcircle(g);
}
}
感谢您的未来帮助。
I wanted to know if it is possible to use/make a function in another Class to draw an image/oval and then call it in the paint public void in our main Class.
If I have
public class Trydraw{
public void drawrcircle(Graphics g){
g.setColor(Color.RED);
g.drawOval(0, 0, 20,20);
g.fillOval(0,0,20,20);
}
}
And then call it here this way
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.*;
public class Display extends JPanel{
public static void main(String[]haha){
JFrame frame = new JFrame();
frame.setSize(800, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
Trydraw l = new Trydraw();
l.drawrcircle(g);
}
}
Thanks for your future help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果我正确地收到您的问题,您可以。
您的示例代码对我有用
如果我添加到您的方法的末尾,
。
使用您的代码段,
paint(g)
方法将永远不会被调用,因为它将使用jpanel的初始化执行,该
jpanel
将使用初始化
类(由于继承)。进行初始化。显示
您可能要创建一个
display
的实例,该实例将使用OverdriddedPaint(g)
方法自动初始化Jpanel
,因此>新的
操作员。由于
jpanel
的构造函数返回jpanel
,显示的构造函数也返回一种jpanel
的类型,其中包含红色圆圈。此jpanel
需要在您的原始jframe
中添加添加
方法。Yes you can, if I get your question correctly.
Your sample code works for me if I add
to the end of your
method.
With your snippet the
paint(g)
method will never be called, because it will be executed with the initialization of theJPanel
which will be initialized with the initialization theDisplay
class (because of inheritance).You probably want to create an instance of
Display
, which automatically initializes theJPanel
with the overriddenpaint(g)
method, thus thenew
Operator.As the constructor of a
JPanel
returns aJPanel
, the constructor of Display returns a type ofJPanel
as well, which contains the red circle. ThisJPanel
needs to be added with theadd
method to your originalJFrame
.