增加java方块大小

nhjlsmyf  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(363)

下午好,我需要帮助,我想增加正方形的大小,当我使用Angular 0和270时,它正常工作,但是当单词的Angular 是90和180时,当它用鼠标增加时,它不遵守规则,按照面板上的数据
Angular :0

Angular 90

此处代码:

txtpn1 = new JEditorPane() {
            @Override
            protected void paintComponent(Graphics g) {
                int angle = txtpn[0];  //Angulo 0,90,180,270 
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                AffineTransform aT = g2.getTransform();
                Shape oldshape = g2.getClip();
                double x = getWidth() / 2.0;
                double y = getHeight() / 2.0;
                aT.rotate(Math.toRadians(angle), x, y);
                g2.setTransform(aT);
                super.paintComponent(g);
            }

        txtpn1.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent arg0) {
                arrastarObejto(txtpn1, arg0);
        }
    }
    public void arrastarObejto(JComponent component, MouseEvent e) {
        if (component.getCursor().getType() != 11 && component.getCursor().getType() != 9) {
            component.setLocation(component.getLocation().x + e.getX(), component.getLocation().y + e.getY());
        }

        if (component.getCursor().getType() == 11 || component.getCursor().getType() == 9) {
            component.setSize(e.getX(), e.getY()); //Here is the error
       }
   }

简单的代码,当正方形成90度时,我不能拉伸它

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;

public class help extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    help frame = new help();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public help() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblCorrect = new JLabel("Correct"){

            @Override
            protected void paintComponent(Graphics g) {
                int angle = 0;
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                AffineTransform aT = g2.getTransform();
                //Shape oldshape = g2.getClip();
                double x = getWidth() / 2.0;
                double y = getHeight() / 2.0;
                aT.rotate(Math.toRadians(angle), x, y);
//                  g2.transform(aT);
                g2.setTransform(aT);
                //g2.setClip(oldshape);
                super.paintComponent(g);
            }

        };
        lblCorrect.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent arg0) {
                lblCorrect.setSize(arg0.getX(),arg0.getY());
            }
        });
        lblCorrect.setBorder(new LineBorder(new Color(0, 0, 0)));
        lblCorrect.setBounds(48, 100, 74, 51);
        contentPane.add(lblCorrect);

        JLabel lblWrong = new JLabel("Wrong"){

            @Override
            protected void paintComponent(Graphics g) {
                int angle = 90;
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                AffineTransform aT = g2.getTransform();
                //Shape oldshape = g2.getClip();
                double x = getWidth() / 2.0;
                double y = getHeight() / 2.0;
                aT.rotate(Math.toRadians(angle), x, y);
//                  g2.transform(aT);
                g2.setTransform(aT);
                //g2.setClip(oldshape);
                super.paintComponent(g);
            }

        };
        lblWrong.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                lblWrong.setSize(e.getX(),e.getY());
            }
        });
        lblWrong.setBorder(new LineBorder(new Color(0, 0, 0)));
        lblWrong.setBounds(239, 100, 74, 51);
        contentPane.add(lblWrong);
    }
}


其他代码:

package teste;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.*;

import javax.swing.*;
import javax.swing.border.LineBorder;
public class help extends JFrame {
   public help() {
      setTitle("Rotate JLabel");
      JLabel label = new RotateLabel("TutorialsPoint");
      add(label, BorderLayout.CENTER);
      setSize(400, 300);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
      label.setBorder(new LineBorder(new Color(0, 0, 0)));
      label.addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseDragged(MouseEvent e) {
            // TODO Auto-generated method stub
            label.setSize(e.getX(),e.getY());

        }
    });
   }
   private class RotateLabel extends JLabel {
      public RotateLabel(String text) {
         super(text);
         Font font = new Font("Verdana", Font.ITALIC, 10);
         FontMetrics metrics = new FontMetrics(font){};
         Rectangle2D bounds = metrics.getStringBounds(text, null);
         setBounds(0, 0, (int) bounds.getWidth(), (int) bounds.getHeight());
      }
      @Override
      public void paintComponent(Graphics g) {
         Graphics2D gx = (Graphics2D) g;
         gx.rotate(Math.PI/2, getX() + getWidth()/2, getY() + getHeight()/2);
         super.paintComponent(g);
      }
   }
   public static void main(String[] args) {
      new help();
   }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题