Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with rotation in java graphics

hawxhawx Posts: 1Member
hi all

I'm trying to make a tangram but I failed to make the rotation of the figures, I have accomplished is to make the translation of the figures and so far I've tried but have not succeeded

if anyone can help me with rotation or appreciate it rotate one example where figures mouse events or keyboard

thanks


[code]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class Figura extends JPanel {

private Polygon[] polygons = {};
private Polygon selected = null;
private Point last = null;
private float rot;

public Figura() {

rot = (float)Math.PI/2;
polygons = new Polygon[3];
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
onMouseDown(e);
Graphics2D g = (Graphics2D)getGraphics();
int x = e.getX(); // x-coordinate where user clicked.
int y = e.getY();
if(e.isMetaDown()){
polygons[0].contains(x, y);
g.rotate(5);
//g.setColor(Color.blue);
//g.fillOval( x - 25, y - 15, 60, 30 );
//g.setColor(Color.black);
//g.drawOval( x - 25, y - 15, 60, 30 );

}
}
public void mouseReleased(MouseEvent e) {
onMouseUp(e);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
onMouseDragged(e);
}
});
}

protected void onMouseDown(MouseEvent e) {
last = e.getPoint();
for (int i = 0; i < polygons.length; i++) {
if (polygons[i].contains(last)) {
selected = polygons[i];
return;
}
}
selected = null;
}

protected void onMouseDragged(MouseEvent e) {
Point now = e.getPoint();
if (last != null) {
int xtrans = now.x - last.x;
int ytrans = now.y - last.y;
if (selected != null) {
selected.translate(xtrans, ytrans);
repaint();
}
last = now;
}
}

protected void onMouseUp(MouseEvent e) {
last = null;
selected = null;
repaint();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);

for (int i = 0; i < polygons.length; i++) {
g2.drawPolygon(polygons[i]);
//g2.rotate(rot, this.getWidth()/2, this.getHeight()/2);
}
}

public Polygon[] getPolygons() {
return polygons;
}

public void setPolygons(Polygon[] polygons) {
this.polygons = polygons;
}


public void triangulo(){
int xVals[] = { 108, 80,135};
int yVals[] = { 340, 380, 380 };
//Polygon[] polys = new Polygon[1];
//for (int i = 0; i < polygons.length; i++) {
polygons[0] = new Polygon(xVals, yVals, 3);

//}

}

public void cuadrado(){
int polx[] = {208, 208,258,258,258};
int poly[] = {340, 390, 340, 390,390};
//Polygon[] polys = new Polygon[1];
// for (int i = 0; i < polygons.length; i++) {
polygons[1] = new Polygon(polx, poly, 4);
// }

}


public void paralelogramo(){

int polx[]={330, 380 , 480, 430};
int poly[]={100, 280, 280 ,330};
polygons[2]= new Polygon(polx, poly, 4);
}



public static void main(String[] args) {
Figura panel = new Figura();

panel.triangulo();
panel.cuadrado();
panel.paralelogramo();
panel.setPolygons(panel.polygons);


JPanel panelImagenes = new JPanel();
TitledBorder border = BorderFactory.createTitledBorder( "Resultados" );
panelImagenes.setLayout(new BorderLayout());
panel.add(panelImagenes);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500);
frame.setContentPane(panel);
frame.show();
}

[/code]
Sign In or Register to comment.