Carré qui tourne

cs_miss2001 Messages postés 2 Date d'inscription lundi 6 mars 2006 Statut Membre Dernière intervention 7 mars 2006 - 6 mars 2006 à 13:17
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 7 mars 2006 à 14:41
Salut a tous !
Je suis sur un problème depuis quelques temps...
Alors je dois faire un carré qui fait une rotation en fonction d ou est la souris. Je vous mets la partie du code que j ai fais parce que là je rame complétement, si quelqu'un pouvait m'aider ça serait vraiment très gentil

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;

public class Caree extends JFrame implements MouseMotionListener{
private int xPrec, yPrec;
private JPanel p;
private Image img;
private Graphics imgGraph;


public Caree(){

super("Le rectangle magic");

p = new JPanel();
p.addMouseMotionListener(this);

img = new BufferedImage(600,600,BufferedImage.TYPE_INT_RGB);
imgGraph = img.getGraphics();

getContentPane().add(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(600,600);

setLocationRelativeTo(null);

setVisible(true);










}

public static void main(String[] args){

new Caree();
}

public void mouseDragged(MouseEvent e){

Graphics g = getGraphics();

g.setColor(Color.BLACK);
for(int i = 0;i < 100; i++){
g.fillRect(i,80,e.getX(),e.getX());


}
mouseMoved(e);
effacer();

}

public void mouseMoved(MouseEvent e){

xPrec = e.getX();
yPrec = e.getY();




}

public void effacer(){

Color c = imgGraph.getColor();
imgGraph.setColor(Color.WHITE);
imgGraph.fillRect(0,0,img.getWidth(this),img.getHeight(this));
imgGraph.setColor(c);


repaint();


}

public void paintComponent(Graphics g){
paintComponent(g);



g.drawImage(img,0,0,this);
}





}

3 réponses

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
6 mars 2006 à 17:47
Salut,



utilise Graphics2D et non Graphics par la meme occosion regarder du coté de l'objet java.awt.geom.AffineTransform.

Vas sur ce lien tu auras bien plus d'information



http://java.sun.com/docs/books/tutorial/2d/display/transforming.html



WORA
0
cs_miss2001 Messages postés 2 Date d'inscription lundi 6 mars 2006 Statut Membre Dernière intervention 7 mars 2006
7 mars 2006 à 13:11
jarrive pas a appliqué tu pourra pas me faire un exemple de caré ki tourne ?? mais pas sur une applet mais dans une JPanel ?!?!

merci bcp pour ton aide !!!!
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
7 mars 2006 à 14:41
Ralalalala,



bon j'ai repris leurs code en supprimant deux trois choses et en activant l'anti aliasing

de plus j'ai ajouté un thread pour qu'il tourne automatiquement, je ne
peux pas faire plus simple apres pour l'application avec le
mouseDragged c'est a toi de voir comment tu peux adapter la chose :)



go pour le code



import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.RenderingHints;

import java.awt.Shape;

import java.awt.geom.AffineTransform;



import javax.swing.JFrame;



public class Test extends JFrame {



private static final long serialVersionUID = 1802199483736051124L;



public Test() {

super("Test carre qui tourne");

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(new Dimension(600, 600));

setLocationRelativeTo(null);

setContentPane(new MyCarre());

}



public static void main(String[] args) {

new Test().setVisible(true);

}



private class MyCarre extends javax.swing.JPanel implements Runnable{

private static final long serialVersionUID = 2230612553024085175L;



private AffineTransform at = new AffineTransform();



private int w, h;



//mieux pour le dessin

private Shape shape;



private boolean firstTime = true;



//angle de rotation

private int angle = 0;



public MyCarre() {

setBackground(Color.white);

shape = new Rectangle(0, 0, 100, 100);

//animation

new Thread(this).start();

}



private void renderShape() {

//application de la rotation

at.rotate(Math.toRadians(angle));

repaint();

}



public void paintComponent(Graphics g) {

super.paintComponent(g);



Graphics2D g2 = (Graphics2D) g;



//activation de l'anti aliasing pour eviter les escaliers

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,


RenderingHints.VALUE_ANTIALIAS_ON);



Dimension d = getSize();

w = d.width;

h = d.height;

// initialisation du transform

if (firstTime) {

at.setToIdentity();

at.translate(w / 2, h / 2);

firstTime = false;

}

Rectangle r = shape.getBounds();





AffineTransform saveXform = g2.getTransform();

AffineTransform toCenterAt = new AffineTransform();

//reajustement de la shape

toCenterAt.concatenate(at);

toCenterAt.translate(-(r.width / 2), -(r.height / 2));

g2.transform(toCenterAt);

g2.fill(shape);

g2.setTransform(saveXform);

}



public void run() {

while(true){

try {


Thread.sleep(500);


if(angle < 180) angle+=5;


else angle = 0;


renderShape();

} catch (Exception e) {


e.printStackTrace();

}

}



}

}

}




WORA
0
Rejoignez-nous