Java Toolkit.getDefaultToolkit().getImage
I can no longer display the comments from the "help" on my application (I've already described my problem and I thought I had received a solution from a chat bot, but it still doesn’t work).
What I’m doing now, which is more precise than before:
Image image_screen = createImage(1140,800); System.out.println(" image_screen* " + image_screen);
gScreenBuffer = image_screen.getGraphics();
name = javadir + "appli_puits_puits_de_potentiel.jpg"; //name of the comment and location javadir on my disk, verified N times
image_screen = Toolkit.getDefaultToolkit().getImage(name);
System.out.println(" image_screen *** " + image_screen);
Tracker tracker = new MediaTracker(this); tracker.addImage(image_screen, 0);
try { tracker.waitForID(0); }
catch (InterruptedException e) {
System.out.println(" image2 not arrived?");
}
int imagew = image_screen.getWidth(this);
int imageh = image_screen.getHeight(this);
System.out.println(" ****imagew " + imagew + " imageh " + imageh);
Result: imagew and imageh are -1 both, instead of 1140 and 800
and the address of image_screen is sun.awt.image.ToolkitImage@233c0b17 instead of BufferedImage@2ac1fdc4 before the call to the toolkit.
and obviously I don’t have the desired comment on the gScreenBuffer (coupled with image_screen). In fact, the tracker does not seem to be at fault. What seems to be wrong, is (it seems to me) the difference in address of the image before and after the call to the toolkit. A bit like if I asked for a screwdriver in the toolbox (the toolkit) and was given a hammer.
I’ve spun this problem in every way, but I always end up at the same conclusion.
Thanks in advance.
1 answer
-
Hello, I restarted from the initial discussion (link) to understand a bit better the goal of the program. Without having all my questions answered yet --', I have already identified 2 problems in the code: - image_ecran was created as a 1140x800 buffer and then replaced by the value of Toolkit.getImage(nom) - gTampon_ecran remained associated with the old image and drew into the old buffer instead of into the new image image_ecran - The Width/Height values at -1 came from KO loading by Toolkit The changes made: - Toolkit.getImage becomes ImageIO.read but loads the JPG into a BufferedImage - image loaded into a clearer variable image_depuis_nom - review of the write process and buffer management - MediaTracker I just preferred to skip for simplification - as a bonus because we are here for best practices, File for file controls - I left the variables in snake_case but camelCase is preferable - I commented a minimum to clarify, that too is good practice - code tagging as well, reading is nicer. You had done it well in your previous post --'
//name of the comment and location javadir on my disk, checked N times File fichier = new File(javadir, "appli_puits_puits_de_potentiel.jpg"); /* * fichier JPG * | * ImageIO.read() * \|/ * gTampon_ecran * image_depuis_nom * BufferedImage * / * | * drawImage() * / * \|/ * |/_ * +--------------------------+ * | image_ecran 1140 x 800 | * +--------------------------+ */ try { System.out.println(" ****Ouverture du fichier : " + fichier.getAbsolutePath()); System.out.println(" ****Fichier existe : " + fichier.exists()); // Utilisation d'un BufferedImage depuis ImageIO BufferedImage image_depuis_nom = ImageIO.read(new File(nom)); if (image_depuis_nom == null) System.out.println(" ****Fichier trouvé, image KO"); else { int imagew=image_depuis_nom.getWidth(); int imageh=image_depuis_nom.getHeight(); System.out.println(" ****imagew : " + imagew + ", imageh : " + imageh); // Remplacement du tampon image_ecran. // Le considère déjà défini ailleurs et surcharge la valeur. // Si pas déjà défini, définir. Mettre BufferedImage au début de la ligne. image_ecran = new BufferedImage(1140, 800, BufferedImage.TYPE_INT_RGB); // Graphics associé au tampon gTampon_ecran = image_ecran.getGraphics(); // Dessiner image_depuis_nom dans le tampon gTampon_ecran.drawImage(image_depuis_nom, 0, 0, null); } } catch (IOException e) { System.out.println(" ****image2 pas arrivee? cause : " + e.getMessage()); }Tiens au jus ;-) Les MAMAA n'ont pas de pétrole mais ils ont des données ! Tu la sens ma Big Data ? Sacrifie quelques libertés pour plus de sécurité et tu les perds TOUTES. ALL YOUR DATABASE ARE BELONG TO US