java - Border with rounded corners -
here have small piece of code getting rectangular box using awt.but want border should rounded corner.can please suggest me?
int width = 150; int height = 50; bufferedimage bufferedimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics2d g2d = bufferedimage.creategraphics(); font font = new font("georgia", font.bold, 18); g2d.setfont(font); renderinghints rh = new renderinghints( renderinghints.key_antialiasing, renderinghints.value_antialias_on); rh.put(renderinghints.key_rendering, renderinghints.value_render_quality); g2d.setrenderinghints(rh); gradientpaint gp = new gradientpaint(0, 0, color.decode("#24777d"), 0, height / 2, color.decode("#008080"), true); g2d.setpaint(gp); g2d.fillrect(0, 0, width, height); g2d.setcolor(new color(255, 255, 255)); g2d.dispose();
use drawroundrect() method of graphics2d class paint border in addition fillroundrect() method.follow api documentation.here sample code try.
jframe f = new jframe(); f.setlayout(null); f.setdefaultcloseoperation(3); f.setsize(500, 500); f.setlocationrelativeto(null); jpanel p = new jpanel() { protected void paintcomponent(graphics g) { super.paintcomponent(g); int width = getwidth(); int height = getheight(); graphics2d graphics = (graphics2d) g; graphics.setrenderinghint(renderinghints.key_antialiasing,renderinghints.value_antialias_on); //draws rounded opaque panel borders. graphics.setcolor(getbackground()); //paint background graphics.fillroundrect(0, 0, width, height, 17, 17); graphics.setcolor(getforeground()); //paint border graphics.drawroundrect(0, 0, width, height, 17, 17); } }; p.setbounds(20,20,150,50); p.setopaque(false); f.getcontentpane().setbackground(color.blue); f.add(p); f.setvisible(true);
Comments
Post a Comment