2013-10-06 51 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了解更多详情...