简体   繁体   English

无法在JPanel中绘制Circle(扩展JComponent)

[英]Unable to draw Circle (extends JComponent) inside JPanel

I am trying to add a custom circle to a JPanel, see this: 我正在尝试向JPanel添加自定义圈子,请参见以下内容:

    graphicPanel = new GraphicPanel();
    JTextArea text = new JTextArea("1233", 5, 10);
    graphicPanel.add(text);

    Circle circle = new Circle();
    circle.setX(30);
    circle.setY(30);
    circle.setDiameter(30);
    graphicPanel.add(circle);
    graphicPanel.repaint();
    graphicPanel.revalidate();

GraphicPanel is just a custom JPanel that doesn't do anything interesting yet (just holds a list that is not used yet) GraphicPanel只是一个自定义的JPanel,尚未执行任何有趣的操作(仅保存一个尚未使用的列表)

GraphicPanel.java GraphicPanel.java

public class GraphicPanel extends JPanel {

    private static final long serialVersionUID = -3813468764873993369L;
    private List<Node> nodes = new ArrayList<Node>();

    public GraphicPanel() {
    }

    public void addNode(Node node) {
        nodes.add(node);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.CYAN);
        g.fillOval((30 - 30 / 2), (30 - 30 / 2), 30, 30);
    }
}

Circle.java Circle.java

public class Circle extends JComponent {
    private static final long serialVersionUID = 628299863960706428L;
    private int x;
    private int y;
    private int diameter;
    private Color color;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDiameter() {
        return diameter;
    }

    public void setDiameter(int diameter) {
        this.diameter = diameter;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.fillOval((x - diameter / 2), (y - diameter / 2), diameter, diameter);
    }
}

The JTextArea appears, the circle does not. 出现JTextArea,但没有圆圈。 If I add the draw code for the circle directly to the paintComponent() of graphicPanel, then a circle appears: 如果我将圆的绘制代码直接添加到graphicPanel的paintComponent()中,则会出现一个圆:

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.CYAN);
        g.fillOval((30 - 30 / 2), (30 - 30 / 2), 30, 30);
}

So the draw code itself should be fine (I considered that maybe the circle is drawn somewhere where I cant see it but this is not the case). 因此,绘制代码本身应该很好(我认为也许是在我看不到的地方绘制了圆圈,但事实并非如此)。 What do I have to change to make the circle appear? 我必须更改什么才能使圆圈出现? I want to draw it like this and not with g.fillOval() in paintComponent() of GraphicPanel. 我想这样绘制,而不是在GraphicPanel的paintComponent()中使用g.fillOval()绘制。 I am using Java 8 我正在使用Java 8

The first thing is that a JPanel has a FlowLayout as a default layout manager. 首先, JPanel具有FlowLayout作为默认布局管理器。

A FlowLayout honors the preferred size of a Component , but Circle doesn't have a specific one, so its size is (0,0). FlowLayout Component的首选大小,但是Circle没有特定的大小,因此其大小为(0,0)。

You may want to override getPreferredSize to give it one, or use a layout manager that will still give a size to your Circle (eg a BorderLayout where you add your component to CENTER ) . 您可能需要重写getPreferredSize来为其指定一个值,或者使用仍会为您的Circle赋予大小的布局管理器(例如,将组件添加到CENTERBorderLayout )。 For later , you may also want to override getMaximumSize and getMinimumSize . 以后,您可能还想覆盖getMaximumSizegetMinimumSize

The second thing is that getX and getY are existing methods from JComponent , that your code overrides (probably not on purpose). 第二件事是getXgetYJComponent现有方法,您的代码将覆盖它们(可能不是故意的)。 Those methods tell the position of this component within its container and would mess up the layout if you play with them (here your Circle is located at 30,30 inside GraphicPanel and gets hidden by the textarea). 这些方法告诉该组件在其容器中的位置,并且如果您使用它们,将会弄乱布局(此处, Circle位于GraphicPanel内部的30,30 GraphicPanel ,并被文本区域隐藏了)。

getX() getX()

the current x coordinate of the component's origin 组件原点的当前x坐标

In the following example, I changed the name and the accessor methods of x and y to avoid overriding getX and getY (there was actually no need to change the names of x and y variables, it is just to keep coherent with those accessor methods names). 在下面的示例中,我更改了xy的名称和访问器方法,以避免覆盖getXgetY (实际上,无需更改x和y变量的名称,只是保持与这些访问器方法名称的一致性)。

A "preferred size" has also been set by adding an overriden getPreferredSize method, computing its optimal size. 通过添加重写的getPreferredSize方法并计算其最佳大小,也可以设置“首选大小”。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;

public class Circle extends JComponent {
    private static final long serialVersionUID = 628299863960706428L;
    private int xCoo;
    private int yCoo;
    private int diameter;
    private Color color;

    @Override
    public Dimension getPreferredSize() {

        return new Dimension(diameter + xCoo, diameter + yCoo);

    }

    /*  @Override
    public Dimension getMinimumSize() {

        return new Dimension(diameter + xCoo, diameter + yCoo);

    }

    @Override
    public Dimension getMaximumSize() {

        return new Dimension(diameter + xCoo, diameter + yCoo);

    }*/

    public int getXCoo() {
        return xCoo;
    }

    public void setXCoo(final int xCoo) {
        this.xCoo = xCoo;
    }

    public int getYCoo() {
        return yCoo;
    }

    public void setYCoo(final int yCoo) {

        this.yCoo = yCoo;
    }

    public int getDiameter() {
        return diameter;
    }

    public void setDiameter(final int diameter) {
        this.diameter = diameter;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(final Color color) {
        this.color = color;
    }

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.fillOval((xCoo - diameter / 2), (yCoo - diameter / 2), diameter, diameter);
    }
}

Also note that JComponent has set/getBackground and set/getForeground methods that you may find useful to set and get background and foreground Color . 还要注意, JComponent具有set / getBackground和set / getForeground方法,您可能会发现它们对于设置和获取背景Color和前景Color很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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