gpt4 book ai didi

java - 在另一个组件中绘制外部组件(即属于不同框架的内容 Pane )

转载 作者:行者123 更新时间:2023-11-30 05:05:23 24 4
gpt4 key购买 nike

大家好。我想绘制一个外部组件(即属于不同框架的内容 Pane ),我们将其称为框架 B,位于框架 A 的组件内。

问题是,当我绘制组件时,它也会在框架 A 的内容 Pane 中绘制,当框架重新调整大小时,它也会闪烁或变得丑陋(即绘制很少的内容)次在组件内部,出现一些蓝色方 block 等)。例如,如果我尝试在绘制之前缩放或平移外部组件,则问题会变得更加明显。

我想,过了一会儿我就整理好了。但我对这个解决方案感觉不太好,出于某种原因,我相信可能有更好、更合适的解决方案。这里我需要你。 :)

这个问题更多的是要求解释为什么外部组件被错误地绘制,而不在组件内部绘制之前和之后操纵它的双缓冲功能。例如,使用任意一对setDoubleBuffered(false) 和 setDoubleBuffered(true)或者禁用DoubleBuffering(jP)和启用DoubleBuffering(jP)

分别在调用外部组件的paint方法之前和之后。

提前谢谢您。显示问题的 SSCCE 如下所示。


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.RepaintManager;

public class PaintForeignComponentSSCCE extends JFrame
{
public static void main(String[] args) throws IOException
{
//foreign panel
JPanel fp = new JPanel();
fp.setBackground(Color.PINK);
fp.setPreferredSize(new Dimension(200, 300));
//component in which the foreign panel is painted
ForeignComponentPainter fcp = new ForeignComponentPainter(fp);
fcp.setPreferredSize(new Dimension(600, 600));
//main frame's content
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.BLUE);
contentPane.add(fcp);
//main frame
JFrame f = new PaintForeignComponentSSCCE();
f.setContentPane(contentPane);
f.setSize(700, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
//foreign panel frame
JFrame fpf = new JFrame();
JPanel panelFrameContent = new JPanel();
panelFrameContent.add(fp);
fpf.setContentPane(panelFrameContent);
fpf.setSize(400, 400);
fpf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
fpf.setVisible(true);
}
}

class ForeignComponentPainter extends JButton
{
private static final long serialVersionUID = 1L;
private JPanel jP;

public ForeignComponentPainter(JPanel jP)
{
super();
this.jP = jP;
}

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawString("OIOI", 50, 50);
//g2.translate(100, 50);
//g2.scale(.5, .5);
// jP.setDoubleBuffered(false);
// disableDoubleBuffering(jP);
jP.paint(g2);
// jP.setDoubleBuffered(true);
// enableDoubleBuffering(jP);
//g2.scale(1/.5, 1/.5);
}
public static void disableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
public static void enableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}

与 SSCCE 示例无关。 以下内容与问题本身无关。这段代码的目的是展示我如何在组件中实现 Printable,我也想以打印预览方式呈现该组件。 print调用组件的paint(如下所示)。


public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if(pageIndex >= pageHeights.size())
return NO_SUCH_PAGE;
int savedPage = currentPageIndex;
currentPageIndex = pageIndex;
Graphics2D g2 = (Graphics2D) g;
paint(g2);
currentPageIndex = savedPage;
return PAGE_EXISTS;
}

最佳答案

抱歉,我无法直接回答您的问题,但通过对同一模型有两种不同的 View 也许可以避免该问题。如图How to Write a Document Listener ,可以更新多个view 文档的。您的模型和 View 可能不同,但该概念仍然适用。

附录:

I think that is what I am doing at the moment, isn't it? I have one model i.e. the 'foreign' component, and two views one doing default paint and second custom paint.

不,您有一个 View 正在更新另一个 View ;您需要两个 View 来响应一个模型。此相关example可能会提供一些见解。

I am still interested in suggestions as to the reasons of this erroneous painting.

在我看来,将两个组件的更新交错的尝试从根本上来说是有缺陷的;它颠覆了Painting in Swing的正常流程。在我的平台上,我只看到非常短暂的闪烁,看不到任何偶然的绘画。尽管在一个系统上可能获得令人满意的结果,但这种安排在不同的实现中可能并不可靠。

Is a simple call to repaint of the label, placed in the ModelObserver's update() method the appropriate solution?

是的,repaint(),但应该在 ButtonHandler 中完成对于查看:

private class ButtonHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
PieceButton pb = (PieceButton) e.getSource();
icon.color = pb.piece.color;
label.repaint();
model.check(pb.piece);
}
}

关于java - 在另一个组件中绘制外部组件(即属于不同框架的内容 Pane ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5274962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com