2013-05-17 67 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(); 
} 
相關問題