I am working on a java code and my image is not shown in the final run. I put it in an entity and I also handed the entity over to the entity manager. Eclipse is not showing me any problem, but when I go to run I can not see my image only the background I did wrote before.
Any suggestions on how to fix it?
The following is the part for my code:
private void gorilla() {
Entity gorilla1 = new Entity("gorilla1");
gorilla1.setPosition(new Vector2f(590, 190));
try {
gorilla1.addComponent(new ImageRenderComponent(new Image("assets/gorillas.gorillas/gorilla_right.png")));
} catch (SlickException e) {
System.err.println("Cannot find file gorilla_right.png!");
e.printStackTrace();
}
entityManager.addEntity(stateID, gorilla1);
}
Related
The requirement is to create a desktop notification which can register a click-event. I cannot use web-sockets or any browser notifications.
I am unable to use Tray-Icons and SystemTray because they cannot register Click-Events on DISPLAY MESSAGE. They can have click-events on the trayicon but not on the display message. The closest example - "When we register a click on a Skype message, it opens Skype for us"
Screenshot
On clicking the above Notification Skype chat opens-up. The same functionality is not supported with Tray-Icons. Either a work around it or a new approach will be do.
Hope I am clear thanks.
I used the following repository from github DorkBox.
Simply add maven dependency as instructed on the github link. However, I was unable to check how to change the UI for the notifications.
Notify.create()
.title(text)
.text(title)
.position(Pos.TOP_RIGHT)
.onAction( new ActionHandler<Notify>() {
#Override
public void handle(Notify value) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
try {
Desktop.getDesktop().browse(new URI(targetUrl));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
})
.hideAfter(5000)
.shake(250, 5)
.darkStyle() // There are two default themes darkStyle() and default.
.showConfirm(); // You can use warnings and error as well.
Add the following code in your main block and you are good to go.
I wrote a program in which a pdf file should be opened on an Action Event (you can have a look at my code below).
menuElementHilfe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
File hilfe = new File ("src\\resources\\Hilfe.pdf");
try {
java.awt.Desktop.getDesktop().open(hilfe);
} catch (IOException e) {
e.printStackTrace();
}
}
});
If I execute the program via Eclipse everything works, but after exporting as a runnable jar I get following Exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: src\resources\Hilfe.pdf doesn't exist.
Any Feedback is appreciated
The way you're retrieving resources may be the problem. try this :
menuElementHilfe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
File hilfe = new File(getClass().getResource("/resources/Hilfe.pdf").getFile());
try {
java.awt.Desktop.getDesktop().open(hilfe);
} catch (IOException e) {
e.printStackTrace();
}
}
});
When running in Eclipse, you are targeting a file in your build path.
When running from JAR/WAR, the URL is different and look like "jar:file:/your-path/your-jar.jar!/Hilfe.pdf" which is not what you set when calling new File(...) So to get the right URL for internal resources, you have to use methods like getResource or getResourceAsStream depending on your needs.
Check out following explanations for more information :)
https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html
[EDIT]
I assume you're working on some Swing app, but I dont know if you're aware that doing some task like that in your AWT-EventQueue thread will freeze your UI.
To prevent that you have to run UI-unrelated stuff in another thread.
This is made using SwingUtilities.invokeLater (Java 5 and prior) method and/or the SwingWorker class (since Java 6).
as mentionned in this answer
You should put the previous solution in something like that :
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Your UI unrelated code here
}
});
The resource can be packed in the application jar, hence File (physical disk file)
is not possible. Copy it to a temporary file, so that the desktop can open it.
menuElementHilfe.addActionListener(evt -> {
Path tmp = Files.createTempFile("hilfe-", ".pdf");
Files.copy(getClass().getResourceAsStream("/Hilfe.pdf"), tmp);
try {
Desktop.getDesktop().open(tmp.toFile());
tmp.toFile().deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
});
An other difference is the forward slash, and that the path is case-sensitive, opposed to Windows File.
After problems
menuElementHilfe.addActionListener(evt ->
SwingUtilities.invokeLater(() -> {
Path tmp = Files.createTempFile("hilfe-", ".pdf");
Logger.getLogger(getClass().getName()).log(Level.INFO, "actionPerformed "
+ tmp + "; event: " + evt);
Files.copy(getClass().getResourceAsStream("/resources/Hilfe.pdf"), tmp);
try {
Desktop.getDesktop().open(tmp.toFile());
//tmp.toFile().deleteOnExit();
} catch (IOException e) {
Logger.getLogger(getClass().getName()).log(Level.WARN, "Error with " + tmp,
e);
}
}));
I did not delete, so the Desktop access can live longer than the java app.
I did an invokeLater in order to have no frozen GUI on the actionPerformed.
I added logging to see every call to actionPerformed
I'm making java application with javaFx ui in kind of tiny machine. I wanted to show loading page, and load data files during the progress indicator going on.
I know it could work well if I added Platform.runlater on the code loading fxml file and controller, but it's weird for me to use Platform.runlater on javaFx main app thread. I checked threads' name but they were same. Also it works if it run separately using annotation.
Why do i need to use Platform.runlater ?
If I don't add that, loading image turns white screen and skip image, and just show menu view.
//Process
//1. Set loading page image
//2. Load data files
//3. Load next page(menu)
public void loadHomeMenuPage() {
setLoadingImage();
execLoadingData();
execLoadingView(this);
}
private void setLoadingImage() {
System.out.println("Load -> " + Thread.currentThread());
File file = new File("Resources/images/load.png");
InputStream is;
try
{
is = new FileInputStream(file);
this.logoImageView.setImage(new Image(is));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
private void execLoadingData() {
// load openCv files
new LoadOpenCV();
// load protocol files
new ProtocolLoader().load();
// load language pack here
}
private void execLoadingView(IController loadController) {
//Load homeMenu after loading all data
//Platform.runLater(() -> {
System.out.println(Thread.currentThread());
IController controller = (IController) FxmlUtils.LOAD
.fxmlPath(PathFxml.ABS_PATH_HOME_MENU_VIEW)
.pane("BorderPane")
.set2BaseBorderPane(this.baseBorderPane, "center")
.exec();
controller.setBaseBorderPane(this.baseBorderPane);
//});
}
I am using external images in my webaplication, everything wass fine until I wanted to add animated gif there, the gif loads, but it doesn't animate.
Java code:
File sourceimage = new File("loading_img.gif");
try {
final BufferedDynamicImageResource r = new BufferedDynamicImageResource("GIF");
r.setImage(ImageIO.read(sourceimage));
add(new Image("gif", r));
} catch (Exception e) {
e.printStackTrace();
}
HTML:
<img wicket:id="gif"/>
EDIT:
Tried martin-g suggestion, gif still doesn't animate
try {
final BufferedDynamicImageResource r = new BufferedDynamicImageResource("GIF"){
#Override
protected void setResponseHeaders(AbstractResource.ResourceResponse data,
IResource.Attributes attributes){
super.setResponseHeaders(data, attributes);
data.setContentType("image/gif");
}
};
r.setImage(ImageIO.read(sourceimage));
add(new Image("gif", r));
} catch (Exception e) {
e.printStackTrace();
}
The problem is that the content type is not automatically set.
You will need to override org.apache.wicket.request.resource.AbstractResource#setResponseHeaders() and set with via resourceResponse.setContentType(String).
Maybe this should be done automatically by Wicket in org.apache.wicket.request.resource.DynamicImageResource, since it knows the format ("png", or "gif" as in your case). Please file a ticket in Wicket's JIRA for this improvement! Thanks!
I'm getting a MalformedURLException some code in my Android Studio project. My aim is to get and display an image from a web page, and the URL seems to be fine, but it's giving me this error.
I have already put the code in a try,catch, but that is still giving me the error.
Here is the code to grab the image and display it:
try
{
url = new URL(items.get(position).getLink());
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch (IOException e)
{
throw new RuntimeException("Url Exception" + e);
}
holder.itemTitle.setText(items.get(position).getTitle());;
holder.itemHolder.setBackgroundDrawable(new BitmapDrawable(bmp));
items.get(position).getLink() is meant to get the link that is being displayed in a ListView, but even something like URL url = new URL("https://www.google.com") doesn't work.
Thanks.
your url is formatted without the protocol at the beginning try
url = new URL("http://"+items.get(position).getLink());
sometimes the url string may have special characters, in which case you need to encode it properly please see this.
And also, the url you posted in the comment is not an image.
it is exception beacuse of url class
add antoher catch like
catch (MalformedURLException e) {
e.printStackTrace();
}
Just click alt+enter and then import try catch section... this helped me...