在JFRAME或JPANEL JAVA上绘制功能

发布于 2025-02-03 03:26:28 字数 824 浏览 6 评论 0原文

我想知道是否可以在其他类中使用/制作功能来绘制图像/椭圆形,然后在我们的主要班级中的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2025-02-10 03:26:28

是的,如果我正确地收到您的问题,您可以。
您的示例代码对我有用

frame.add(new Display());

如果我添加到您的方法的末尾,

public static void main(String[] haha)

使用您的代码段,paint(g)方法将永远不会被调用,因为它将使用jpanel的初始化执行,该jpanel将使用初始化进行初始化。显示类(由于继承)。

您可能要创建一个display的实例,该实例将使用Overdridded Paint(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

frame.add(new Display());

to the end of your

public static void main(String[] haha)

method.

With your snippet the paint(g) method will never be called, because it will be executed with the initialization of the JPanel which will be initialized with the initialization the Display class (because of inheritance).

You probably want to create an instance of Display, which automatically initializes the JPanel with the overridden paint(g) method, thus the new Operator.
As the constructor of a JPanel returns a JPanel, the constructor of Display returns a type of JPanel as well, which contains the red circle. This JPanel needs to be added with the add method to your original JFrame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文