java - 圆圈未显示在 JPanel 中
问题描述
所以我试图在inner
面板内画圆圈。我有一个类,paintTimeUnit
它在给定 xy 坐标对的情况下在面板内创建圆圈,但是每当我创建一个新paintTimeUnit
对象并将其添加到面板时,它似乎都没有出现。
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;
}
}
它的实现如下:
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);
解决方案
要让组件在给定位置绘制圆,请paintComponent
按照 oracle教程中的说明正确覆盖:
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
}
}
但是,正如建议的那样,最好有一个绘制一堆圆圈的容器。
要实现它,您需要添加一个集合来存储所有要绘制的圆圈,例如
List<Point> circleCenters== new ArrayList<>()
您还需要向该集合添加点:
void addCircle(int centerX, int centerY){
circleCenters.add(new Point(centerX, centerY));
}
并paintComponent
根据其存储的中心绘制圆圈:
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
}
}
把它们放在一起:
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));
}
}
并使用它:
PaintTimeUnit ptu= new PaintTimeUnit();
//add 3 circles
ptu.addCircle(90,90);
ptu.addCircle(150,150);
ptu.addCircle(210,90);
(在线运行)
推荐阅读
- android-intent - 如何从用户输入(语音)中捕获触发词?
- javascript - 如何解决 Javascript 中未定义的数组属性?
- css - 为什么我的 css 卡在不同的位置高度
- python - 如何使用此公式计算购买的每月付款?
- openstack - 在端口创建中是否可以有“条件”?
- python-3.x - 如何使用 Python 通过 IBAPI 从 TWS 导入基础数据
- python - Python - 抓取网页以获取仅在滚动后出现的信息
- go - 如何将当前时间格式化为 YYYY-MM-DDTHH:MM:SSZ
- grouping - Dask:具有 nlargest 的 Groupby 自动引入索引并且不允许 reset_index()
- python - 如何判断函数来自哪个模块?