Placing smaller images in a bigger java frame : why does this not work?


#1

I am writing this little program : I want to create sparks that are drawn in a smaller image, and then place them in a bigger image with a black background to make it look like a firework. So each spark is drawn in an image, which is then drawn onto the background, a frame.

My class sparks looks like this :

public class Etincelle extends Canvas{
//ATTRIBUTS
public Color couleur ;
public double duree, x,y ;
public String forme ;
public Image image ;
Graphics g ;  

//CONSTRUCTEUR
public Etincelle(double a, double b){
	x=a;
	y=b;
	forme = "croix";
	couleur=Color.red ; 
	image = createImage(12,12); 
	g = image.getGraphics();
}

//METHODES
public void peindreEtincelle(Graphics globalg) { 
	g.setColor(couleur);
	if (forme == "croix") {
		g.drawLine(6-5, 6, 6+5, 6);
		g.drawLine(6, 6-5, 6, 6+5);	
	}
	else if (forme == "point"){
		g.fillOval(6, 6, 8, 8);
	}
	else if (forme=="etoile"){
		g.drawLine(6-5, 6, 6+5, 6);
		g.drawLine(6, 6-5, 6, 6+5);	
		g.drawLine(6-5, 6-5, 6+5, 6+5);
		g.drawLine(6-5, 6+5, 6+5, 6-5);	
	}
	globalg.drawImage(image,(int)x,(int)y,null); // the image is drawn into the global graphic context, which will be the background later on in my program
}


//MAIN
public static void main(String[] args) {
	 }

}

g is the graphic context of the little image in which the spark is drawn, and globalg is the global context of the background where I want to place it.

However this doesn't work, when I test it with a simple black background it doesn't even open a window ! Could anyone maybe telle me what's wrong with this program ?


#2

@monabf,
And where is your CANVAS class definition ??


#3

@leonhard.wettengmx.n
As in it needs a paint() ? I tried not to make it extend Canvas which made it bug, if it doesn't extend Canvas I can't use the createImage function.. Does any class that extends canvas have to have a paint in it ? If so I just tried to replace peindreEtincelle by paint(Graphics g) and put globalg as one of the attributes of the class Etincelle. When I tested it in a test class (just creating a frame painting it black and drawing the spark on it), it opened a window painted in black but no spark...


#4

@monabf,
I am not an expert in this area....
but in such a case as yours
i would go on the lookout with google search
== discussions / opinions ==
java class extends canvas how to site:stackoverflow.com
and take it form there
reading the found articles.....at stackoverflow.com
for instance
http://stackoverflow.com/questions/34438142/cant-draw-image-in-canvas


#5

@leonhard.wettengmx.n
Thanks for the link, I looked at it !
I have been thinking about my code a lot and I finally found a solution!!! My class doesn't extend Canvas anymore, the function's name is paintEtincelle and instead of using createImage() (which only works if you extend Canvas) I create a BufferedImage and the corresponding graphics like this :

//CONSTRUCT0R
public Etincelle(double a, double b){
	x=a;
	y=b;
	forme = "croix";
	couleur=Color.red ; 
	image = new BufferedImage(12,12, BufferedImage.TYPE_INT_ARGB); 
	g = image.createGraphics();
}

//METHODES
public void peindreEtincelle(Graphics globalg) {
	g.setColor(couleur);
	//..... the content of my former code
	globalg.drawImage(image,(int)x,(int)y,null); /
}

It finally works!!!!! So thank you so much for making me realize the problem was extending Canvas :slightly_smiling: