I am trying to do the simple activity of setting the Application Icon in a java application. I have many working examples with me but in this instance it fails. Please help I have tried a) b) c) marked in the code. a) Gives a error hint 'Non-static getClass cannot be referenced from a static context' So I tried b) and c). In both, the program runs, but NO Icon is set, NO errors. (I have put the same image in different paths for test purpose)
private static void createAndShowGUI() {
myFrame = new MyDynamic();
myFrame.setTitle( "Sunsong Public School : Home" );
a)myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("../Images/Sudan.png")));
b)myFrame.setIconImage(ImageIO.read(new File("../Images/Sudan.png")));
c)Image icoon = Toolkit.getDefaultToolkit().getImage("Sudan.png"
Got the solution from some other source. The funny thing is I have dozens of working Applications with Icon. In all of them I am using the Toolkit-getclass-getresources. But in this case I was tinkering with an old code, and stuck inside a static block. From the static context Calling toolkits non-static getClass is NOT working. Had tried hundreds of examples from the net in the last 20 or so hours. Java IO also NOT working complains cannot 'read from file'.
This is the one Finally worked..
ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("Sudan.png"));
Image img = icon.getImage();
myFrame.setIconImage(img); // frame is a JFrame
Related
I have been trying to automatize some tasks on my computer and did choose Sikuli from Java to do so (I work with Java everyday and didn't know any automation tool using java, sikuli was the first I found). I use java with maven and eclipse as IDE. I have added Sikuli as a Maven dependency.
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>2.0.5</version>
</dependency>
I tried to do some simple stuff. I did a few screenshots of parts of my screen using windows' screenshot tool, and wanted sikuli to hover it. It works quite fine for one image, but not at all for the others. It seems that the bigger the image the better it works as I did not have success for any small images. The one working is a screen of a whole window (reduced to ~1/4 of my screen).
I also tried to find a button inside this window, to find the windows logo on bottom left, to find a screen of my package explorer, but none work correctly.
I played with similar() using various values, but it didn't improve the results. In some cases (button inside the window) it did find a result for some low similar value, but it was another button. The weird part is : its finding this other button which is bright blue, while the one i'm looking for is purple.
My pc background never changes, I did some screen.highlight() and its looking at the correct screen (dual screen). It's not an issue with the path to images (already solved this one).
Do you have any idea of what I could try ? I have read about people having different success rate depending on whether they were using Sikuli IDE or another IDE. So maybe I could give sikuli IDE a try.
I can give code samples as soon as I am back home.
The code I'm using to test :
public class CleanTest {
static Screen screen = new Screen();
public static void main(String[] args) throws FindFailed, AWTException, IOException, InterruptedException {
String pathYourSystem = System.getProperty("user.dir") + "\\";
System.out.println(pathYourSystem);
Pattern pLauncher = new Pattern(pathYourSystem+"img\\full_launcher.PNG").similar(0.9d);
Desktop.getDesktop().open(new File("path_to_an_exe_opening_a_launcher"));
screen.wait(pLauncher, 300);
screen.mouseMove();
System.out.println("launcher found");
}
}
It works with the "full launcher" image, but it doesn't find a sub-part of the launcher (a button). I tried to make some code to test if there was some threshold for the similar parameter :
double similarValue = 1d;
Pattern pLauncher = new Pattern(pathYourSystem+"img\\the_button.PNG").similar(similarValue);
Desktop.getDesktop().open(new File("path_to_an_exe_opening_a_launcher"));
while(!screen.has(pLauncher)) {
similarValue-=0.1;
pLauncher = new Pattern(pathYourSystem+"img\\login.PNG").similar(similarValue);
}
System.out.println(similarValue);
screen.mouseMove();
it finds something at around 0.5, but it's a totally different button.
Thank you !
EDIT: if someone has the same issue, try to use sikulix IDE to take the screenshots. It works with the screenshots taken by the IDE.
This is a simple test, that completely stays within the SikuliX features.
import org.sikuli.basics.Debug;
import org.sikuli.script.*;
public class SikulixTest {
public static void main(String[] args) {
System.out.println("SikulixTest");
Screen scr = new Screen();
// craete an image to be searched on the screen
Image img = new Image(scr.userCapture());
// try to find it
Match mImg = scr.exists(img);
if (mImg != null) {
// show sthg. when found
Debug.info("%s", mImg);
mImg.highlight(2);
}
}
}
This is RaiMan from SikuliX
I'm writing a game over libgdx; I'm using the junit framework to simplify unit-testing my code.
Now there's part of the code (a map generator, a class converting my own map format into TiledMap...) which I need to test thoroughly, but it uses libgdx code: from file handling to asset loading.
I'm not planning to test the actual graphical output, or the game itself, in this way: but I want to test the single components (calculation, asset access...) to avoid blatant errors.
I've tried to do something like this in the "setUpBeforeClass" method:
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.useGL20 = true;
cfg.width = 480;
cfg.height = 320;
cfg.resizable = true;
LwjglApplication app = new LwjglApplication( new TestApplicationListener(), cfg);
And calling within tearDownAfterClass():
Gfx.app.exit()
But it does create a window I do not need, and seems overkill when all I need is the file handling initialized. Is there a better way to initialize the libGDX components without creating an entire application object?
Thanks.
EDIT
Going back over it (thanks to Sam in the comments), I realize GL access is needed (loading assets requires it), but this approach does not seem to work: the graphic library does not seem to be initialized. GDX documentation hasn't helped. Any clue?
This question hasn't been answered and I am surprised nobody has pointed out the headless backend, which is ideal for this situation. Combine this with your favorite mocking library and you should be good to go.
public class HeadlessLauncher {
public static void main(final String[] args) {
final HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
config.renderInterval = Globals.TICK_RATE; // Likely want 1f/60 for 60 fps
new HeadlessApplication(new MyApplication(), config);
}
}
As already showed there is a HeadlessApplication backend which gives you an initialized libGDX but has no OpenGL context. For working with OpenGL you indeed need the LwjglApplication backend which creates an OpenGL window.
If you have problems writing tests which rely on the OpenGL context keep in mind that OpenGL is only attached to the thread of your LwjglApplication which is not the tread of your tests. Your tests have to call Gdx.app.postRunnable(Runnable r) to access the thread with the OpenGl context.
You may want to use synchronized and CountDownLatch to pause the test while waiting for your application to execute the command.
I'm trying to get the sketchytruck example to work in Java. Its found here. Before anyone gives me the "you didn't look hard enough", I have. I tried this and I followed the directions doing everything but now I'm getting an error that says Selection does not contain a main type. I created a main type
package se.treplex.sketchytruck;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import se.treplex.sketchytruck.GameActivity;
public class SketchyDesktop {
public static void main(String[] args) {
new LwjglApplication(new GameActivity(), "SketchyTruck", 320, 480, false);
}
}
Then it says LwjglApplication cannot be resolved to a type. So I tried fix project setup which didn't do anything. I notice GameActivity extends LayoutGameActivity and I spent about 3 hours googling for andengine source that runs as a java application with no luck. So my question is what main do I use to get this working as a Java Application? If there isn't one then how do you get this to work?
AndEngine has a few (minor) Android dependencies. (Logging, Activity subclasses, GLSurfaceView subclass, ...). Without changes it will ONLY run on Android (And BlackBerry 10).
public class Intro extends JFrame implements ActionListener {
ImageIcon pic = new ImageIcon(this.getClass().getResource("cars-games.jpg"));
JLabel l1 = new JLabel();
Image car = pic.getImage();
public static void main (String[]args){
Intro i = new Intro();
i.show();
}
}
Its giving me this error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Intro.<init>(Intro.java:15)
at Intro.main(Intro.java:58)
Can anyone help plz.
Your resource is null, and ImageIcons cannot be constructed with null parameters.
Make sure you've entered the correct path to "cars-games.jpg".
I think it's not able to read your image file and hence the issue.
Try using classLoader as :
ImageIcon pic = new ImageIcon(getClass().getClassLoader()
.getResource("cars-games.jpg"));
If still you get the same issue then make sure that cars-games.jpg is available in root of your class loader location.
I was getting this issue a lot at the beginning of development for my java game project for this semester. This generally means that the resource that you are trying to access is not able to be found (i.e. Nullpointerexception). What I did to make everything much easier was to just make a separate folder in your java project called images (especially if you are using multiple images in this project). Then you can just call new ImageIcon with your directory. Makes things a lot easier in the end.
As stated earlier, getClassLoader() works as well!
I want to change the icon of the project instead of java icon. When the program icon is being displayed in status bar, it should be displaying the customized icon instead of default java icon.
I am using the following code. Please suggest me what's wrong in this code.
class newframe extends JFrame
{
Container cp;
newframe()
{
cp=this.getContentPane();
cp.setLayout(null);
}
public static void main(String args[])
{
newframe frm= new newframe();
frm.setbounds(0,0,1000,800);
frm.setVisible(true);
ImageIcon im1= new ImageIcon("path upto image");
frm.setIconImage(im1.getImage());
}
}
..new ImageIcon("path upto image");
A frame icon will typically be an embedded-resource, so must be accessed by URL rather than (a String representing a path to) a File.
There are a couple of things that would be keeping it from compiling. First:
frm.setbounds(0,0,1000,800);
Your "setbounds" should have a capital B. Typically, functions will be cased such that the first letter of the first word is lowercased, and subsequent words are upper-cased. See this link for the doc on setBounds: setBounds
There's a second issue in your ImageIcon path. Its hard to say if that came right from your code or if you removed the path for the sake of the example, but Andrew Thompson has addressed that adequately.
I think the problem is the decleration of the imageicon. What you should do is instead of getting the direct path, do something like this:
ImageIcon im1= new ImageIcon("Toolkit.getDefaultToolkit().
getImage(getClass().getResource("path upto image"))");
I do this with all of my applications, and it works every time.