swing - Paint method not working on full screen - Java -
i trying create full screen paint method. want "this day" in pink blue background show up. problem is, when run program, shows painted on screen toolbar , applications instead of showing blue background "this day" in middle in pink. code below.
public static void main(string args[]) { displaymode dm1 = new displaymode(800, 600, 16, displaymode.refresh_rate_unknown); runscreen runprogram = new runscreen(); runprogram.run(dm1); } public void run(displaymode dm) { getcontentpane().setbackground(color.blue); setforeground(color.pink); setfont(new font("arial", font.plain, 24)); fullscreen s = new fullscreen(); try { s.setfullscreen(dm, this); try { thread.sleep(5000); } catch (exception e) { } } { s.restorescreen(); } } @override public void paint(graphics g) { g.drawstring("this day", 200, 200); }
this seems work fine...
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.fontmetrics; import java.awt.graphics; import java.awt.graphicsconfiguration; import java.awt.graphicsdevice; import java.awt.graphicsenvironment; import java.awt.event.actionevent; import java.awt.event.keyevent; import javax.swing.abstractaction; import javax.swing.actionmap; import javax.swing.inputmap; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.keystroke; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testfullscreen { public static void main(string[] args) { new testfullscreen(); } public testfullscreen() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception ex) { } catch (instantiationexception ex) { } catch (illegalaccessexception ex) { } catch (unsupportedlookandfeelexception ex) { } jframe frame = new jframe("test"); frame.setundecorated(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpane()); graphicsdevice device = graphicsenvironment.getlocalgraphicsenvironment().getdefaultscreendevice(); device.setfullscreenwindow(frame); frame.setvisible(true); } }); } public class testpane extends jpanel { @override protected void paintcomponent(graphics g) { super.paintcomponent(g); string text = "hello"; fontmetrics fm = g.getfontmetrics(); int x = (getwidth() - fm.stringwidth(text)) / 2; int y = ((getheight() - fm.getheight()) / 2) + fm.getascent(); g.drawstring(text, x, y); } } }
beware, "window" request made full screen window may not window used system.
you should avoid overriding paint
, use paintcomponent
instead. should avoid overriding paint
of top level containers (like jframe
).
check out performing custom painting , painting in awt , swing more details...
Comments
Post a Comment