Circle not showing up in JPanel

五迷三道 提交于 2020-05-09 07:53:44

问题


So I'm trying to draw circles inside of the inner panel. I have a class, paintTimeUnit that creates the circle inside the panel given an xy coordinate pair but whenever I create a new paintTimeUnit object and add it to the panel it doesn't seem to be showing up.

public class paintTimeUnit extends JPanel {


    private static final long serialVersionUID = 1L;
    private int value;
    private int xlocation;
    private int ylocation;

    public paintTimeUnit(int x, int y) {
        value = 0;
        xlocation = x;
        ylocation = y;
    }

    public void paint(Graphics g) {    
        g.drawOval(xlocation, ylocation, 100, 100);  
        g.setColor(Color.RED);    
    }  

    public int getValue() {
        return value;
    }

    public void setValue(int t) {
        value = t;
    }

}

And the implementation of it is as follows:

           JPanel Panel = new JPanel();
           ...
           JPanel inner = new JPanel();
           inner.setLayout(null);
           inner.setSize(325, 570);
           inner.setBackground(null);
           inner.setLocation(500, 350);
           inner.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));
           inner.setVisible(true);

           paintTimeUnit hour1 = new paintTimeUnit(600, 400);
           hour1.setValue(1);
           hour1.setVisible(true);
           inner.add(hour1);

           //----Containers to Panel/Panel to Frame---------
           Panel.add(inner);
           ...
           frame.add(Panel, BorderLayout.CENTER);

回答1:


To have a component that draws a circle at a given location, properly override paintComponent as explained in oracle's tutorial:

class PaintTimeUnit extends JPanel {

    private final int xlocation, ylocation;
    private static final int W = 500, H = 300, RADIUS = 50;

    public PaintTimeUnit(int x, int y) {
        xlocation = x;
        ylocation = y;
        setPreferredSize(new Dimension(W, H));
    }

    @Override
    public void paintComponent(Graphics g) { //override paintComponent for custom painting
        super.paintComponent(g); //call super
        g.setColor(Color.RED);  //set painting color
        g.drawOval(xlocation, ylocation, RADIUS, RADIUS); //draw circle
    }
} 

However, as advised, it may better to have a container that draws a bunch of circles.
To achieve it you need to add a collection to store all circles to be painted such as

List<Point> circleCenters== new ArrayList<>()

You would also need to add points to that collection:

 void addCircle(int centerX, int centerY){
    circleCenters.add(new Point(centerX, centerY));
 }

and have paintComponent draw circles based on their stored centers:

public void paintComponent(Graphics g) { //override paintComponent for custom painting
    super.paintComponent(g); //call super
    g.setColor(Color.RED);  //set painting color
    for(Point center : circleCenters){
       g.drawOval(center.x, center.y, RADIUS, RADIUS); //draw circle
    }
}

Put it all together:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
class PaintTimeUnit extends JPanel {

    private final List<Point> circleCenters;
    private static final int W = 500, H = 300, RADIUS = 50;

    public PaintTimeUnit() {
        circleCenters = new ArrayList<>();
        setPreferredSize(new Dimension(W, H));
    }

    @Override
    public void paintComponent(Graphics g) { //override paintComponent for custom painting
        super.paintComponent(g); //call super
        g.setColor(Color.RED);  //set painting color
        for(Point center : circleCenters){
            g.drawOval(center.x, center.y, RADIUS, RADIUS); //draw circle
        }
    }

    void addCircle(int centerX, int centerY){
        circleCenters.add(new Point(centerX, centerY));
    }
}

And use it :

 PaintTimeUnit ptu= new PaintTimeUnit();
 //add 3 circles
 ptu.addCircle(90,90);
 ptu.addCircle(150,150);
 ptu.addCircle(210,90);


(Run it online)

来源:https://stackoverflow.com/questions/61431280/circle-not-showing-up-in-jpanel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!