看不到drawRect()

问题描述 投票:0回答:1

我一直在开发一个小游戏,我试图在后台制作星星,但是drawRect()函数不起作用

@Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        Random ran = new Random();

        g2.setColor(Color.WHITE);
        g2.drawRect(0, 0, 10, 10);



        FadingText ft = new FadingText("copernicus");

        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,ft.op));

        g.setColor(Color.black);
        g.fillRect(0, 0, 1000, 1000);
       
        g2.dispose();
    }

当我运行代码时,矩形不会绘制。如果有人知道如何解决这个问题那就太好了,谢谢! (同时忽略褪色文本参考)

java swing
1个回答
0
投票

这是一个可运行的演示应用程序,用于演示如何完成此操作。您要做的第一件事是将要在其上绘制星星的面板的背景设置为黑色,例如:

JPanel gamePanel = new JPanel();
gamePanel.setBackground(Color.BLACK);
frame.getContentPane().add(gamePanel);

我不明白你为什么试图把矩形画成星星。加上圆圈不是更好吗?您可以使用 java.awt.geom.Ellipse2D.Double 类来执行此操作。

演示中的恒星和行星的数量和大小是随机的。不同的尺寸暗示了距离。行星也是随机颜色的。查看类成员变量以按照您想要的方式配置事物。阅读代码中的注释:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintComponentDemo {

    private final int numberOfStars = 700;
    private final int minStarSize = 1;        // In Pixels
    private final int maxStarSize = 5;        // In Pixels

    private final int maxNumberOfPlanets = 3; // If 0 then: Paint No Planets at all.
    private final int minNumberOfPlanets = 0;
    private final int minPlanetSize = 3;       // In Pixels
    private final int maxPlanetSize = 100;     // In Pixels
    private final Color[] planetColors = {Color.RED, Color.BLUE, Color.ORANGE,
        Color.GRAY, Color.WHITE, Color.LIGHT_GRAY, Color.CYAN, Color.DARK_GRAY,
        Color.GREEN, Color.MAGENTA};
    /* You `could` use the same concept to make stars random colors 
       as well (like: yellow, light gray, white...):     */

    private JFrame frame;
    private MyGamePanel gamePanel;

    public PaintComponentDemo() {
        initForm();
    }

    private void initForm() {
        frame = new JFrame("PaintComponent Demo - Stars");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gamePanel = new MyGamePanel();
        gamePanel.setBackground(Color.black);
        frame.getContentPane().add(gamePanel);
        frame.pack();
    }

    public static void main(String[] args) {
        new PaintComponentDemo().startDemo(args);
    }

    private void startDemo(String[] args) {
        java.awt.EventQueue.invokeLater(() -> {
            frame.setSize(600, 500);
            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
        });
    }

    class MyGamePanel extends JPanel {

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setColor(Color.WHITE);
            Random rand = new Random();

            // STARS:
            /* Paint the desired number of Stars of random sizes (minStarSize to 
               maxStarSize) in random locations:   */
            for (int i = 0; i < numberOfStars; i++) {
                int x = rand.nextInt(gamePanel.getWidth());
                int y = rand.nextInt(gamePanel.getHeight());
                double diameter = rand.nextInt((maxStarSize - minStarSize) + 1) + minStarSize;
                Ellipse2D.Double circle = new Ellipse2D.Double(x, y, diameter, diameter);
                g2.fill(circle);
            }

            // PLANETS
            /* Paint a random number planets (minNumberOfPlanets to maxNumberOfPlanets) 
               in random sizes (minPlanetSize to maxPlanetSize), in random locations of
               random Colors. As an additional optional task, paint a random size image 
               of a planet using the Graphics2D#drawImage() method (place your images 
               into a resource package:       */
            if (maxNumberOfPlanets > 0) {
                int randNumOfplanets = rand.nextInt(
                        (maxNumberOfPlanets - minNumberOfPlanets) + 1) + minNumberOfPlanets;
                for (int i = 0; i < randNumOfplanets; i++) {
                    int randPlantSize = rand.nextInt((maxPlanetSize - minPlanetSize) + 1) + minPlanetSize;
                    int color = rand.nextInt((planetColors.length));
                    g2.setColor(planetColors[color]);
                    int x = rand.nextInt(gamePanel.getWidth());
                    int y = rand.nextInt(gamePanel.getHeight());
                    Ellipse2D.Double planetCircle = new Ellipse2D.Double(x, y, randPlantSize, randPlantSize);
                    g2.fill(planetCircle);
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.