java - Creating A Custom Shape For a JTextPane Border -
i designing chat application.
for chat box jtextpane
, used custom shape other default rectangle shape shown in first image. when window jinternalframe
hovered on window(jinternalframe
), border line of custom jtextpane
show dragged lines shown in second image.
please why happening?
what did write set jtextpane
's border subclass of abstractborder
. attached subclass of abstractborder
used. i've noticed when remove graphic.setclip(area)
, graphic.setclip(null)
under if(parent != null)
block of paintborder()
method, problem goes away.
how solve line problem , still have fancy text box?
import java.awt.basicstroke; import java.awt.color; import java.awt.component; import java.awt.graphics; import java.awt.graphics2d; import java.awt.insets; import java.awt.polygon; import java.awt.rectangle; import java.awt.renderinghints; import java.awt.geom.area; import java.awt.geom.roundrectangle2d; import javax.swing.jpanel; import javax.swing.border.abstractborder; public class textbubbleborder1 extends abstractborder { private color color; private int thickness = 4; private int radii = 8; private int pointersize = 7; private insets insets = null; private basicstroke stroke = null; private int strokepad; private int pointerpad = 4; private boolean left = true; int xi, yi; renderinghints hints; jpanel pane; boolean right; textbubbleborder1( color color, int thickness, int radii, int pointersize) { this.thickness = thickness; this.radii = radii; this.pointersize = pointersize; this.color = color; stroke = new basicstroke(thickness); strokepad = thickness / 2; hints = new renderinghints( renderinghints.key_antialiasing, renderinghints.value_antialias_on); int pad = radii + strokepad; int bottompad = pad + pointersize + strokepad; insets = new insets(-2,16,0,0); } textbubbleborder1( color color,int thickness,int radii,int pointersize,bolean left, boolean right, int x, int y) { this(color, thickness, radii, pointersize); this.left = left; this.xi = x; this.yi = y; this.right = right; } @override public insets getborderinsets(component c) { return insets; } @override public insets getborderinsets(component c, insets insets) { return getborderinsets(c); } @override public void paintborder( component c, graphics g, int x, int y, int width, int height) { graphics2d g2 = (graphics2d) g; int bottomliney = height - thickness - pointersize + 10 ; roundrectangle2d.double bubble = null; area area = null; polygon pointer = null; if(!right){ bubble = new roundrectangle2d.double( 10, 0, !left ? width-9 : width - 25, bottomliney, radii, radii); area = new area(bubble); pointer = new polygon(); if(!left){ // left point pointer.addpoint(11, 3); pointer.addpoint(11, 14); pointer.addpoint(0, 8); }else{ pointer.addpoint(width-17, 2); pointer.addpoint(width-23, 16); pointer.addpoint(width-5, 6); } area.add(new area(pointer)); }else{ bubble = new roundrectangle2d.double( 0, -2, width, bottomliney+5, radii, radii); area = new area(bubble); } g2.setrenderinghints(hints); // paint bg color of parent, everywhere outside clip // of text bubble. component parent = c.getparent(); if (parent!=null) { color bg = parent.getbackground(); rectangle rect = new rectangle(0,0,width, height); area borderregion = new area(rect); borderregion.subtract(area); g2.setclip(borderregion); g2.setcolor(bg); g2.fillrect(0, 0, width, height); g2.setclip(null); } g2.setcolor(color); g2.setstroke(stroke); g2.draw(area); } }
you can store original clip , set back
e.g.
shape oldclip=g2.getclip(); ...your code here... g2.setclip(oldclip);
clip not rectangle custom shape. when set null destroy parent because graphics instance goes parent children. should restore properly.
Comments
Post a Comment