java - java中如何停止背景图形?

标签 java swing graphics

我尝试制作一个简单的java游戏。它工作正常,但我画了一个背景,但我没有停止方 block 的颜色,它总是在变化 enter image description here

我为此编写了另一个类,并在 main 中调用它,但我没有使用它

@SuppressWarnings(“串行”)

class BackPan extends JFrame{


    private JLayeredPane layers;
    private JPanel down;
    static int width = Board.boardWidth;
    static int height = Board.boardHeight;
    Random rnd = new Random();

    public BackPan(){

           layers = new JLayeredPane();
           rnd =new Random(); 
           down = new JPanel(){
           public void paintComponent(Graphics g){
            Graphics2D g2d = (Graphics2D)g; 
            //***************************************************************
            int low = 50; 
            int high = 255;
            for(int i = 0; i<= width; i+=50){
                g2d.setColor(new Color(rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low));             
                g2d.fillRect(i, 50, 50, 50);
                for(int j = 0; j<= height; j += 50 ){
                    g2d.setColor(new Color(rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low));
                    g2d.fillRect(i, j, 50, 50);                 
                }
            }
           }
           };
           //****************************************************************
           down.setBounds(0, 0, width, height);
           layers.add(down, new Integer(1));
           getContentPane().add(layers, BorderLayout.CENTER);


    }

}

最佳答案

比使用设定种子进行随机化更好,只需通过以下两种方法之一修复随机图像即可:

  1. 创建一个颜色数组,在需要时随机化,然后在绘制背景时使用该数组
  2. 更好的是,只需将随机颜色绘制到 BufferedImage 并在 PaintComponent 方法中绘制即可。如果需要重新随机化,则重新创建图像。

对于后者的示例,请注意,仅当按下按钮时图像才会重新随机化(通过调用 createBackground() 方法):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;

@SuppressWarnings("serial")
public class ColorSquares extends JPanel {
    public static final int SQR_SIDE = 50;
    public static final int COLUMNS = 20;
    public static final int ROWS = 16;
    private int columns;
    private int rows;
    private int sqrSide;
    private Image backgroundImg;    

    public ColorSquares(int columns, int rows, int sqrSide) {
        this.columns = columns;
        this.rows = rows;
        this.sqrSide = sqrSide;
        backgroundImg = createBackground();
        add(new JButton(new AbstractAction("New Background") {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                backgroundImg = createBackground();
                repaint();
            }
        }));
    }

    public Image createBackground() {
        int w = columns * sqrSide;
        int h = rows * sqrSide;
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                float hue = (float) Math.random();
                float saturation = (float) (Math.random() * 0.5 + 0.5);
                float brightness = (float) (Math.random() * 0.5 + 0.5);
                Color randColor = Color.getHSBColor(hue, saturation, brightness);
                g.setColor(randColor);
                int x = c * sqrSide;
                int y = r * sqrSide;
                g.fillRect(x, y, sqrSide, sqrSide);
            }
        }
        g.dispose();
        return img;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (backgroundImg != null) {
            g.drawImage(backgroundImg, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(columns * sqrSide, rows * sqrSide);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Colors");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ColorSquares(COLUMNS, ROWS, SQR_SIDE));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

使用 BufferedImage 的另一个好处是绘制它比实际绘制要快。

关于java - java中如何停止背景图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41301414/

相关文章:

Java - 带有嵌套循环的paintComponent

Java-单元格更改时如何保存JTable?

java - 如何使 java JPanel 和 graphics2d 透明?

c++ - SDL 分辨率问题

java - 通过 SNMP 监控 JRockit JVM

java - 设置带 2 位小数的 double 格式

java - 在JTextArea中使用Timer实现打字机效果?

java - Java中如何比较和统计多个Streams的元素?

java - Hibernate:一对多/多对一删除失败

java - Google 地理编码 API 返回非法访问错误