I have a java application in which I need to rotate one using the and JLabel
methods , I have implemented one which makes it possible to move the .Graphics
Graphics2D
MouseMotionListener
JLabel
if I can rotate the JLabel
, but its content is lost.
This is my code.
public class Textos extends JLabel implements MouseMotionListener {
public void objeto() {
String valor;
if (FTexto.conTexto == true) {
valor = FTexto.TContenidos.getText();
NewJFrame.la = new JLabel(valor) {
public void paintComponent(Graphics g) {
Graphics2D gx = (Graphics2D) g;
gx.rotate(Math.toRadians(90), 10, 100);
super.paintComponent(g);
}
};
NewJFrame.la.setSize(80, 170);
NewJFrame.la.setOpaque(true);
NewJFrame.la.setBackground(Color.yellow);
NewJFrame.la.setFont(new Font(CFuente.getSelectedItem().toString(), Font.PLAIN, Integer.parseInt(String.valueOf(FTexto.CTama.getSelectedItem()))));
NewJFrame.la.setVerticalTextPosition(JLabel.TOP);
NewJFrame.la.setHorizontalTextPosition(JLabel.LEFT);
NewJFrame.PPanel1.add(NewJFrame.la, new Integer(3));
NewJFrame.labels.add(NewJFrame.la);
NewJFrame.indice++;
NewJFrame.la.repaint();
System.out.println(NewJFrame.labels.size());
NewJFrame.con2 = true;
NewJFrame.labels.get(NewJFrame.indice - 1).addMouseMotionListener(this);
} else {
valor = "";
NewJFrame.labels.get(NewJFrame.indice - 1).addMouseMotionListener(this);
}
}
}
What am I doing wrong?
Update:
This is the code that I have to create it JLabel
and Jlabel
it is created in a Frame
call NewJFrame.la
. As you can see I have the dragged method to be able to move the labels
public class Textos extends JLabel implements MouseMotionListener {
public void objeto() {
String valor;
if (FTexto.conTexto == true) {
valor = FTexto.TContenidos.getText();
NewJFrame.la = new JLabel(valor) {
protected void paintComponent(Graphics grafico) {
Graphics2D graficoNuevo = (Graphics2D) grafico;
graficoNuevo.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
AffineTransform at = graficoNuevo.getTransform();
Shape figura = graficoNuevo.getClip();
double X = getWidth() / 2.0;
double Y = getHeight() / 2.0;
at.rotate(Math.toRadians(90), X, Y);
graficoNuevo.setTransform(at);
graficoNuevo.setClip(figura);
super.paintComponent(grafico);
}
};
NewJFrame.la.setSize(200, 30);
NewJFrame.la.setOpaque(true);
NewJFrame.la.setBackground(Color.yellow);
NewJFrame.la.setFont(new Font(CFuente.getSelectedItem().toString(), Font.PLAIN, Integer.parseInt(String.valueOf(FTexto.CTama.getSelectedItem()))));
NewJFrame.la.setVerticalTextPosition(JLabel.TOP);
NewJFrame.la.setHorizontalTextPosition(JLabel.LEFT);
NewJFrame.PPanel1.add(NewJFrame.la, new Integer(3));
NewJFrame.labels.add(NewJFrame.la);
NewJFrame.indice++;
NewJFrame.la.repaint();
System.out.println(NewJFrame.labels.size());
NewJFrame.con2 = true;
NewJFrame.labels.get(NewJFrame.indice - 1).addMouseMotionListener(this);
} else {
valor = "";
NewJFrame.labels.get(NewJFrame.indice - 1).addMouseMotionListener(this);
}
}
@Override
public void mouseDragged(MouseEvent me) {
if (NewJFrame.labels.size() >= 1) {
if (me.getSource() == NewJFrame.labels.get(0)) {
NewJFrame.labels.get(0).setLocation(NewJFrame.labels.get(0).getX() + me.getX(), NewJFrame.labels.get(0).getY() + me.getY());
}
}
if (NewJFrame.labels.size() >= 2) {
if (me.getSource() == NewJFrame.labels.get(1)) {
NewJFrame.labels.get(1).setLocation(NewJFrame.labels.get(1).getX() + me.getX(), NewJFrame.labels.get(1).getY() + me.getY());
}
}
if (NewJFrame.labels.size() >= 3) {
if (me.getSource() == NewJFrame.labels.get(2)) {
NewJFrame.labels.get(2).setLocation(NewJFrame.labels.get(2).getX() + me.getX(), NewJFrame.labels.get(2).getY() + me.getY());
}
}
if (NewJFrame.labels.size() >= 4) {
if (me.getSource() == NewJFrame.labels.get(3)) {
NewJFrame.labels.get(3).setLocation(NewJFrame.labels.get(3).getX() + me.getX(), NewJFrame.labels.get(3).getY() + me.getY());
}
}
}
@Override
public void mouseMoved(MouseEvent me) {
if (NewJFrame.labels.size() >= 1) {
NewJFrame.labels.get(0).setCursor(new Cursor(Cursor.MOVE_CURSOR));
}
if (NewJFrame.labels.size() >= 2) {
NewJFrame.labels.get(1).setCursor(new Cursor(Cursor.MOVE_CURSOR));
}
if (NewJFrame.labels.size() >= 3) {
NewJFrame.labels.get(2).setCursor(new Cursor(Cursor.MOVE_CURSOR));
}
if (NewJFrame.labels.size() >= 4) {
NewJFrame.labels.get(3).setCursor(new Cursor(Cursor.MOVE_CURSOR));
}
}
}
Code
Result
Explanation
In addition to the method that you are already using
paintComponent()
, you can also make use of the AffineTransform library , which, as you can see in said documentation, this library is used to rotate graphics and images. Also, for this example we have used the libraryMath
indicating it as radians 180, for which we have rotated the JLabel from one end to the other.