java - JFrame with custom border not showing controls -
i'm trying make own custom border, , have done through overriding paint function in jframe. problem have run into, fact paint being called after constructor, causing paint window on controls. because of this, table appears when happen click on in jframe. wondering if there way make paint function happen before constructor, or if there better way create custom border. here code:
public class guimain extends jframe { int posx=0, posy=0; jtable serverlist; public guimain() { this.setundecorated(true); this.setlayout(new gridbaglayout()); serverlist = new jtable(variables.servers, variables.serversheader); add(serverlist); this.addmouselistener(new mouseadapter(){ public void mousepressed(mouseevent e) { posx = e.getx(); posy = e.gety(); } }); this.addmousemotionlistener(new mouseadapter(){ public void mousedragged(mouseevent evt) { if(posy <= 20) { setlocation(evt.getxonscreen()-posx, evt.getyonscreen()-posy); } } }); } public void paint(graphics g) { g.setcolor(new color(100, 100, 100)); g.fillrect(0, 0, main.width, main.height); g.setcolor(new color(70, 70, 70)); g.fillrect(0, 0, main.width, 20); } }
any appreciated! thanks!
you know not safest thing do, overriding paint(...)
of top-level window. type of "border" trying create? call super.paint(g);
? myself, i'd create own class extended abstractborder class, , use border on jpanel jframe's contentpane.
for example,
import java.awt.*; import java.awt.geom.point2d; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.malformedurlexception; import java.net.url; import javax.imageio.imageio; import javax.swing.*; import javax.swing.border.abstractborder; @suppresswarnings("serial") public class frameeg extends jpanel { public static final string frame_url_path = "http://th02.deviantart.net/" + "fs70/pre/i/2010/199/1/0/just_frames_5_by_scrapbee.png"; public static final int inset_gap = 120; private bufferedimage frameimg; private bufferedimage smlframeimg; public frameeg() { try { url frameurl = new url(frame_url_path); frameimg = imageio.read(frameurl); final int smlframewidth = frameimg.getwidth() / 2; final int smlframeheight = frameimg.getheight() / 2; smlframeimg = new bufferedimage(smlframewidth, smlframeheight, bufferedimage.type_int_argb); graphics g = smlframeimg.getgraphics(); g.drawimage(frameimg, 0, 0, smlframewidth, smlframeheight, null); g.dispose(); int top = inset_gap; int left = top; int bottom = top; int right = left; insets insets = new insets(top, left, bottom, right); myborder myborder = new myborder(frameimg, insets); jtextarea textarea = new jtextarea(50, 60); textarea.setwrapstyleword(true); textarea.setlinewrap(true); (int = 0; < 300; i++) { textarea.append("hello world! how going? "); } setlayout(new borderlayout(1, 1)); setbackground(color.black); dimension prefsize = new dimension(frameimg.getwidth(), frameimg.getheight()); jpanel centerpanel = new mypanel(prefsize); centerpanel.setborder(myborder); centerpanel.setlayout(new borderlayout(1, 1)); centerpanel.add(new jscrollpane(textarea), borderlayout.center); mypanel rightupperpanel = new mypanel(new dimension(smlframewidth, smlframeheight)); mypanel rightlowerpanel = new mypanel(new dimension(smlframewidth, smlframeheight)); top = top / 2; left = left / 2; bottom = bottom / 2; right = right / 2; insets smlinsets = new insets(top, left, bottom, right); rightupperpanel.setborder(new myborder(smlframeimg, smlinsets)); rightupperpanel.setlayout(new borderlayout()); rightlowerpanel.setborder(new myborder(smlframeimg, smlinsets)); rightlowerpanel.setbackgroundimg(createbackgroundimg(rightlowerpanel .getpreferredsize())); jtextarea rutextarea1 = new jtextarea(textarea.getdocument()); rutextarea1.setwrapstyleword(true); rutextarea1.setlinewrap(true); rightupperpanel.add(new jscrollpane(rutextarea1), borderlayout.center); jpanel rightpanel = new jpanel(new gridlayout(0, 1, 1, 1)); rightpanel.add(rightupperpanel); rightpanel.add(rightlowerpanel); rightpanel.setopaque(false); add(centerpanel, borderlayout.center); add(rightpanel, borderlayout.east); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } private bufferedimage createbackgroundimg(dimension preferredsize) { bufferedimage img = new bufferedimage(preferredsize.width, preferredsize.height, bufferedimage.type_int_argb); point2d center = new point2d.float(img.getwidth()/2, img.getheight()/2); float radius = img.getwidth() / 2; float[] dist = {0.0f, 1.0f}; color centercolor = new color(100, 100, 50); color outercolor = new color(25, 25, 0); color[] colors = {centercolor , outercolor }; radialgradientpaint paint = new radialgradientpaint(center, radius, dist, colors); graphics2d g2 = img.creategraphics(); g2.setpaint(paint); g2.fillrect(0, 0, img.getwidth(), img.getheight()); g2.dispose(); return img; } private static void createandshowgui() { frameeg mainpanel = new frameeg(); jframe frame = new jframe("frameeg"); frame.setundecorated(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(mainpanel); frame.setresizable(false); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } } @suppresswarnings("serial") class mypanel extends jpanel { private dimension prefsize; private bufferedimage backgroundimg; public mypanel(dimension prefsize) { this.prefsize = prefsize; } public void setbackgroundimg(bufferedimage background) { this.backgroundimg = background; } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (backgroundimg != null) { g.drawimage(backgroundimg, 0, 0, this); } } @override public dimension getpreferredsize() { return prefsize; } } @suppresswarnings("serial") class myborder extends abstractborder { private bufferedimage borderimg; private insets insets; public myborder(bufferedimage borderimg, insets insets) { this.borderimg = borderimg; this.insets = insets; } @override public void paintborder(component c, graphics g, int x, int y, int width, int height) { g.drawimage(borderimg, 0, 0, c); } @override public insets getborderinsets(component c) { return insets; } }
which so:
Comments
Post a Comment