Is storing Graphics objects a good idea?

No, storing a Graphics object is usually a bad idea. 🙂 Here’s why: Normally, Graphics instances are short-lived and is used to paint or draw onto some kind of surface (typically a (J)Component or a BufferedImage). It holds the state of these drawing operations, like colors, stroke, scale, rotation etc. However, it does not hold … Read more

What does SwingUtilities.invokeLater do? [duplicate]

As other answers have said, it executes your Runnable on the AWT event-dispatching thread. But why would you want to do that? Because the Swing data structures aren’t thread-safe, so to provide programmers with an easily-achievable way of preventing concurrent access to them, the Swing designers laid down the rule that all code that accesses … Read more

How to simulate a real mouse click using java?

Well I had the same exact requirement, and Robot class is perfectly fine for me. It works on windows 7 and XP (tried java 6 & 7). public static void click(int x, int y) throws AWTException{ Robot bot = new Robot(); bot.mouseMove(x, y); bot.mousePress(InputEvent.BUTTON1_DOWN_MASK); bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); } May be you could share the name of the … Read more

Java Event-Dispatching Thread explanation

The event dispatch thread is a special thread that is managed by AWT. Basically, it is a thread that runs in an infinite loop, processing events. The java.awt.EventQueue.invokeLater and javax.swing.SwingUtilities.invokeLater methods are a way to provide code that will run on the event queue. Writing a UI framework that is safe in a multithreading environment … Read more

Loading resources like images while running project distributed as JAR archive

First of all, change this line : image = ImageIO.read(getClass().getClassLoader().getResource(“resources/icon.gif”)); to this : image = ImageIO.read(getClass().getResource(“/resources/icon.gif”)); More info, on as to where lies the difference between the two approaches, can be found on this thread – Different ways of loading a Resource For Eclipse: How to add Images to your Resource Folder in the Project … Read more

tech