为JTextPane边框创建自定义形状

时间:2015-07-22 13:19:33

标签: java swing

Perfect Desired appearance

Undesirable Appearance

我正在设计一个聊天应用程序。

对于JTextPane的聊天框,我使用了默认矩形以外的自定义形状,如第一张图片所示。但是当此JInternalFrame窗口悬停在另一个窗口(JInternalFrame)上时,自定义JTextPane的边框线会显示拖动的线条,如第二张图片所示。

请问为什么会这样?

我所做的是将JTextpane的边框设置为AbstractBorder的子类。附加的是AbstractBorder的子类。但是我注意到,当我删除graphic.setclip(area) graphic.setclip(null)if(parent != null)块下的paintBorder()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 the BG color of the parent, everywhere outside the clip // of the 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); } } 时,问题就消失了。

如何解决此问题,仍然有我喜欢的文本框?

hid = mat1[,1]
mat1 = mat1[,-1]
mat2 = mat2[,-1]

mat3 = matrix(0, nrow(mat1), ncol(mat1))  

1 个答案:

答案 0 :(得分:1)

您可以存储原始剪辑并将其设置回来

E.g。

Shape oldCLip=g2.getClip();
...your code here...
g2.setClip(oldClip);

剪辑不是矩形,但可以是自定义形状。当您将其设置为null时,您也会将其销毁为父级,因为Graphics实例从父级转到子级。所以你应该正确地恢复它。