0

To be clear, I've been researching for more than five hours now, I read all the related questions and more than 20 google searches, none of them worked for me and none of them described my case specifically.

First of all here's my code :

import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import static Debug.StaticVar.*;

/*
<applet code="ImageTest" width=300 height=100>
</applet>
*/

public class ImageTest extends Applet {
Image img;
MediaTracker tracker;

public void init() {
    tracker = new MediaTracker(this);
    Thread Loader = new Thread(() -> {
        img = getImage(getCodeBase(), "1.jpg");
        tracker.addImage(img, 1);
    });
    Loader.setPriority(10);
    Loader.start();
}

public void start() {
    try {
        tracker.waitForAll();
        repaint();
    } catch (InterruptedException e) {
    }
}

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
}
}

My Problem is the repaint method not calling paint method. To be more specific the paint method executes if I call repaint from another thread, or if I add the paint method to a child class and call repaint but it doesn't work in my code, where I directly call it from the applet main thread. Please HEEEEELP, I'm tired

MkAnis
  • 1
  • 2
  • 2
    So, straight of my head, you probably don't need the `MediaTracker` as the `Applet` can act as `ImageObserver`, if you pass `this` instead of `null` to `drawImage` - Applets are also a dead technology, they are no longer supported – MadProgrammer Jul 28 '17 at 08:04
  • @MadProgrammer I know they're dead technologies, and I'm just using them to learn cuz I'm copying the code from a book... but can u try it on your own and tell if it works for u... I can't see where's the problem with my code – MkAnis Jul 28 '17 at 08:40
  • 2
    I’d put a System.out statement after the waitAll and see if the image is actually getting loaded, if the fails, I’d look at using ImageIO while will actually generate an IOException if the image can’t be loaded – MadProgrammer Jul 28 '17 at 09:06
  • @MadProgrammer yes I diiid and it loads it successfully... and sometimes it works correctly but 9/10 of times it doesn't – MkAnis Jul 28 '17 at 09:39
  • When I comment out the 4 redundant imports (one of which prevented the code from compiling), and add `System.out.println("paint!");` to the paint method, the output is `paint!`. So the paint method is *definitely* being called. – Andrew Thompson Jul 28 '17 at 21:00
  • .. then when I comment out all the code related to the media tracker & replace `Image img;` with `Image img = new BufferedImage(40,40,BufferedImage.TYPE_INT_RGB);` I see an image painted in the top left. So a) The paint method is being called. b) The code will paint an image if it has one. c) I suspect @MadProgrammer is wise to suggest using `ImageIO` instead, as it provides better feed back when the image is not found. – Andrew Thompson Jul 28 '17 at 21:04
  • @AndrewThompson thank u for your heeeeelp, I'm learning java from "Java the Complete reference 9th edition" which's pretty old, it still uses applets and it didn't mention anything about BufferedImage I'l do some research... and I'll see how to solve it maybe the problem is Image... thanks – MkAnis Jul 29 '17 at 18:59
  • The `BufferedImage` is only relevant to my comments in that it is an easy way to create an image when one is needed, so it can be used for 'self contained' code. Because the code works with an image created at run-time, it suggests that the image is not being loaded correctly. That's why it is a good idea to change to using `ImageIO` as suggested by @MadProgrammer, when the image cannot be loaded, it will throw exceptions telling us what went wrong. – Andrew Thompson Jul 29 '17 at 20:14
  • @AndrewThompson really good advice thanks didn't that before, I'll continue using it from now on, USE BufferdImage for self-contained code and ImageIO for runtime image loading... okay – MkAnis Jul 29 '17 at 22:29

0 Answers0