swing - drawImage in Java applet -
i can't display image in applet. using drawimage()
in paint()
method. (graphics2d) cast part of tutorial program. image supposed change every few seconds , correspond title , http link. works images. tried oracle's tutorials , looked through other questions on stackoverflow. tried passing different arguments drawimage()
method. think may have unnecessary 'import's.
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.*; // 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 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"); (int = 0; < 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]); } }
}
from can tell, paint code should work fine
the problem is, likely, in fact images not loading, since you've chosen ignore errors raised process, won't have idea why...
so, instead of // dont know
, use e.printstacktrace()
when loading images
for (int = 0; < 5; i++) { try { url url = new url(getcodebase(), imagestring[i]); images[i] = imageio.read(url); } catch (ioexception e) { e.printstacktrace(); } }
this @ least provide more clues problems facing.
you should avoid using awt (button
) components on swing (jappelt
) containers. tend not play nice together.
having said that, encourage not use jappelt
learning tool. applets come swag of own issues difficult diagnose @ best of times, more when you're trying learn java , swing api. swing api complex enough adding unnecessary challenges.
you should avoid extending top level containers (in case, have no choice), should avoid painting directly top level containers. apart complexities of paint process, not double buffered, introduces flickering when ui updated.
instead, start jpanel
, override it's paintcomponent
method. jcomponent
s double buffered default, won't flicker when repainted. must call super.paintxxx
. said, paint process complex process, each paintxxx
method link in chain, if break chain, should prepared strange , unexpected behaviour down track.
once have component setup, free choose how deploy it, adding jframe
or japplet
, making component more flexible , reusable.
take @ performing custom painting more details
the next question comes mind why? why custom painting @ all, when jlabel
s not job, better.
take @ creating gui swing more details...
Comments
Post a Comment