2013-05-17 56 views
-3

使用getPixel(2,3) 使setColor(java.awt.Color.Black)变为黑色,然后用show()显示图片 提示:您必须声明并使用图片参考 变量,代码如图片p。你能纠正我在这段代码中犯的错误吗?

public static void main(String[] a) 
{ 
    new Picture(FileChooser.pickAFile(); 
} 

这是我的答案,任何人都可以告诉我,如果这是正确的,如果我需要做出任何改变?任何帮助,将不胜感激。此外,这不是一个问题,即时审查考试。所以如果你打算告诉我去做自己的事,不要麻烦回答。 :)

public static void main(String[] a) 
{ 
    Picture p = new Picture; 
    new Picture(FileChooser.pickAFile(); 
    Pixel pixRef; 
    pixRef.getPixel(2,3); 
    pixRef.setColor(java.awt.Color.Black); 
    p.show(); 
} 
+0

第一个代码..加上“)” – matzone

+0

你使用的是编辑器吗?如果没有,你应该考虑使用一个。你在''新图片(FileChooser.pickAFile()' – Bill

+0

')末尾丢失了'''什么是图片和像素类? – Craig

回答

3

,你就会失败考试这样。

public static void main(String[] a) 
{ 
    Picture p; // constructor is a method - but seems you instantiate it in next line 
    p =new Picture(FileChooser.pickAFile()); // assign it to p 
    Pixel pixRef = new Pixel(); //avoid nullpointerexception! but logically you should get the pixel from the picture, which displayed in next line, can remove the "new Pixel()"; 
    pixRef = p.getPixel(2,3); // Shouldn't you get pixelref from picture? 
    pixRef.setColor(java.awt.Color.Black); 
    p.show(); // I don't understand this. Where do you show this? shouldn't you put it inside a Frame or something? 
} 
+0

如果您将其引用到p,则无需创建新的像素.getPixel后面的行。 – Lynch

+0

你是对的@Lynch,但是我也想在原代码中显示错误。 – Rudy

+0

是的,我明白了,这样保持。 – Lynch

0
public static void main(String[] a) 
{ 

Picture p = new Picture; //what are you trying to call here? you must try checking out how to create an object it must be "Picture p = new Picture();" , and if you have just defined a parameterised constructor in Picture class and need an object of that directly do it this was 
"Picture p = new Picture(FileChooser.pickAFile()); 

new Picture(FileChooser.pickAFile()); // must be enclosed properly 
Pixel pixRef; 
pixRef.getPixel(2,3); // must be initialized before using the instance methods "Pixel pixRef = new Pixel();" 
pixRef.setColor(java.awt.Color.Black); 
p.show(); 
} 
相关问题