如何在JPanel上画圆?Java 2D
我有一个JPanel,我为此设置了一些图像作为背景。我需要在图像上方绘制一堆圆圈。现在,圆将基于某个坐标x,y进行定位,其大小将基于某个整数大小。这就是我上课的时间。
import java.awt.Dimension;import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
class ImagePanel extends JPanel {
private Image img;
CircleList cList; //added this
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
cList = new CircleList(); //added this
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
cList.draw(null); //added this
}
}
如何创建可以执行此操作的方法?
回答:
您的方法可能与此类似,其中您也使用一个类CircleList
来容纳所有圆和绘制例程:
class CircleList{
static class Circle
{
public float x, y, diameter;
}
ArrayList<Circle> circles;
public CirclesList()
{
circles = new ArrayList<Circle>();
}
public void draw(Graphics2D g) // draw must be called by paintComponent of the panel
{
for (Circle c : circles)
g.fillOval(c.x, c.y, c.diameter, c.diameter)
}
}
以上是 如何在JPanel上画圆?Java 2D 的全部内容, 来源链接: utcz.com/qa/416985.html