Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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 如何将多个paintComponent()添加到框架中?_Java_Swing_Frame_Paintcomponent_Custom Painting - Fatal编程技术网

Java 如何将多个paintComponent()添加到框架中?

Java 如何将多个paintComponent()添加到框架中?,java,swing,frame,paintcomponent,custom-painting,Java,Swing,Frame,Paintcomponent,Custom Painting,这是我的主要课程: package testgame; import java.awt.EventQueue; import javax.swing.JFrame; public class Game extends JFrame { public static JFrame frame = new JFrame("Just a test!"); public static void LoadUI() { frame.setDefaultCloseOperation(frame.

这是我的主要课程:

package testgame;

import java.awt.EventQueue;
import javax.swing.JFrame;

public class Game extends JFrame {

public static JFrame frame = new JFrame("Just a test!");

public static void LoadUI() {
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setSize(550, 500);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true); }

public static void main(String[] args) {
    LoadUI();
    frame.add(new Circles());
    }
}
这个类处理我想画的东西:

package testgame;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Circles extends JPanel {

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    drawBubbles(g); }

public void drawBubbles(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    RenderingHints rh
            = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, 
            RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHints(rh);
    int x, y, size;
    x = (int) (Math.random() * 500) + 15;
    y = (int) (Math.random() * 450) + 15;
    size = (int) (Math.random() * 50) + 25;
    g2d.setColor(Color.GREEN);
    g2d.drawOval(x, y, size, size);
    g2d.fillOval(x, y, size, size); }
}
如果我再加一个

 frame.add(new Circles());

什么也没发生。我认为这与框架的布局有关,但气泡的坐标是随机的,因此我不确定如何使用它。

在这种情况下,我使用的是5的固定大小数组,您可以将其更改为更大的固定大小数组或数组列表,如中所示

对于您的特殊情况,我将创建一个Circle类,该类可能包含每个圆的数据,即坐标和大小

然后创建一个CirclePane类,该类将在单个paintComponent方法中绘制所有圆

最后,主类将有一个JFrame,其中可能添加了圆环烷

考虑到以上提示,您可能会得到如下结果:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CircleDrawer {
    private JFrame frame;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CircleDrawer()::createAndShowGui); //We place our program on the EDT
    }
    
    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());
        
        CirclePane circle = new CirclePane(5); //We want to create 5 circles, we may want to add more so we change it to 10, or whatever number we want
        
        frame.add(circle);
        
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    //Data class
    class Circle {
        private Point coords;
        private int size;
        
        public Circle(Point coords, int size) {
            this.coords = coords;
            this.size = size;
        }
        
        public Point getCoords() {
            return coords;
        }
        public void setCoords(Point coords) {
            this.coords = coords;
        }
        public int getSize() {
            return size;
        }
        public void setSize(int size) {
            this.size = size;
        }
    }
    
    //The drawing class
    @SuppressWarnings("serial")
    class CirclePane extends JPanel {
        private int numberOfCircles;
        private Circle[] circles;

        public CirclePane(int numberOfCircles) {
            this.numberOfCircles = numberOfCircles;
            
            circles = new Circle[numberOfCircles];
            
            for (int i = 0; i < numberOfCircles; i++) {
                Point coords = new Point((int) (Math.random() * 500) + 15, (int) (Math.random() * 450) + 15); //We generate random coords
                int size = (int) (Math.random() * 50) + 25; //And random sizes
                circles[i] = new Circle(coords, size); //Finally we create a new Circle with these properties
            }
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            
            for (int i = 0; i < numberOfCircles; i++) {
                g2d.draw(new Ellipse2D.Double(circles[i].getCoords().getX(), circles[i].getCoords().getY(), circles[i].getSize(), circles[i].getSize())); //We iterate over each circle in the array and paint it according to its coords and sizes
            }
        }
        
        @Override
        public Dimension getPreferredSize() { //Never call JFrame.setSize(), instead override this method and call JFrame.pack()
            return new Dimension(500, 500);
        }
    }
}
产生与此类似的输出:

我希望这能帮助您更好地了解MVC模式,因为我在回答这个问题时使用了它

注: 在这个答案中,我使用了shapesapi,这是根据@MadProgrammer在本文中的建议。我在g2d里用过。画。。。线路


要更深入地了解Swing中自定义绘制的工作原理,请查看Oracle和教程。

JFrame的默认布局是BorderLayout,因此使用JFrame.addComponent将仅替换现有的圆JPanel。对于您的问题,我建议使用一个JPanel来指定要绘制的圆的数量。哎呀,很抱歉回答晚了。非常感谢你!我想知道tho:如果我想添加一个圆而不是一个主圆中的一串,该怎么办?我必须写两个圆圈=新圆圈1@GianfrancoStercoforti如果这个答案解决了你的问题,那么请确保让其他人知道这个问题已经解决了:还有另一件事:当你叫CirclePane circle=new CirclePane1;你在调用类还是方法?我在从CirclePane创建一个新对象,实际上我在调用构造函数:public CirclePaneint numberOfCircles{它将为我们提供对新CirclePane实例的引用