简体   繁体   中英

Java ImageView get position and set alignment

http://docs.oracle.com/javase/6/docs/api/javax/swing/text/html/ImageView.html

JEditorPane contains within itself the ImageView. I can get the size and alignment:

    ImageView.getPreferredSpan(View.X_AXIS)); //20px
    ImageView.getPreferredSpan(View.Y_AXIS)); //20px
    ImageView.getAlignment(View.X_AXIS)); //0.5
    ImageView.getAlignment(View.Y_AXIS)); //1.0

I need to get the coordinates x/y (absolute or relative) and change the y alignment to 0.75. How can I do this?

For reference, the value derives from the align attribute of the img tag, as defined in the applicable HTML 3.2 Reference Specification . Values other than left (0.0), middle (0.5) or right (1.0) are not supported directly.

A bit late, but I ran into the same problem. The only way to fix this, or so I did it, was, to create your own ImageView and overwrite the vAlign value in its setPropertiesFromAttributes() function. Problem is, that setPropertiesFromAttributes is protected, so you have to copy and paste the whole original code of ImageView into your own, and then replace the code to:

    vAlign = 1.0f;
    if (alignment != null) {
        alignment = alignment.toString();
        if ("top".equals(alignment)) {
            vAlign = 0f;
        }
        else if ("middle".equals(alignment)) {
            vAlign = .5f;
        }
    }
    vAlign = .745f;

You also need to create your own HTMLEditorKit and overwrite getViewFactory():

@Override
    public ViewFactory getViewFactory() {

        return new HTMLEditorKit.HTMLFactory() {

            public View create(Element e) {

                View v = super.create(e);

                Object o = e.getAttributes().getAttribute(StyleConstants.NameAttribute);

                if (o instanceof HTML.Tag) {

                    HTML.Tag kind = (HTML.Tag) o;

                    if (kind == HTML.Tag.IMG) {
                        return new MyImageView(e);
                    }
                }
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM