Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing应用程序-Arraylist和circleobjects,如何在jframe内的Jpanel中绘制它们?_Java_Swing_Arraylist - Fatal编程技术网

Java Swing应用程序-Arraylist和circleobjects,如何在jframe内的Jpanel中绘制它们?

Java Swing应用程序-Arraylist和circleobjects,如何在jframe内的Jpanel中绘制它们?,java,swing,arraylist,Java,Swing,Arraylist,我正在制作一个简单的应用程序,可以让你用按钮显示不同的圆形物体 问题是我不知道如何将circleobjects打印到jPanel中。 当您第一次运行程序时,第一个circleobject应该出现在jPanel中。这是我的circleclass: public class Circle { private int height; private int width; private Color color; public Circle (int height, int width, Colo

我正在制作一个简单的应用程序,可以让你用按钮显示不同的圆形物体

问题是我不知道如何将circleobjects打印到jPanel中。 当您第一次运行程序时,第一个circleobject应该出现在jPanel中。这是我的circleclass:

public class Circle {
private int height;
private int width;
private Color color;


public Circle (int height, int width, Color color){
this.height = height;
this.width = height;
this.color = color;
}
public int getHeight() {
    return height;
}
public void setHeight(int height) {
    this.height = height;
}
public Color getColor() {
    return color;
}
public void setColor(Color color) {
    this.color = color;
}
public int getWidth() {
    return width;
}
public void setWidth(int width) {
    this.width = width;
}
}
这是GUI代码的第一部分。我在arraylist中创建了5个圆形对象

public class CircleGUI extends javax.swing.JFrame{

public ArrayList<Circle> circles = new ArrayList<Circle>();

public CircleGUI(){
    initComponents();
    circles.add(new Circle(15, 15, Color.blue));
    circles.add(new Circle(20, 15, Color.black));
    circles.add(new Circle(30, 10, Color.green));
    circles.add(new Circle(20, 10, Color.orange));
    circles.add(new Circle(35, 35, Color.red));     
    }
公共类CircleGUI扩展了javax.swing.JFrame{
public ArrayList circles=new ArrayList();
公共电路{
初始化组件();
添加(新的圆圈(15,15,颜色.蓝色));
添加(新的圆圈(20,15,颜色.黑色));
添加(新的圆圈(30,10,Color.green));
添加(新的圆圈(20,10,颜色为橙色));
添加(新的圆圈(35,35,颜色.红色));
}

现在,如何使我的第一个对象出现在屏幕截图上标记的jPanel中?

您将必须覆盖jPanel的
paintComponent(Graphics)
方法


然后根据当前圆对象包含的数据在图形对象上绘制。

您必须覆盖JPanel的
paintComponent(Graphics)
方法

然后,根据当前圆对象包含的数据在图形对象上进行绘制。

签出。它完全按照您的要求进行绘制(除了绘制矩形)

它实际上显示了两种方法:

  • 从ArrayList绘制对象(这是您想要的)
  • 在BuffereImage上绘制对象
  • 您案例中的基本绘画代码是:

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
    
        //  Custom code to paint all the Rectangles from the List
    
        for (DrawingArea.ColoredRectangle cr : coloredRectangles)
        {
            g.setColor( cr.getForeground() );
            Rectangle r = cr.getRectangle();
            g.drawRect(r.x, r.y, r.width, r.height);
        }
    }
    
    当然,您可以绘制椭圆并使用Circle类查看。它完全可以完成您想要的操作(除了绘制矩形)

    它实际上显示了两种方法:

  • 从ArrayList绘制对象(这是您想要的)
  • 在BuffereImage上绘制对象
  • 您案例中的基本绘画代码是:

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
    
        //  Custom code to paint all the Rectangles from the List
    
        for (DrawingArea.ColoredRectangle cr : coloredRectangles)
        {
            g.setColor( cr.getForeground() );
            Rectangle r = cr.getRectangle();
            g.drawRect(r.x, r.y, r.width, r.height);
        }
    }
    

    当然,您可以绘制椭圆并使用Circle类

    在代码中的何处添加paintcomponent方法?创建一个扩展JPanel的新类,在那里重写paintcomponent,并使用该类的实例而不是JPanel。我创建了一个名为draw的新类,exending JPanel,带有@override,然后我有:public void paintCcomponent(Graphics.g){super.paintComponent(g);}}}现在,我已经尝试了从jPanel.getGraphics到jPanel.paintComponent的所有方法,但我都没有弄明白。请参见这里的示例:我在代码中的何处添加paintComponent方法?创建一个扩展jPanel的新类,在那里重写paintComponent,我创建了一个名为draw的新类,exending JPanel,带有@override,然后我有了:public void paintComponent(Graphics.g){super.paintComponent(g);}Now,我尝试了从jPanel.getGraphics到jPanel.paintComponent的所有方法,但都没有弄明白。请看下面的示例:我应该在新类中这样做吗?另外,drawingArea是我的jpanel和ColoredRectangle是类?我应该在新类中这样做吗?还有,drawingArea是我的jpanel和ColoredRectangle是类吗?