java - Drop Shadow/Duplicate Text on JButton -
as title suggest, trying duplicate text underneath main text on jbutton, , move down 1 pixel. i'm trying drop-shadow effect on it, without blur, , 1 pixel directly under text.
how this, , easier set jpanel actionlistener rather button?
code -
import java.awt.color; import java.awt.component; import java.awt.gradientpaint; import java.awt.graphics; import java.awt.graphics2d; import java.awt.insets; import java.awt.renderinghints; import java.awt.shape; import java.awt.geom.roundrectangle2d; import javax.swing.buttonmodel; import javax.swing.jbutton; import javax.swing.jframe; class buttonui extends jbutton { private color startcolor = new color(162, 110, 235); private color endcolor = new color(110, 49, 237); private color rollovertopcolor = new color(171, 137, 219); private color rolloverbottomcolor = new color(129, 89, 216); private color pressedstartcolor = new color(73, 30, 113); private color pressedmidcolor = new color(113, 57, 221); private color pressedendcolor = new color(93, 43, 198); private color outerbordercolor = new color(117, 14, 182); private color innerbordertopcolor = new color(178, 133, 237, 255); private color innerborderbottomcolor = new color(178, 133, 237, 150); /** * @param top * , left, bottom, right */ private insets insetsforbuttons = new insets(0, 13, 4, 13); private int outerroundrectsize = 4; private int innerroundrectsize = 2; private gradientpaint gp; /** * * @param text */ public buttonui(string text) { super(); settext(text); setalignmentx(component.center_alignment); setcontentareafilled(false); setborderpainted(false); setforeground(color.white); setfocusable(false); setmargin(insetsforbuttons); } /** * * @param startcolor * @param endcolor * @param rollovercolor * @param rollovertopcolor * @param rolloverbottomcolor * @param pressedcolor * @param pressedtopcolor * @param pressedbottomcolor */ public buttonui(color startcolor, color endcolor, color rollovertopcolor, color rolloverbottomcolor, color pressedstartcolor, color pressedmidcolor, color pressedendcolor) { super(); this.startcolor = startcolor; this.endcolor = endcolor; this.pressedstartcolor = pressedstartcolor; this.pressedmidcolor = pressedmidcolor; this.pressedendcolor = pressedendcolor; setforeground(color.white); setfocusable(false); setcontentareafilled(false); setborderpainted(false); } public void paintcomponent(graphics g) { graphics2d g2d = (graphics2d) g.create(); g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2d.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); int h = getheight(); int w = getwidth(); buttonmodel model = getmodel(); if (!model.isenabled()) { setforeground(color.black); gp = new gradientpaint(0, 0, new color(73, 30, 113), 0, h, new color(73, 30, 113), true); } else { setforeground(color.white); if (model.isrollover()) { gp = new gradientpaint(0, 0, rollovertopcolor, 0, h, rolloverbottomcolor, true); } else { gp = new gradientpaint(0, 0, startcolor, 0, h, endcolor, true); } } g2d.setpaint(gp); gradientpaint p1; gradientpaint p2; if (model.ispressed()) { gp = new gradientpaint(0, 0, pressedstartcolor, 0, h, pressedmidcolor, true); g2d.setpaint(gp); p1 = new gradientpaint(0, 0, outerbordercolor, 0, h - 1, outerbordercolor); // sure edit make less dramatic p2 = new gradientpaint(0, 1, innerbordertopcolor, 0, h - 3, innerbordertopcolor); } else { p1 = new gradientpaint(0, 0, outerbordercolor, 0, h - 1, outerbordercolor); p2 = new gradientpaint(0, 1, innerbordertopcolor, 0, h - 3, innerborderbottomcolor); gp = new gradientpaint(0, 0, startcolor, 0, h, endcolor, true); } roundrectangle2d.float r2d = new roundrectangle2d.float(0, 0, w - 1, h - 1, outerroundrectsize, outerroundrectsize); shape clip = g2d.getclip(); g2d.clip(r2d); g2d.fillrect(0, 0, w, h); g2d.setclip(clip); g2d.setpaint(p1); g2d.drawroundrect(0, 0, w - 1, h - 1, outerroundrectsize, outerroundrectsize); g2d.setpaint(p2); g2d.drawroundrect(1, 1, w - 3, h - 3, innerroundrectsize, innerroundrectsize); g2d.dispose(); super.paintcomponent(g); } } public class buttonclass { public static void creategui() { jframe programframe = new jframe("custom button ui"); buttonui testbutton = new buttonui("custom jbutton ui!"); programframe.setdefaultcloseoperation(jframe.exit_on_close); programframe.add(testbutton); programframe.pack(); programframe.setvisible(true); programframe.setresizable(true); programframe.setsize(250, 75); programframe.setlocationrelativeto(null); } public static void main(string[] args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { creategui(); } }); } }
and usual, if downvote this, explain can better, won't happen again.
edit: posted overrided jbutton class, use of buttons, rather making each button.
you'll need address 2 issues:
a button's text rendered ui delegate; custom
buttonui
shown here.a drop shadow can rendered in of variety of ways.
addendum: absent sscce in update, i'm not sure what's not working way want. instead of custom ui delegate, might experiment implementation of icon
renders text either plain or shadowed. use setrollovericon()
method, shown here, change icons desired. layouttest
may geometry.
Comments
Post a Comment