Dessiner dans un JPanel apartir d'une autre class
Bonjour tous le monde je suis un débutant dans le dessin Graphics en Java
je n'arrive pas a dessiner une ligne ou plusieur dans une autre classe pas celle qui extends le JPanel
je vous donne la classe qui extends le JPanel :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package environnement;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Dessin extends JPanel {
private Image drawingImage;
private ImageIcon image;
private Graphics g;
private Graphics2D g2;
public int x,y,w,z;
public Dessin(int x,int y,int w,int z){
this.x=x;
this.y=y;
this.w=w;
this.z=z;
repaint();
}
public Dessin() {
x=1;y=1;z=100;w=300;
repaint();
}
void initialiser(){
// super.paintComponent(g);
Dimension dim=getSize();
drawingImage = this.createImage(dim.width,dim.height);
g = super.getGraphics();
super.setSize(new Dimension(800,700));
super.setBackground(Color.white);
super.setPreferredSize(new Dimension(600,600));
Graphics2D g2 = (Graphics2D)g;
// g2.drawLine(x+2,y+3,z+70,w+10);
}
public Graphics getImageGraphics(){
if (super.getGraphics() == null)
initialiser();
return super.getGraphics();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
initialiser();
image=new ImageIcon("turtleLeft.jpg");
image.paintIcon(this, g, 280,280);
g2 = (Graphics2D)g;
// g2.drawLine(x-x, y-y, w, z);
}
public void update() {
Graphics gg=this.getGraphics();
this.paintComponent(gg) ;
}
} |
//_______________________________________________
et la classe ou je faire dessiner mes drawline :
j'ai essayer plusieur methode (maniere) mais aucune ne marche j'ai mis chaque methode en commentaire
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package commandes;
import environnement.Dessin;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
/**
*
* @author brahim
*/
public class Reculer extends Dessin{
Dessin dessin = new Dessin(400, 0, 0, 400);
private Graphics gdessin;
//private final Graphics2D g2dessin;
public Reculer() {
// Premiere maniere mais elle ne marche pas
// super(400, 0, 0, 400);
// deuxieme maniere mais elle ne marche pas
// gdessin = dessin.getImageGraphics();
// g2dessin = (Graphics2D)gdessin;
// gdessin.drawLine(400, 0, 0, 400);
// Troisieme maniere mais elle ne fonctionne pas
// Dessin dessin = new Dessin(400, 0, 0, 400);
}
} |
je sais qu'il y a un truc qui m'échappe dans l'histoire mais je sais pas lequel
merci les gens .