アプレットで画像を表示できません。メソッドで使用drawImage()
しpaint()
ます。(Graphics2D) キャストは、チュートリアル プログラムの一部です。画像は数秒ごとに変化し、タイトルと http リンクに対応しています。私の画像以外はすべて機能します。Oracle のチュートリアルを試し、stackoverflow に関する他の質問に目を通しました。メソッドに異なる引数を渡そうとしまし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]);
}
}
}