I have a little problem with my code, which has to do with the Graphics class. I need to draw a robot and after that I have to rotate the number of degrees set. I've tried with AffineTransform.getRotateInstance (rotate, centerx, centery), but I have not been able to call all the figures together (they're in a class called Figure) so I can rotate them, I need to call that class (Figure) in JPanelWorkArea to spin the robot, please if someone can help me, I would be very grateful. Figure class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
public class Figure {
private Graphics2D graphics2D;
private int size;
private Point point;
private double rotate;
/**
* Constructor encargado de asignarle un valor al tamaño y coordenadas del robot
*
* @param size
* Tamaño de la figura
* @param point
* Coordenada de la figura
*/
public Figure(int size, Point point, double rotate) {
this.size = size;
this.point = point;
this.rotate = rotate;
}
/**
* Asigna un determinado grafico para la construccion del robot
*
* @param graphics2D
* Grafico asignado
*/
public void setGraphics(Graphics2D graphics2D) {
this.graphics2D = graphics2D;
}
public Graphics2D getGraphics() {
return this.graphics2D;
}
/**
* Encargado de construir el sombrero del robot mediante un triangulo, con ayuda
* del poligono
*/
private void hat() {
int[] hatX1 = { this.point.x + this.size / 2, this.point.x, this.point.x + this.size };
int[] hatY1 = { this.point.y - this.size * 7 / 8, this.point.y, this.point.y };
graphics2D.setColor(new Color(219, 32, 32));
graphics2D.fillPolygon(hatX1, hatY1, 3);
}
/**
* Encargado de construir la cara del robot, con ayuda de un cuadrado
*/
private void face() {
graphics2D.setColor(new Color(113, 126, 140));
graphics2D.fillRect(this.point.x, this.point.y, this.size, this.size);
}
/**
* Encargado de construir las orejas del robot, con ayuda de arcos
*/
private void ears() {
graphics2D.setColor(new Color(252, 88, 93));
graphics2D.fillArc(this.point.x - this.size / 4, this.point.y + this.size / 4, this.size / 2, this.size / 4, 90,
180);
graphics2D.fillArc(this.point.x + this.size * 3 / 4, this.point.y + this.size / 4, this.size / 2, this.size / 4,
90, -180);
}
/**
* Encargado de construir los ojos del robot, a partir de cuadrados, y poligonos
* de rombos para las pupilas
*/
private void eyes() {
int[] eye1X = { this.point.x + this.size / 4, (int) (this.point.x + this.size * 1.5 / 8),
this.point.x + this.size / 4, (int) (this.point.x + this.size * 2.5 / 8) };
int[] eye1Y = { (int) (this.point.y + this.size * 2.5 / 8), this.point.y + this.size * 3 / 8,
(int) (this.point.y + this.size * 3.5 / 8), this.point.y + this.size * 3 / 8 };
int[] eye2X = { this.point.x + this.size * 3 / 4, (int) (this.point.x + this.size * 5.5 / 8),
this.point.x + this.size * 3 / 4, (int) (this.point.x + this.size * 6.5 / 8) };
int[] eye2Y = { (int) (this.point.y + this.size * 2.5 / 8), this.point.y + this.size * 3 / 8,
(int) (this.point.y + this.size * 3.5 / 8), this.point.y + this.size * 3 / 8 };
graphics2D.setColor(new Color(211, 211, 211));
graphics2D.fillRect(this.point.x + this.size / 8, this.point.y + this.size / 4, this.size / 4, this.size / 4);
graphics2D.fillRect(this.point.x + this.size * 5 / 8, this.point.y + this.size / 4, this.size / 4, this.size / 4);
graphics2D.setColor(new Color(0, 0, 0));
graphics2D.fillPolygon(eye1X, eye1Y, 4);
graphics2D.fillPolygon(eye2X, eye2Y, 4);
}
/**
* Encargado de construir la boca del robot mediante un rectangulo, y una linea
* para los dientes
*/
private void mouth() {
graphics2D.setColor(Color.BLACK);
graphics2D.fillRect(this.point.x + this.size / 8, this.point.y + this.size * 5 / 8, this.size * 3 / 4,
this.size / 4);
graphics2D.setColor(Color.WHITE);
graphics2D.drawLine(this.point.x + this.size / 8, this.point.y + this.size * 3 / 4,
this.point.x + this.size * 7 / 8, this.point.y + this.size * 3 / 4);
}
/**
* Encargado de construir la columna del robot, a traves de un rectangulo
*/
private void column() {
graphics2D.setColor(new Color(111, 111, 111));
graphics2D.fillRect(this.point.x + this.size * 3 / 8, this.point.y + this.size, this.size / 4,
this.size * 11 / 4);
}
/**
* Encargado de construir el tronco del robot, junto con un cajon, con ayuda de
* cuadrados
*/
private void trunk() {
graphics2D.setColor(new Color(113, 126, 140));
graphics2D.fillRect(this.point.x - this.size / 8, this.point.y + this.size * 11 / 8, this.size * 5 / 4,
this.size * 5 / 4);
graphics2D.setColor(new Color(99, 99, 99));
graphics2D.drawRect(this.point.x, this.point.y + this.size * 3 / 2, this.size, this.size);
graphics2D.fillRect((int) (this.point.x + this.size * 0.3 / 8), (int) (this.point.y + this.size * 12.3 / 8),
(int) (this.size * 7.6 / 8), (int) (this.size * 7.6 / 8));
graphics2D.setColor(Color.BLACK);
graphics2D.fillOval((int) (this.point.x + this.size * 6.5 / 8), (int) (this.point.y + this.size * 15.5 / 8),
this.size / 8, this.size / 8);
}
/**
* Encargado de construir los hombros del robot con ayuda de rectangulos
*/
private void shoulder() {
graphics2D.setColor(Color.BLACK);
graphics2D.fillRect(this.point.x - this.size * 3 / 8, this.point.y + this.size * 3 / 2, this.size / 4,
this.size * 5 / 8);
graphics2D.fillRect(this.point.x + this.size * 9 / 8, this.point.y + this.size * 3 / 2, this.size / 4,
this.size * 5 / 8);
graphics2D.fillRect(this.point.x - this.size / 2, this.point.y + this.size * 13 / 8, this.size / 8,
this.size * 3 / 8);
graphics2D.fillRect((int) (this.point.x + this.size * 11.1 / 8), this.point.y + this.size * 13 / 8, this.size / 8,
this.size * 3 / 8);
}
/**
* Encargado de construir los brazos del robot, mediante rectangulos
*/
private void arms() {
graphics2D.setColor(new Color(113, 126, 140));
graphics2D.fillRect(this.point.x - this.size * 3 / 4, (int) (this.point.y + this.size * 7 / 4), this.size / 4,
this.size / 8);
graphics2D.fillRect(this.point.x + this.size * 3 / 2, (int) (this.point.y + this.size * 7 / 4), this.size / 4,
this.size / 8);
graphics2D.fillRect(this.point.x - this.size * 3 / 4, (int) (this.point.y + this.size * 7 / 4), this.size / 8,
(int) (this.size * 7 / 8));
graphics2D.fillRect(this.point.x + this.size * 7 / 4, (int) (this.point.y + this.size * 7 / 4), this.size / 8,
(int) (this.size * 7 / 8));
}
/**
* Encargado de construir las manos del robot, con ayuda de poligonos
* irregulares
*/
private void hands() {
graphics2D.setColor(new Color(35, 92, 132));
int[] hand1X = { this.point.x - this.size * 3 / 4, this.point.x - this.size * 7 / 8,
(int) (this.point.x - this.size * 6.5 / 8), this.point.x - this.size * 3 / 4,
this.point.x - this.size * 5 / 8, (int) (this.point.x - this.size * 4.5 / 8),
this.point.x - this.size / 2, this.point.x - this.size * 5 / 8 };
int[] hand1Y = { this.point.y + this.size * 21 / 8, this.point.y + this.size * 11 / 4,
this.point.y + this.size * 23 / 8, this.point.y + this.size * 11 / 4, this.point.y + this.size * 11 / 4,
this.point.y + this.size * 23 / 8, this.point.y + this.size * 11 / 4,
this.point.y + this.size * 21 / 8 };
int[] hand2X = { this.point.x + this.size * 7 / 4, this.point.x + this.size * 13 / 8,
(int) (this.point.x + this.size * 13.5 / 8), this.point.x + this.size * 7 / 4,
this.point.x + this.size * 15 / 8, (int) (this.point.x + this.size * 15.5 / 8),
this.point.x + this.size * 2, this.point.x + this.size * 15 / 8 };
int[] hand2Y = { this.point.y + this.size * 21 / 8, this.point.y + this.size * 11 / 4,
this.point.y + this.size * 23 / 8, this.point.y + this.size * 11 / 4, this.point.y + this.size * 11 / 4,
this.point.y + this.size * 23 / 8, this.point.y + this.size * 11 / 4,
this.point.y + this.size * 21 / 8 };
graphics2D.fillPolygon(hand1X, hand1Y, 8);
graphics2D.fillPolygon(hand2X, hand2Y, 8);
}
/**
* Encargado de construir el cinturon del robot, con un rectangulo
*/
private void belt() {
graphics2D.setColor(new Color(188, 120, 24));
graphics2D.fillRect(this.point.x, this.point.y + this.size * 21 / 8, this.size, this.size / 4);
}
/**
* Encargado de construir el soporte del robot, mediante rectangulos
*/
private void support() {
graphics2D.setColor(new Color(93, 112, 112));
graphics2D.fillRect(this.point.x - this.size / 4, this.point.y + this.size * 15 / 4, this.size * 3 / 2,
this.size / 8);
graphics2D.fillRect(this.point.x, this.point.y + this.size * 29 / 8, this.size, this.size / 8);
graphics2D.fillRect(this.point.x - this.size / 4, this.point.y + this.size * 15 / 4, this.size / 8,
this.size / 2);
graphics2D.fillRect((int) (this.point.x + this.size * 9.1 / 8), this.point.y + this.size * 15 / 4, this.size / 8,
this.size / 2);
graphics2D.fillRect(this.point.x, this.point.y + this.size * 29 / 8, this.size / 8, this.size * 3 / 8);
graphics2D.fillRect((int) (this.point.x + this.size * 7.1 / 8), this.point.y + this.size * 29 / 8, this.size / 8,
this.size * 3 / 8);
}
/**
* Encargado de construir las ruedas que permiten el movimiento del robot,
* mediante circulos
*/
private void wheels() {
graphics2D.setColor(new Color(0, 0, 0));
graphics2D.fillOval((int) (this.point.x - this.size * 2.5 / 8), this.point.y + this.size * 17 / 4, this.size / 4,
this.size / 4);
graphics2D.fillOval((int) (this.point.x + this.size * 8.6 / 8), this.point.y + this.size * 17 / 4, this.size / 4,
this.size / 4);
graphics2D.fillOval((int) (this.point.x - this.size * 0.5 / 8), this.point.y + this.size * 4, this.size / 4,
this.size / 4);
graphics2D.fillOval((int) (this.point.x + this.size * 6.5 / 8), this.point.y + this.size * 4, this.size / 4,
this.size / 4);
}
/**
* Encargado de ensamblar todas las partes del robot, y mostrarlo como una
* unidad
*/
public void show() {
hat();
face();
ears();
eyes();
mouth();
column();
trunk();
shoulder();
arms();
hands();
belt();
support();
wheels();
}
}
And this is the JPanelWorkArea class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import javax.swing.JPanel;
public class JPanelWorkArea extends JPanel {
private static final long serialVersionUID = 1L;
private Figure figure;
private double centrox = 600;
private double centroy = 400;
private double rotate;
private Point point;
private int size;
/**
* Constructor que le asigna un valor al atributo figure
*
* @param figure
* Figura asignada
*/
public JPanelWorkArea(double rotate, Point p, int size) {
super();
this.point = p;
this.rotate = rotate;
this.size = size;
}
/**
* Metodo sobreescrito de la clase paint
*
* @param graphics
*/
@Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.transform(AffineTransform.getRotateInstance(Math.toRadians(rotate),this.centrox,this.centroy));
// this.repaint();
}
}