使用java graphics 2d翻译,缩放,旋转和反射矩形

时间:2015-11-25 04:06:24

标签: java graphics rotation translate

我遇到了一个问题,我想要做的是基本上创建一个面板,我可以翻译,缩放,旋转和反射一个矩形,通过在文本框上提供一些信息并单击一个按钮,我已经绘制了一个矩形,我可以移动在绘制它之前,我需要在绘制之后对其进行转换,我想要的只是转换矩形而不是引导线,我该怎么做?这是我的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Ventana extends JFrame implements ActionListener {
    MiPanel panelFigura = new MiPanel();
    JPanel panelBotones = new JPanel();
    JButton btnTransladar = new JButton("Translador"); // Means translate
    JButton btnEscala = new JButton("Escala"); // Means Scale
    JButton btnRotar = new JButton("Rotar"); // Means Rotate
    JTextField txT = new JTextField(4);
    JTextField tyT = new JTextField(4);
    JTextField exT = new JTextField(4);
    JTextField eyT = new JTextField(4);
    JTextField gradoT = new JTextField(3);

    public Ventana() {
        setSize(800, 600);
        setVisible(true);
        setTitle("Unidad 2");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        add(panelFigura);
        panelBotones.add(new JLabel("x: "));
        panelBotones.add(txT);
        panelBotones.add(new JLabel("y: "));
        panelBotones.add(tyT);
        btnTransladar.addActionListener(this);
        panelBotones.add(btnTransladar);
        panelBotones.add(new JLabel("x: "));
        panelBotones.add(exT);
        panelBotones.add(new JLabel("y: "));
        panelBotones.add(eyT);
        panelBotones.add(btnEscala);
        panelBotones.add(new JLabel("º: "));
        panelBotones.add(gradoT);
        panelBotones.add(btnRotar);

        panelBotones.setBackground(Color.LIGHT_GRAY);
        btnRotar.addActionListener(this);
        btnTransladar.addActionListener(this);
        btnEscala.addActionListener(this);
        add(panelBotones, BorderLayout.SOUTH);

    }

    public class MiPanel extends JPanel {
        public int tx = 0, ty = 0, ex = 0, ey = 0, grado = 0; // tx and ty are
                                                                // how many
                                                                // pixel is the
                                                                // rectangle
                                                                // moving, ex
                                                                // and ex is how
                                                                // much is
                                                                // scaling and
                                                                // grado is the
                                                                // angle of the
                                                                // rotation
        Rectangle2D.Double rectangulo = new Rectangle2D.Double(tx, ty, 60, 60);

        public MiPanel() {
            this.setSize(800, 400);
            this.setBackground(Color.WHITE);
        }

        public void paintComponent(Graphics g) {
            // super.paintComponent( g );
            Graphics2D g2D = (Graphics2D) g;
            g2D.translate((this.getWidth() / 2), (this.getHeight() / 2));
            g2D.drawLine((0 - (this.getWidth() / 2)), 0, (this.getWidth() / 2),
                    0);
            g2D.drawLine(0, this.getHeight(), 0, (0 - this.getHeight() / 2));
            g2D.draw(rectangulo);

        }
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == btnTransladar) {
            panelFigura.tx = Integer.getInteger(txT.getText());
            panelFigura.ty = Integer.getInteger(tyT.getText());
        }

        if (e.getSource() == btnEscala) {
            panelFigura.ex = Integer.getInteger(exT.getText());
            panelFigura.ey = Integer.getInteger(eyT.getText());
        }

        if (e.getSource() == btnRotar) {
            panelFigura.grado = Integer.getInteger(gradoT.getText());
        }

    }
}

感谢您帮助我!

0 个答案:

没有答案