Добрый день, мне нужна помощь, я хотел увеличить размер квадрата, когда я использую угол 0 и 270, он работает нормально, но когда слово с углом 90 и 180, когда оно увеличивается с помощью мыши, оно не соблюдает правило, следуйте данным на панели
Угол: 0
Угол 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();
}
}