小编Rof*_*Rof的帖子

graphics.setColor(color); 不工作

我试图用颜色创建一个小小的塔防游戏但是颜色不起作用,这就是我绘制矩形的方法。

public static void fill(GridLocation loc, Color c){
    Main.frame.getGraphics().setColor(c);
    Main.frame.getGraphics().fillRect(loc.toFrameLocation().x, loc.toFrameLocation().y, 20, 20);
    Main.frame.getGraphics().dispose();
}
Run Code Online (Sandbox Code Playgroud)

这就是我调用/创建JFrame的方式

public static void main(String[] args) {
    frame = new JFrame("Tower defense");
    frame.setSize(1000, 700);
    frame.setVisible(true);
    frame.setLayout(null);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    while (frame.isVisible()){
        Grid.fill(new GridLocation(1,1), new Color(1F,0F,0F));
        Grid.fill(new GridLocation(2,1), Color.gray);
        Grid.drawGrid();
        Grid.fill(new GridLocation(3,1), Color.green);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题:矩形始终是黑色的?

java graphics rgb awt jframe

2
推荐指数
1
解决办法
3046
查看次数

标签 统计

awt ×1

graphics ×1

java ×1

jframe ×1

rgb ×1