setColor无法按预期工作

时间:2017-11-18 12:38:31

标签: java swing

我有一个从属性中读取颜色名称的函数:

   private Color getBlockColor() {
        Color blockColor;
        try {
            Field field = Class.forName("java.awt.Color").getField(PlatformParameters.getProperties().getProperty("blockColor"));
            blockColor = (Color) field.get(null);
        } catch (Exception e) {
            blockColor = DEFAULT_BLOCK_COLOR;
        }
        return blockColor;
    }

例如,对于LIGHT_GRAY字符串,我得到以下内容:

System.out.println(blockColor);
[output]: java.awt.Color[r=192,g=192,b=192]

现在我想画一个这种颜色的矩形:

g.setColor(getBlockColor());
g.fillRect(50,50,50,50);

但它不起作用(矩形是白色的)。使用g.setColor(Color.red)工作正常。这有什么不对?

来自班级的整个代码 - 我正在使用JPanel,blocksArray[][]包含块的位置信息:

package GamePanel;

import Parameters.*;

import java.awt.*;
import java.lang.reflect.Field;
import javax.swing.*;

import static GamePanel.Constants.*;
import static java.lang.Math.toIntExact;

public class GamePanel {

    private JPanel panel;

    public GamePanel(int frameWidth, int frameHeight) {
        panel = new JPanel(true) {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                drawRectangles(g, frameHeight, frameWidth);
            }
        };
        panel.setLayout(new FlowLayout());
        panel.setPreferredSize(new Dimension(frameWidth, frameHeight));
        setBackgroundColor(panel);
    }

    public JPanel getPanel() {
        return panel;
    }

    private void setBackgroundColor(JPanel panel) {
        Color backgroundColor;
        try {
            Field field = Class.forName("java.awt.Color").getField(PlatformParameters.getProperties().getProperty("backgroundColor"));
            backgroundColor = (Color) field.get(null);
        } catch (Exception e) {
            backgroundColor = DEFAULT_BACKGROUND_COLOR;
        }
        panel.setBackground(backgroundColor);
    }

    private Color getBlockColor() {
        Color blockColor;
        try {
            Field field = Class.forName("java.awt.Color").getField(PlatformParameters.getProperties().getProperty("blockColor"));
            blockColor = (Color) field.get(null);
        } catch (Exception e) {
            blockColor = DEFAULT_BLOCK_COLOR;
        }
        return DEFAULT_BLOCK_COLOR;
    }

    private Color getStrokeColor() {
        Color strokeColor;
        try {
            Field field = Class.forName("java.awt.Color").getField(PlatformParameters.getProperties().getProperty("strokeColor"));
            strokeColor = (Color) field.get(null);
        } catch (Exception e) {
            strokeColor = DEFAULT_STROKE_COLOR;
        }
        return strokeColor;
    }

    private void drawRectangles(Graphics g, int frameHeight, int frameWidth) {
        int numberOfColumns = PlatformParameters.getNumberOfColumns();
        int numberOfRows = PlatformParameters.getNumberOfRows();
        boolean[][] blocksArray = PlatformParameters.getBlocksArray();

        final int BLOCK_HEIGHT = toIntExact(Math.round(frameHeight / (double) numberOfRows));
        final int BLOCK_WIDTH = toIntExact(Math.round(frameWidth / (double) numberOfColumns));

        Color blockColor = getBlockColor();
        Color strokeColor = getStrokeColor();
        // g.setColor(blockColor) should work... but it does not... why?


        for (int i = 0; i < numberOfRows; ++i) {
            for (int j = 0; j < numberOfColumns; ++j) {
                if (blocksArray[i][j]) {
                    g.setColor(blockColor);
                    g.fillRect(j * BLOCK_WIDTH, i * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
//                    g.setColor(strokeColor);
//                    g.drawRect(j * BLOCK_WIDTH, i * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
                }
            }
        }
    }
}

0 个答案:

没有答案