首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Piccolo可缩放界面,如何在缩放时更改文本

Piccolo可缩放界面,如何在缩放时更改文本
EN

Stack Overflow用户
提问于 2012-10-18 01:25:04
回答 3查看 774关注 0票数 2

我正在尝试一些Piccolo来创建一个可缩放的界面。

我正在画布上创建一个矩形,上面有一些PText。现在,当缩放时,我想要将文本更改为不同的内容。

我已经在我的初始化中这样做了:

代码语言:javascript
复制
//
        //specify the current Piccolo PCanvas
        //
        m_canvas = getCanvas();
        m_canvas.removeInputEventListener(m_canvas.getPanEventHandler());
        //m_canvas.addInputEventListener(new ClickAndDragHandler(m_canvas));

        //
        //add nodes to the collection -> adding to the collection = adding to the canvas
        //
        m_nodecollection = new NodeCollection(m_canvas);

        RectangleNode node_links = new RectangleNode();
        node_links.setBounds(10, 10, 500, 500);
        m_nodecollection.addNode(node_links);

        RectangleNode node_rechts = new RectangleNode();
        node_rechts.setBounds(600,10,500,500);
        m_nodecollection.addNode(node_rechts);

        PImage node_arrowLeft = new PImage("left.gif");
        node_arrowLeft.setBounds(600, 550, node_arrowLeft.getWidth(), node_arrowLeft.getHeight());
        m_nodecollection.addNode(node_arrowLeft);

        PImage node_arrowRight = new PImage("right.gif");
        node_arrowRight.setBounds(680, 550, node_arrowRight.getWidth(), node_arrowRight.getHeight());
        m_nodecollection.addNode(node_arrowRight);

        m_nodecollection.connectNodesWithLine(node_rechts, node_arrowRight, true);
        m_nodecollection.connectNodesWithLine(node_rechts, node_arrowLeft, true);


        PText node_text = new PText("Zoomlevel Not Supported");
        node_text.setBounds(180,150, node_text.getWidth(), node_text.getHeight());
        m_nodecollection.connectNodesWithLine(node_links, node_text, true);
        m_nodecollection.addNode(node_text);
        node_links.addChild(node_text);
        node_links.setCollection(m_nodecollection);

创建了我自己的rectangle类,其中包含整个节点集合和作为成员变量的PText。

代码语言:javascript
复制
public class RectangleNode extends PNode{

    private Rectangle2D m_rectangle;
    private NodeCollection collection;
private PText text;

    public RectangleNode()
    {
        m_rectangle = new Rectangle2D.Double();
    }

    public Rectangle2D getRectangle()
    {
        if(m_rectangle == null)
            m_rectangle = new Rectangle2D.Double();
        return m_rectangle;
    }

    public boolean setBounds(double x, double y, double w, double h)
    {
        if(super.setBounds(x, y, w, h))
        {
            m_rectangle.setFrame(x, y, w, h);
            return true;
        }
        return false;
    }

    public void setCollection(NodeCollection collection)
    {
        this.collection = collection;
    }
        public void setText(PText text)
{
    this.text = text;
}
    public void paint(PPaintContext paintcontext)
    {
        Graphics2D g2 = paintcontext.getGraphics();

        //niet ingezoomd
        if(paintcontext.getScale() <= 0.2)
            g2.setPaint(Color.BLACK);

        //half ingezoomd
        if(paintcontext.getScale() > 0.2 && paintcontext.getScale() < 0.7)
        {

            g2.setPaint(Color.BLACK);

        }

        //volledig ingezoomd
        if(paintcontext.getScale() > 0.7)
        {
            g2.setPaint(Color.RED);
            g2.fill(getRectangle());
            g2.setPaint(Color.BLACK);
            g2.draw(getRectangle());
        }
    }
}

现在,我想我可以像这样更改文本: text.setText("Tester");但它不起作用,例如,当设置文本然后将节点添加到集合中时,它会在当前文本上显示一个巨大的错误...

有人能帮帮我吗?

致以亲切的问候,

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-20 08:38:31

考虑将整个示例发布为SSCCE,看起来缺少某些代码部分。目前还不清楚您实际是如何执行setText的。

组成现有节点和侦听从摄像头激发的事件可能会更容易。考虑下面的示例,该示例绘制了一个矩形,其中包含一些根据缩放级别更新的文本:

代码语言:javascript
复制
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.nodes.PText;
import edu.umd.cs.piccolo.util.PPaintContext;
import edu.umd.cs.piccolox.PFrame;

public class TestRectZoom extends PFrame {
    public TestRectZoom() {
        super("TestRectZoom", false, null);
    }

    public void initialize() {
        getCanvas().setInteractingRenderQuality(
                PPaintContext.HIGH_QUALITY_RENDERING);
        getCanvas().setDefaultRenderQuality(
                PPaintContext.HIGH_QUALITY_RENDERING);
        getCanvas().setAnimatingRenderQuality(
                PPaintContext.HIGH_QUALITY_RENDERING);

        final PPath rect = PPath.createRectangle(100, 100, 200, 200);
        rect.setPaint(Color.GREEN);
        getCanvas().getLayer().addChild(rect);

        final PText text = new PText(getZoomLevelString());
        rect.addChild(text);

        text.centerFullBoundsOnPoint(rect.getBounds().getCenterX(), rect
                .getBounds().getCenterY());

        getCanvas().getCamera().addPropertyChangeListener(
                PCamera.PROPERTY_VIEW_TRANSFORM, new PropertyChangeListener() {
                    public void propertyChange(final PropertyChangeEvent evt) {
                        text.setText(getZoomLevelString());

                        if (getCanvas().getCamera().getViewScale() > 0.9) {
                            rect.setPaint(Color.GREEN);
                        } else {
                            rect.setPaint(Color.RED);
                        }
                    }
                });
    }

    private String getZoomLevelString() {
        return "Zoom level:"
                + String.format("%.2f", getCanvas().getCamera().getViewScale());
    }

    public static void main(final String[] args) {
        new TestRectZoom();
    }
}

这就是结果的样子:

票数 2
EN

Stack Overflow用户

发布于 2013-01-01 13:32:54

您正在寻找Piccolo Patterns页面上提供的语义缩放。模式17:语义缩放

http://www.piccolo2d.org/learn/patterns.html

票数 1
EN

Stack Overflow用户

发布于 2014-02-10 16:57:00

我按照Aqua的建议解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12939896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档