Color类中定义的常量 - Myprogramminglab

时间:2012-09-13 16:33:15

标签: java colors constants

我无法想出这个:

> Given g, a reference to a Graphics object, use a constant defined in
> the Color class to write a statement that arranges for the next drawn
> rectangle to be red:

有人有想法吗?它适用于Myprogramminglab

我想到了:

g.setColor(Color.red); 
g.drawRect (10, 10, 200, 200);

或类似的东西,但不起作用。尝试了我能想象到的一切。

由于

修改

关于Myprogramminglab的最后一个问题是这样的:

> Write the invocation (method name with arguments) needed to display
> the outline of a square whose sides are 60 pixels and whose top right
> corner is located at (100,200).

答案是:

drawRect(40,200,60,60)

因此,我不会获得有关我需要做什么的更多或更少的信息,而答案与构建整个脚本不同。所以我真的没有线索。在Java Solutions一书中,也没有关于我现在遇到的问题的信息。

2 个答案:

答案 0 :(得分:1)

您必须将代码放在paintComponent(Graphics g)方法中,并从JComponent类(如JPanel)中重写。

----编辑----

以下是一些代码可以为您提供一些线索:

public class MyHomeworkClass extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.drawRect(40,200,60,60);
    }
}

然后只需将此面板添加到框架即可显示它。

答案 1 :(得分:-1)

g.setColor(Color.red);

g.setColor(Color.RED);

都工作。

相关问题