0

我想不通这个:

> 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 一书中,也没有关于我现在遇到的问题的信息。

4

2 回答 2

1

You have to put your code inside a paintComponent(Graphics g) method, overridden from a JComponent class (like JPanel).

---- Edit ----

Here is some code to give you some clues:

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

Then just add this panel to a Frame to show it.

于 2012-09-13T16:37:26.937 回答
-1
g.setColor(Color.red);

g.setColor(Color.RED);

两者都有效。

于 2012-09-13T16:59:25.753 回答