在 Codename One 中翻转(反转)Y 轴

问题描述 投票:0回答:1

这是反转 y 轴以从下到上绘制(而不是像往常一样从上到下)的代码。

import javax.swing.*;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class InvertedYAxisSquare extends JPanel {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Inverted Y-Axis Square");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new InvertedYAxisSquare());
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.BLUE);
        AffineTransform at = new AffineTransform();
        at.scale(1, -1);
        at.translate(0, -getHeight());
        g2d.transform(at);
        g2d.fillRect(50, 0, 100, 111);
        g2d.dispose();
    }
}

请提供使用transform的类似代码和代号one库。 我的尝试没有成功,例如这个:

---------
@Override
public void paint(Graphics g) {
    super.paint(g);
    int height = getHeight();
    Transform t = Transform.makeIdentity();
    t.scale(1, -1);
    t.translate(0, -height);
    g.setTransform(t);
    g.setColor(0x0000ff);
    g.fillRect(50, 0, 100, 100);
    g.resetAffine();
}
--------

enter image description here

该正方形位于高于中间的某个位置,而不是应在的最底部。

java graphics codenameone
1个回答
0
投票

这样的东西就是你想要的:

final Form f = new Form(new BorderLayout());
        Component custom = new Component() {
            @Override
            public void paint(Graphics g) {
                int height = getHeight();
                Transform t = g.getTransform();
                Transform orig = t.copy();
                t.translate(0, height);
                t.scale(1, -1);
                g.setTransform(t);
                g.setColor(0x0000ff);
                g.fillRect(50, 0, 100, 111);
                g.setTransform(orig); // Could also just do g.resetAffine()
            }
        };
        f.add(BorderLayout.CENTER, custom);
        f.show();

要点:您需要首先从图形上下文中获取当前变换,然后修改该变换。然后将图形变换设置为您修改后的变换。然后将其恢复到原来的样子。

© www.soinside.com 2019 - 2024. All rights reserved.