java - Screen not created for drawstring text to display -
java - Screen not created for drawstring text to display -
i working on java game development tutorials given on buckysroom.org. creating screen class, allows me go total screen. then, when seek paint text, screen not appear, , text shown on working window (eclipse) if comment out paint method or create paintcomponent, reddish screen appears, without text. can help me on how deal this?
the screen class:
import java.awt.*; import javax.swing.jframe; public class screen { private graphicsdevice vc; public screen() { graphicsenvironment env = graphicsenvironment.getlocalgraphicsenvironment(); vc = env.getdefaultscreendevice(); } public void setfullscreen(displaymode dm, jframe window) { window.setundecorated(true); window.setresizable(false); vc.setfullscreenwindow(window); if (dm != null && vc.isdisplaychangesupported()) { seek { vc.setdisplaymode(dm); } grab (exception ex) { } } } public window getfullscreenwindow() { homecoming vc.getfullscreenwindow(); } public void restorescreen() { window w = vc.getfullscreenwindow(); if (w != null) { w.dispose(); } vc.setfullscreenwindow(null); } }
this class phone call it:
import java.awt.*; import javax.swing.jframe; public class bucky extends jframe { public static void main(string[] args) { displaymode dm = new displaymode(800, 600, 16, displaymode.refresh_rate_unknown); bucky m = new bucky(); m.run(dm); } public void run(displaymode dm) { this.getcontentpane().setbackground(color.red); setforeground(color.black); setfont(new font("arial", font.plain, 24)); screen s = new screen(); seek { s.setfullscreen(dm, this); seek { thread.sleep(5000); } grab (exception ex) { } } { s.restorescreen(); } } public void paint(graphics g) { g.drawstring("this awesome", 200, 200); } }
my suspicion thread.sleep
cause event dispatching thread set sleep, preventing processing paint events (or other kind of events matter) and/or paint
method of jframe
not appropriate method performing paint in (generally it's not number of reasons)...
instead, should consider using javax.swing.timer
can utilize set alarm time in future , phone call back.
you should consider using jpanel
perform custom painting on, example...
import java.awt.color; import java.awt.dimension; import java.awt.displaymode; import java.awt.eventqueue; import java.awt.font; import java.awt.graphics; import java.awt.graphics2d; import java.awt.graphicsdevice; import java.awt.graphicsenvironment; import java.awt.window; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class fullscreentest { public static void main(string[] args) { new fullscreentest(); } public fullscreentest() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setcontentpane(new testpane()); displaymode dm = new displaymode(800, 600, 16, displaymode.refresh_rate_unknown); screen.setfullscreen(null, frame); timer timer = new timer(5000, new actionlistener() { @override public void actionperformed(actionevent e) { screen.restorescreen(); } }); timer.start(); } }); } public class testpane extends jpanel { public testpane() { setfont(new font("arial", font.plain, 24)); setbackground(color.red); } @override public dimension getpreferredsize() { homecoming new dimension(200, 200); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); g.drawstring("this awesome", 200, 200); g2d.dispose(); } } public static class screen { public static graphicsdevice getgraphicsdevice() { graphicsenvironment env = graphicsenvironment.getlocalgraphicsenvironment(); homecoming env.getdefaultscreendevice(); } public static void setfullscreen(displaymode dm, jframe window) { window.setundecorated(true); window.setresizable(false); graphicsdevice gd = getgraphicsdevice(); gd.setfullscreenwindow(window); if (dm != null && gd.isdisplaychangesupported()) { seek { gd.setdisplaymode(dm); } grab (exception ex) { } } } public static window getfullscreenwindow() { graphicsdevice gd = getgraphicsdevice(); homecoming gd.getfullscreenwindow(); } public static void restorescreen() { graphicsdevice gd = getgraphicsdevice(); window w = gd.getfullscreenwindow(); if (w != null) { w.dispose(); } gd.setfullscreenwindow(null); } } }
take at:
concurrency in swing how utilize swing timers performing custom paintingfor more details...
also, because of type of work it's doing, screen
static
class. maintains no contextual info , provides utility functionality - imho.
if needed 1 each graphicsdevice
, story ;)
java
Comments
Post a Comment