2013-10-06 53 views
0

我無法在我的小程序中顯示圖像。在paint()方法中使用drawImage()。 (Graphics2D)強制轉換是教程程序的一部分。圖像應該每隔幾秒更改一次,並對應標題和http鏈接。一切工作,但我的形象。我嘗試了Oracle的教程,並在stackoverflow上查看了其他問題。嘗試傳遞不同的參數到drawImage()方法。另外我想我可能會有一些不必要的「進口」。Java中的drawImage小程序

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.net.*; 
import java.net.URL; 
// image libraries 
import java.awt.Image.*; 
import java.io.*; 
import java.awt.image.*; // for buffered image 
import javax.imageio.*; // read buffered image 
import java.awt.image.BufferedImage.*; 

public class Ch_19_Ex_01 extends JApplet implements Runnable, ActionListener { 
     String[] pageTitle = new String[5]; 
     String[] imageString = new String[5]; 
     URL[] pageLink = new URL[5]; 
     BufferedImage[] images = new BufferedImage[5]; 
     Color butterscotch = new Color(255, 204, 158); 
     int current = 0; 
     Thread runner; 

public void init() { 
    pageTitle = new String[] { 
     "Horoscope for cancer", 
     "Brainy Quotes", 
     "NJ Daily Lottery", 
     "Daily Jokes", 
     "West Milford weather", 
    }; 
    imageString = new String[] { 
     "0.jpg", 
     "1.png", 
     "2.png", 
     "3.jpg", 
     "4.gif", 
    }; 
    pageLink[0] = getURL("http://my.horoscope.com/astrology/free-daily-horoscope-taurus.html"); 
    pageLink[1] = getURL("http://www.brainyquote.com/quotes/keywords/daily_life.html"); 
    pageLink[2] = getURL("http://www.state.nj.us/lottery/home.shtml"); 
    pageLink[3] = getURL("http://www.jokes.com/"); 
    pageLink[4] = getURL("http://www.weather.com/weather/today/90005"); 

    for (int i = 0; i < 5; i++) { 
     try { 
      URL url = new URL(getCodeBase(), imageString[i]); 
      images[i] = ImageIO.read(url); 
     } catch (IOException e) { 
      // dont know 
     } 
    } 
    Button goButton = new Button("Go"); 
    goButton.addActionListener(this); 
    FlowLayout flow = new FlowLayout(); 
    setLayout(flow); 
    add(goButton); 
    Button stopButton = new Button("Stop"); 
    add(stopButton); 
} 

URL getURL(String urlText) { 
    URL pageURL = null; 
    try { 
     pageURL = new URL(getDocumentBase(), urlText); 
    } catch (MalformedURLException m) { 
     System.out.println("Error>>>>"); 
    } 
    return pageURL; 
} 

public void paint(Graphics screen) { 
    Graphics2D screen2D = (Graphics2D) screen; 
    screen2D.setColor(butterscotch); 
    screen2D.fillRect(0, 0, getSize().width, getSize().height); 
    screen2D.setColor(Color.black); 
    screen2D.drawString(pageTitle[current], 5, 60); 
    screen2D.drawString("" + pageLink[current], 5, 80); 
    screen2D.drawImage(images[current], 0, 0, 100, 200, this); 
} 

public void start() { 
    if (runner == null) { 
     runner = new Thread(this); 
     runner.start(); 
    } 
} 

public void run() { 
    Thread thisThread = Thread.currentThread(); 
    while(runner == thisThread) { 
     current ++; 
     if (current > 4) { 
      current = 0; 
     } 
     repaint(); 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      System.out.println("Error>>>>>>>>>>>"); 
     } 
    } 
} 

public void stop() { 
    if (runner != null) { 
     runner = null; 
    } 
} 

public void actionPerformed(ActionEvent event) { 
    if (runner != null) { 
     runner = null; 
    } 
    AppletContext browser = getAppletContext(); 
    if (pageLink[current] != null) { 
     browser.showDocument(pageLink[current]); 
    } 
} 

}

+3

而不是'//不know',用'e.printStackTrace()',它至少會告訴你什麼時候出現錯誤... – MadProgrammer

+0

非常感謝你'MadProgrammer' –

回答

2

從我可以告訴你的繪製代碼應該工作正常

的問題是,最有可能的,在事實,即圖像不加載,但自從你選擇忽略由這個過程中引發的任何錯誤,你不會有任何想法,爲什麼...

所以,相反的// dont know,您加載圖像時使用e.printStackTrace()

for (int i = 0; i < 5; i++) { 
    try { 
     URL url = new URL(getCodeBase(), imageString[i]); 
     images[i] = ImageIO.read(url); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

這至少會爲您提供一些關於您面臨的問題的更多線索。

您還應該避免在Swing(JAppelt)容器上使用AWT(Button)組件。他們往往不會一起打好。

說了這麼多,我鼓勵你不要使用JAppelt作爲學習工具。小應用程序帶有一大堆他們自己的問題,這些問題很難在最佳時間進行診斷,尤其是當您嘗試學習Java和Swing API時。 Swing API足夠複雜,增加了不必要的挑戰。

你也應該避免從頂級容器中擴展(在這種情況下,你別無選擇),但是你也應該避免直接繪製頂層容器。除了繪製過程的複雜性之外,它們沒有雙重緩衝,當更新UI時引入了閃爍。

取而代之,從JPanel之類的東西開始,並覆蓋它的paintComponent方法。 JComponent s默認情況下是雙緩衝,因此它們在重新繪製時不會閃爍。您還必須致電super.paintXxx。正如我所說,繪畫過程是一個複雜的過程,每個paintXxx方法是鏈中的一個環節,如果你打破了鏈條,你應該準備好一些奇怪的和意想不到的行爲。

組件設置完成後,您可以自由選擇如何部署它,方法是將其添加到JFrameJApplet之類的組件中,使組件更加靈活和可重用。

看看Performing Custom Painting更多細節

想到的就是接下來的問題?爲什麼要做任何定製的繪畫,當JLabel s不僅會做的工作,但可能會做得更好。

看看Creating an GUI with Swing瞭解更多詳情...