Cursor Position in JTextField not properly aligned with text

22 views
Skip to first unread message

Christian Ledergerber

unread,
Apr 2, 2015, 8:05:59 PM4/2/15
to piccolo...@googlegroups.com
Hi,

I have been using Piccolo2d quite extensively in my current Project at work. It is very flexible - great job!

Now, this is a question regarding what I think is a problem / bug in piccolo2d:

If I use a JTextField on a PSwingCanvas and zoom then the text cursor is not aligned correctly with the text. In below image you would expect, when hitting delete, that the character 'f' will be deleted. However, 'd' is deleted. So, the cursor is logically between 'd' and 'f'. However, it is displayed visually too far to the right.



The misalignment gets worse the more text I enter in the Text Field / e.g. the further right the cursor is positioned. If the cursor is on the very left within the text field the alignment is correct.

The question is, does anybody have experience with this problem? Is there a way to fix it? Where would I have to dig in the Piccolo2d code for an answer?

Below you find the modified PSwingScaleExample which demonstrates the issue.

Thank you,

Christian



import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.piccolo2d.PCamera;
import org.piccolo2d.PLayer;
import org.piccolo2d.PNode;
import org.piccolo2d.extras.PFrame;
import org.piccolo2d.extras.pswing.PSwing;
import org.piccolo2d.extras.pswing.PSwingCanvas;
import org.piccolo2d.util.PBounds;

/**
 * Demonstrate that PSwing nodes properly receive events even when they are parented by nodes
 * with extreme scales. This is an effective regression test that previously failed before fix
 * applied to {@link org.piccolo2d.extras.pswing.PSwingEventHandler}.
 */
public class PSwingScaleExample extends PFrame {

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

    public PSwingScaleExample() {
        super(PSwingScaleExample.class.getSimpleName(), false, new PSwingCanvas());
    }

    public void initialize() {
        final PSwingCanvas canvas = (PSwingCanvas) getCanvas();
       
//        PMouseWheelZoomEventHandler mouseWheelZoomEventHandler = new PMouseWheelZoomEventHandler();
//        mouseWheelZoomEventHandler.zoomAboutMouse();
//        canvas.addInputEventListener(mouseWheelZoomEventHandler);
       
        final PLayer layer = canvas.getLayer();
        final PCamera camera = canvas.getCamera();
       
        PNode parent = new PNode();
        parent.setPaint(Color.orange);
        parent.setBounds(0, 0, 200, 200);
       
        JTextField button = new JTextField("Drink Me");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(PSwingScaleExample.this, "Thank You");
            }
        });
        final PSwing ps = new PSwing(button);
        centerFullBoundsIn(ps, parent.getGlobalFullBounds());
        parent.addChild(ps);
        parent.scale(0.001);

        layer.addChild(parent);

        camera.animateViewToCenterBounds(ps.getGlobalFullBounds(), true, 0);
    }
   
    private static void centerFullBoundsIn(PNode centerMe, PBounds bounds) {
        centerMe.centerFullBoundsOnPoint(bounds.getCenterX(), bounds.getCenterY());
    }

}

Reply all
Reply to author
Forward
0 new messages