这些意义(java)

时间:2013-09-17 13:16:29

标签: java

public void paint(Graphics g)
{   
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 800, 480);
    g.setColor(Color.BLUE);
    g.drawRect(60, 200, 100, 250);
    g.setColor(Color.BLACK);
    g.drawString("My name is James", 200, 400);    
} 

我想知道这些java属性的确切含义。请告诉我这个

我可以更改“g”(我可以将其命名为任何名称)

此setColor的工作原理如何?还有更多关于你所知道的事情?

1 个答案:

答案 0 :(得分:0)

public void paint(Graphics g)  
/*
Above The name of the method, methods are ways (re-usable blocks of code) of operating on objects
public means the method can be called from outside or inside it's package
void means that the method does not return a value, 
for example if void was changed to int, then this method would give out an int value when run
the 'g' is a variable of type Graphics which is local to this method. The g can be changed to    
any legal variable name and it will only affect the code within the method as that is where it is used.  
So if you changed g to helloMyNameIsEarl for example, all the places where g is referenced
in the code below would error, until you swapped wherever g is referenced to helloMyNameIsEarl
eg helloMyNameIsEarl.setColor(Color.WHITE);

*/
{   
g.setColor(Color.WHITE);
g.fillRect(0, 0, 800, 480);
g.setColor(Color.BLUE);
g.drawRect(60, 200, 100, 250);
g.setColor(Color.BLACK);
g.drawString("My name is James", 200, 400);    

/*
All of the above g.something(something, something) are methods being called on g, which is     referencing the 
Graphics object passed to it (Look up pass by value in java methods) so your method you have is     receiving a 
Graphics object and performing the operations between the { } on it.
*/
} 

以下链接将为您提供更多信息。这是Java标准版API并提供了详细信息 关于上述方法。 Java Se 7 api Graphics这将详细介绍该方法及其“属性”。

祝你好运