如何在位于JFrame内部的JPanel中绘制图形。

huangapple go评论79阅读模式
英文:

How to draw shape inside of JPanel that is inside JFrame

问题

我在使用Netbeans GUI添加了一个JPanel但在其中绘制形状时遇到了问题现在我不知道在哪里添加绘制该JPanel内部圆形的代码以及如何在目前为空的JPanel中插入并调用它以等待绘制这个形状我已经将目标JPanel设置为流布局

Netbeans Designer创建了一个大的类在这个类中我有整个带有这个JPanel的框架我希望将它保留在其中因为我实际上无法以其他任何方式添加它因为Designer不允许我更改主要的initComponents方法而所有组件现在都在其中我已经阅读了教程和先前的帖子但没有人真正在使用Netbeans Designer时遇到过这个问题
因此有人可以帮助我在这个框架类中添加适当的方法以及如何从我要绘制的JPanel中调用它该JPanel的大小为50x50像素

所以根据@Abra的建议我修改了一些代码

所以我创建了一个新的Circle类并进行了一些调整因为我不想创建一个新的框架而是将其放在JPanel中

```java
public class Circle extends JPanel {
    private Color color;

    public Circle(Color color) {
        this.color = color;
        setPreferredSize(new Dimension(30, 30));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.drawOval(0, 0, getWidth(), getHeight());
    }
}

然后,我在Designer中打开了JPanel,并添加了运行它的代码,将其放在initComponents方法中,就像这样:

circlePanel.setPreferredSize(new Dimension(40, 40));
new Circle(Color.RED).showGUI();
PanelDS.add(circlePanel);

其中,circlePanel是绘制的目标,位于PanelDS内部。虽然这种方式不起作用,但Netbeans在代码中没有显示错误。另外,如何将颜色传递给Circle类呢?


<details>
<summary>英文:</summary>

I have issue with drawing shapes inside of JPanel that I already added using Netbeans GUI. Now, I have no idea where to add code for drawing a circle inside of that JPanel and how to insert and call it in the JPanel that is sitting empty now, waiting for this shape to be drawn. I already set up destination JPanel to be Flow layout.
Netbeans Designer created a big class in which I have entire frame with this JPanel, and I want to keep it inside of it as I can&#39;t really add it any other way because Designer doesn&#39;t let me change main initComponents method in which all components are sitting now. I have been reading tutorials and previous posts but noone really encountered this using Netbeans Designer. 
SO can someone just help me with adding proper method in this frame class and how to call it from JPanel I want to draw in. JPanel is 50x50 pixels.

So as per @Abra, I changed some code:

so I made a new Circle Class, adjusted it a bit as I don&#39;t want to create a new frame but put this in JPanel.
 

                                                
    public class Circle extends JPanel {
    Color color;
    public void circle(Color color)    {
        this.color = color;
        setPreferredSize(new Dimension (30,30));
    }
    public void paintComponent(Graphics g)    {
        super.paintComponent(g);
        g.drawOval(0, 0, r, r);
        g.setColor(color);
    }
    private void showGUI() {
        JPanel panel = new JPanel();
        panel.add(this, FlowLayout.CENTER);
        panel.setVisible(true);
    }       
    }  
                                                                                                                            

Then I opened JPanel in Designer, and added code to run it, in initComponents method like this: 

  

    circlePanel.setPreferredSize(new java.awt.Dimension(40, 40));
        new Circle().showGUI();
        PanelDS.add(circlePanel); 
                       

                                                          
circlePanel is destination for this drawing and is inside PanelDS itself. It doesn&#39;t work this way tho, but Netbeans shows no errors in code. Additionally, how can I forward color to circle class.



</details>


# 答案1
**得分**: 1

```java
为了在 `JPanel` 上绘图,您需要重写 `JPanel` 的 `paintComponent()` 方法。为了重写该方法,您需要创建一个继承自 `JPanel` 的类。我认为目前不存在一个能为您生成所需代码的GUI设计工具。因此,您需要编写一个继承自 `JPanel` 的类的代码。

这是一个最简示例。它显示一个蓝色的圆。
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Drawing2 extends JPanel {
    private JFrame frame;

    public Drawing2() {
        setPreferredSize(new Dimension(100, 100));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillOval(25, 25, 50, 50);
    }

    private void showGui() {
        frame = new JFrame("Drawing");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(this, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Drawing2().showGui();
    }
}

当您运行上述代码时,您应该看到以下内容。

如何在位于JFrame内部的JPanel中绘制图形。


<details>
<summary>英文:</summary>

In order to draw on a `JPanel` you need to override the `paintComponent()` method of `JPanel`. In order to override the method, you need to create a class that extends `JPanel`. I don&#39;t think that there exists a GUI designer that can generate the required code for you. So you have to write the code of the class that extends `JPanel`.

Here is a minimal example. It displays a blue circle.
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Drawing2 extends JPanel {
    private JFrame  frame;

    public Drawing2() {
        setPreferredSize(new Dimension(100, 100));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillOval(25, 25, 50, 50);
    }

    private void showGui() {
        frame = new JFrame(&quot;Drawing&quot;);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(this, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Drawing2().showGui();
    }
}

Here's what you should see when you run the above code.

如何在位于JFrame内部的JPanel中绘制图形。

huangapple
  • 本文由 发表于 2020年9月22日 21:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64011022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定