From 796c285d8269a6074f190419ebaba3225a4c72c8 Mon Sep 17 00:00:00 2001 From: Starryi Date: Sat, 24 Jul 2021 15:59:57 +0800 Subject: [PATCH] =?UTF-8?q?EPORT-55747=20=E3=80=90=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E5=88=86=E7=A6=BB=E3=80=91=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=A4=8D=E7=94=A8-=E8=AE=BE=E8=AE=A1=E5=99=A8=E7=94=BB?= =?UTF-8?q?=E5=B8=83=E6=95=88=E6=9E=9C=EF=BC=8C=E6=A0=87=E9=A2=98=E8=83=8C?= =?UTF-8?q?=E6=99=AF=E9=81=AE=E6=8C=A1=E8=BE=B9=E6=A1=86=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【问题原因】 边框图片被标题背景遮挡。 标题背景由XLabel绘制,边框图片由XWTitleLayout绘制导致的 【改动思路】 1. 标题背景/主体背景/边框图片均由XWTitleLayout绘制 2. 整理下背景/边框绘制相关的接口 --- .../creator/XBorderStyleWidgetCreator.java | 119 +++++++++--------- .../fr/design/designer/creator/XLabel.java | 8 +- .../designer/creator/XWTitleLayout.java | 54 ++++++++ .../form/FormElementCaseDesigner.java | 4 - 4 files changed, 119 insertions(+), 66 deletions(-) diff --git a/designer-form/src/main/java/com/fr/design/designer/creator/XBorderStyleWidgetCreator.java b/designer-form/src/main/java/com/fr/design/designer/creator/XBorderStyleWidgetCreator.java index 398dcc1f2..d17351f58 100644 --- a/designer-form/src/main/java/com/fr/design/designer/creator/XBorderStyleWidgetCreator.java +++ b/designer-form/src/main/java/com/fr/design/designer/creator/XBorderStyleWidgetCreator.java @@ -52,33 +52,26 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{ return this.background4Painting; } - public void setBackground4Painting(Background background4Painting) { - this.background4Painting = background4Painting; - this.repaint(); - } - public double getBackgroundOpacity4Painting() { return backgroundOpacity4Painting; } - public void setBackgroundOpacity4Painting(double backgroundOpacity4Painting) { - this.backgroundOpacity4Painting = backgroundOpacity4Painting; + public void setBackground4Painting(Background background, double opacity) { + this.background4Painting = background; + this.backgroundOpacity4Painting = opacity; } public Background getBorderImage4Painting() { return borderImage4Painting; } - public void setBorderImage4Painting(Background borderImage4Painting) { - this.borderImage4Painting = borderImage4Painting; - } - public double getBorderImageOpacity4Painting() { return borderImageOpacity4Painting; } - public void setBorderImageOpacity4Painting(double borderImageOpacity4Painting) { - this.borderImageOpacity4Painting = borderImageOpacity4Painting; + public void setBorderImage4Painting(Background borderImage, double opacity) { + this.borderImage4Painting = borderImage; + this.borderImageOpacity4Painting = opacity; } /** @@ -129,11 +122,13 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{ } this.setBorder(border); - this.setBorderImage4Painting(style != null ? style.getBorderImage() : null); - this.setBorderImageOpacity4Painting(style != null ? style.getBorderImageOpacity() : 0.0); - - this.setBackground4Painting(style != null ? style.getBackground() : null); - this.setBackgroundOpacity4Painting(style != null ? style.getAlpha() : 0.0); + if (style != null) { + this.setBorderImage4Painting(style.getBorderImage(), style.getBorderImageOpacity()); + this.setBackground4Painting(style.getBackground(), style.getAlpha()); + } else { + this.setBorderImage4Painting(null, 0.0); + this.setBackground4Painting(null, 0.0); + } } private void clearTitleWidget() { @@ -213,42 +208,56 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{ XCreator titleCreator = titleParent.getTitleCreator(); XCreator bodyXCreator = titleParent.getBodyCreator(); - Border border = bodyXCreator.getBorder(); - - titleParent.setBorder(border); // 容器绘制完整边框 - bodyXCreator.setBorder(BorderFactory.createEmptyBorder()); // body不绘制边框 - titleCreator.setBorder(BorderFactory.createEmptyBorder()); // title绘制底部边框 - if (border instanceof LineBorder) { - Color color = ((LineBorder) border).getLineColor(); - int thickness = ((LineBorder) border).getThickness(); - titleCreator.setBorder(new BottomLineBorder(color, thickness)); - } - - if (bodyXCreator instanceof XBorderStyleWidgetCreator) { - XBorderStyleWidgetCreator styledBodyXCreator = (XBorderStyleWidgetCreator) bodyXCreator; - Background borderImage4Painting = styledBodyXCreator.getBorderImage4Painting(); - double opacity = styledBodyXCreator.getBorderImageOpacity4Painting(); - - styledBodyXCreator.setBorderImage4Painting(null); // body不绘制图片边框 - styledBodyXCreator.setBorderImageOpacity4Painting(0.0); - titleParent.setBorderImage4Painting(borderImage4Painting); // 容器绘制完整图片边框 - titleParent.setBorderImageOpacity4Painting(opacity); - } - - if (bodyXCreator instanceof XBorderStyleWidgetCreator) { - XBorderStyleWidgetCreator styledBodyXCreator = (XBorderStyleWidgetCreator) bodyXCreator; - Background background4Painting = styledBodyXCreator.getBackground4Painting(); - double opacity = styledBodyXCreator.getBackgroundOpacity4Painting(); - - styledBodyXCreator.setBackground4Painting(null); // body不绘制背景 - styledBodyXCreator.setBackgroundOpacity4Painting(0.0); - titleParent.setBackground4Painting(background4Painting); // 容器绘制完整背景 - titleParent.setBackgroundOpacity4Painting(opacity); - } + reshuffleBorderPaintingEffectIfTitleExists(titleParent, titleCreator, bodyXCreator); + reshuffleBackgroundPaintingEffectIfTitleExists(titleParent, titleCreator, bodyXCreator); } } } + private void reshuffleBorderPaintingEffectIfTitleExists(XWTitleLayout parentCreator, XCreator titleCreator, XCreator bodyCreator) { + Border border = bodyCreator.getBorder(); + + parentCreator.setBorder(border); // 容器绘制完整边框 + bodyCreator.setBorder(BorderFactory.createEmptyBorder()); // body不绘制边框 + titleCreator.setBorder(BorderFactory.createEmptyBorder()); // title绘制底部边框 + if (border instanceof LineBorder) { + Color color = ((LineBorder) border).getLineColor(); + int thickness = ((LineBorder) border).getThickness(); + titleCreator.setBorder(new BottomLineBorder(color, thickness)); + } + + if (bodyCreator instanceof XBorderStyleWidgetCreator) { + XBorderStyleWidgetCreator styledBodyXCreator = (XBorderStyleWidgetCreator) bodyCreator; + Background borderImage= styledBodyXCreator.getBorderImage4Painting(); + double opacity = styledBodyXCreator.getBorderImageOpacity4Painting(); + + styledBodyXCreator.setBorderImage4Painting(null, 0.0); // body不绘制图片边框 + parentCreator.setBorderImage4Painting(borderImage, opacity); // 容器绘制完整图片边框 + } + } + + private void reshuffleBackgroundPaintingEffectIfTitleExists(XWTitleLayout parentCreator, XCreator titleCreator, XCreator bodyCreator) { + if (titleCreator instanceof XLabel) { + XLabel labelCreator = (XLabel) titleCreator; + Label titleLabel = labelCreator.toData(); + + Background background = titleLabel.getBackground(); + double opacity = titleLabel.getBackgroundOpacity(); + + labelCreator.setEnabledBackgroundPainting(false); // 标题不绘制背景 + parentCreator.setTitleBackground4Painting(background, opacity); // 容器绘制完整背景 + } + + if (bodyCreator instanceof XBorderStyleWidgetCreator) { + XBorderStyleWidgetCreator styledBodyXCreator = (XBorderStyleWidgetCreator) bodyCreator; + Background background = styledBodyXCreator.getBackground4Painting(); + double opacity = styledBodyXCreator.getBackgroundOpacity4Painting(); + + styledBodyXCreator.setBackground4Painting(null, 0.0); // body不绘制背景 + parentCreator.setBodyBackground4Painting(background, opacity); // 容器绘制完整背景 + } + } + // 根据当前组件边框裁剪内容,如果当前组件存在圆角,则应当按圆角裁剪内容 private void clipByRoundedBorder(Graphics2D g2d) { @@ -273,16 +282,6 @@ public class XBorderStyleWidgetCreator extends XWidgetCreator{ g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) backgroundOpacity4Painting)); Shape shape = new Rectangle2D.Double(0, 0, getWidth(), getHeight()); - if (this instanceof XWTitleLayout) { - // 兼容性考虑,组件样式背景不作用在标题范围内,只作用在控件整体,但同时不能遮挡作用于整体的边框图片 - // 所以考虑样式背景与边框图片都由XWTitleLayout绘制,但样式背景要向下偏移标题栏的高度 - XWTitleLayout titleParent = (XWTitleLayout) this; - if (getComponentCount() > 1) { - XCreator titleCreator = titleParent.getTitleCreator(); - int titleHeight = titleCreator != null ? titleCreator.getHeight() : 0; - shape = new Rectangle2D.Double(0, titleHeight, getWidth(), getHeight() - titleHeight); - } - } background4Painting.paint(g2d, shape); g2d.setComposite(oldComposite); diff --git a/designer-form/src/main/java/com/fr/design/designer/creator/XLabel.java b/designer-form/src/main/java/com/fr/design/designer/creator/XLabel.java index 13358f2b2..3993c4daa 100644 --- a/designer-form/src/main/java/com/fr/design/designer/creator/XLabel.java +++ b/designer-form/src/main/java/com/fr/design/designer/creator/XLabel.java @@ -19,7 +19,6 @@ import com.fr.form.ui.Label; import com.fr.form.ui.container.WParameterLayout; import com.fr.general.Background; -import com.fr.general.act.BorderPacker; import com.fr.stable.ArrayUtils; import com.fr.stable.Constants; import com.fr.stable.core.PropertyChangeAdapter; @@ -36,11 +35,16 @@ import java.beans.IntrospectionException; public class XLabel extends XWidgetCreator { private int cornerSize = 15; + private boolean enabledBackgroundPainting = true; public XLabel(Label widget, Dimension initSize) { super(widget, initSize); } + public void setEnabledBackgroundPainting(boolean enable) { + this.enabledBackgroundPainting = enable; + } + /** * 生成creator对应的控件widget * @@ -93,7 +97,7 @@ public class XLabel extends XWidgetCreator { Dimension size = this.getSize(); //先画背景,再画标题 Background background = label.getBackground(); - if (background != null) { + if (background != null && enabledBackgroundPainting) { Graphics2D g2d = (Graphics2D) g; Composite oldComposite = g2d.getComposite(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, label.getBackgroundOpacity())); diff --git a/designer-form/src/main/java/com/fr/design/designer/creator/XWTitleLayout.java b/designer-form/src/main/java/com/fr/design/designer/creator/XWTitleLayout.java index ba864e607..c04c93a38 100644 --- a/designer-form/src/main/java/com/fr/design/designer/creator/XWTitleLayout.java +++ b/designer-form/src/main/java/com/fr/design/designer/creator/XWTitleLayout.java @@ -12,10 +12,12 @@ import com.fr.form.ui.Widget; import com.fr.form.ui.WidgetTitle; import com.fr.form.ui.container.WAbsoluteLayout.BoundsWidget; import com.fr.form.ui.container.WTitleLayout; +import com.fr.general.Background; import com.fr.general.ComparatorUtils; import java.awt.*; import java.awt.event.ContainerEvent; +import java.awt.geom.Rectangle2D; /** * 一些控件 如图表、报表块,有标题设置,且标题的高度字体等不变 @@ -31,6 +33,11 @@ public class XWTitleLayout extends DedicateLayoutContainer { private static final int INDEX = 0; + protected Background titleBackground4Painting; // 设计器预览界面中绘制title背景图 + protected double titleBackgroundOpacity4Painting; + protected Background bodyBackground4Painting; // 设计器预览界面中绘制body背景图 + protected double bodyBackgroundOpacity4Painting; + public XWTitleLayout() { super(new WTitleLayout("titlePane"), new Dimension()); } @@ -45,6 +52,16 @@ public class XWTitleLayout extends DedicateLayoutContainer { super(widget, initSize); } + public void setTitleBackground4Painting(Background background, double opacity) { + this.titleBackground4Painting = background; + this.titleBackgroundOpacity4Painting = opacity; + } + + public void setBodyBackground4Painting(Background background, double opacity) { + this.bodyBackground4Painting = background; + this.bodyBackgroundOpacity4Painting = opacity; + } + @Override protected void initXCreatorProperties() { super.initXCreatorProperties(); @@ -150,6 +167,43 @@ public class XWTitleLayout extends DedicateLayoutContainer { } + @Override + public void paintBackground(Graphics2D g2d) { + if (getComponentCount() > 1) { + paintTitleBackground(g2d); + paintBodyBackground(g2d); + } + } + + private void paintTitleBackground(Graphics2D g2d) { + if (titleBackground4Painting != null) { + Composite oldComposite = g2d.getComposite(); + g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) this.titleBackgroundOpacity4Painting)); + XCreator titleCreator = getTitleCreator(); + int titleHeight = titleCreator != null ? titleCreator.getHeight() : 0; + + Shape shape = new Rectangle2D.Double(0, 0, getWidth(), titleHeight); + titleBackground4Painting.paint(g2d, shape); + + g2d.setComposite(oldComposite); + } + } + + private void paintBodyBackground(Graphics2D g2d) { + if (bodyBackground4Painting != null) { + Composite oldComposite = g2d.getComposite(); + g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) this.bodyBackgroundOpacity4Painting)); + + XCreator titleCreator = getTitleCreator(); + int titleHeight = titleCreator != null ? titleCreator.getHeight() : 0; + // 兼容性考虑,组件样式背景不作用在标题范围内,只作用在控件整体,但同时不能遮挡作用于整体的边框图片 + // 所以考虑样式背景与边框图片都由XWTitleLayout绘制,但样式背景要向下偏移标题栏的高度 + Shape shape = new Rectangle2D.Double(0, titleHeight, getWidth(), getHeight() - titleHeight); + bodyBackground4Painting.paint(g2d, shape); + + g2d.setComposite(oldComposite); + } + } /** * 将WLayout转换为XLayoutContainer diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/form/FormElementCaseDesigner.java b/designer-realize/src/main/java/com/fr/design/mainframe/form/FormElementCaseDesigner.java index 567345ff2..2dd3dbbb7 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/form/FormElementCaseDesigner.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/form/FormElementCaseDesigner.java @@ -39,7 +39,6 @@ import com.fr.report.elementcase.TemplateElementCase; import com.fr.report.worksheet.FormElementCase; import com.fr.report.worksheet.WorkSheet; -import javax.imageio.ImageIO; import javax.swing.JComponent; import javax.swing.JPanel; import javax.swing.JScrollBar; @@ -49,7 +48,6 @@ import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Transparency; import java.awt.image.BufferedImage; -import java.io.File; /** * 表单中的ElementCase编辑面板 @@ -160,8 +158,6 @@ public class FormElementCaseDesigner grid.setTranslucent(oldTranslucent); } - ImageIO.write(image, "png", new File("/Users/Starryi/test.png")); - } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); }