如何在JPanel上画圆?Java 2D

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1836440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:09:00  来源:igfitidea点击:

How to draw circle on JPanel? Java 2D

javauser-interfaceswingjava-2d

提问by user69514

I have a JPanel for which I set some image as the background. I need to draw a bunch of circles on top of the image. Now the circles will be positioned based on some coordinate x,y, and the size will be based on some integer size. This is what I have as my class.

我有一个 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
    }
}

How can I create some method that can performed this?

如何创建一些可以执行此操作的方法?

采纳答案by Hyman

Your approach can be something similar to this, in which you use a class CircleListto hold all the circles and the drawing routine too:

您的方法可能与此类似,其中您也使用一个类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)
  }
}

回答by jitter

Easiest thing to do would be to place something along these lines into your paintComponentmethod.

最简单的方法就是将这些内容放到你的paintComponent方法中。

int x = ...;
int y = ...;
int radius = ...;
g.drawOval(x, y, radius, radius);

回答by camickr

Well, you will probably want to create an ArrayList to store the information about the circles to be drawn. Then when the paintComponent() method is invoked you just loop through the ArrayList and draw the circles.

好吧,您可能想要创建一个 ArrayList 来存储有关要绘制的圆的信息。然后,当调用paintComponent() 方法时,您只需遍历ArrayList 并绘制圆圈。

Custom Painting Approachesshows how this might be done for a rectangle. You can modify the code for an oval as well you would probably add methods to update the Array with the location information rather than by doing it dynamically.

自定义绘画方法展示了如何为矩形完成此操作。您也可以修改椭圆的代码,您可能会添加方法来使用位置信息更新数组,而不是动态更新。

回答by oxbow_lakes

Have you looked at JXLayer? It's an awesome library that allows you to layer special painting on top of any GUI element in an obvious way. I believe that will be included in the main java libraries for JDK7

你看了JXLayer吗?这是一个很棒的库,它允许您以明显的方式在任何 GUI 元素之上分层特殊绘画。我相信它将包含在 JDK7 的主要 Java 库中