1,105,332 Community Members

Beginner image opening program help

Member Avatar
jarograv
Light Poster
40 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I currently have this code:

import java.util.Random;
import java.io.*;
public class randomdwarf {
public static void main(String[]args)throws IOException { 
String dwarves[]=
	{"Dopey", "Sneezy", "Happy", "Sleepy", "Grumpy", "Bashful",	"Doc"};
	Random generator = new Random();
	int randomIndex = generator.nextInt( 7 );
	System.out.println(dwarves[randomIndex]);
	Runtime.getRuntime().exec("H:\\Profile\\Desktop\\dwarves\\Doc.jpeg");
}
}

It is a very simple code that is designed to get a random dwarf name then display the image. Note that currently I am just testing the program out and have it only open the picture of "Doc". I will implement the random image opening once I figure out how to open the image. After searching for a while I have not been able to find a solution. I am currently using windows and would appreciate any help you can give me.

Thanks,


Jaro

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

once I figure out how to open the image

For basic image handling I suggest the Toolkit class
here's a tutorial
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Runtime.exec isn't going to do what you need.
Assuming you are on Java 1.6 or 1.7 (which you should be) you can use the new Desktop class to open any arbitrary file in whatever application the OS associates with that file type (eg Image Viewer for jpegs).
This tutorial tells you everything you could possibly want to know (and a lot more)...
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

but the short version is:

Desktop d = Desktop.getDesktop();
File f = new File("H:\\Profile\\Desktop\\dwarves\\Doc.jpeg");
d.open(f);
Member Avatar
Philippe.Lahaie
Posting Whiz
376 posts since Oct 2007
Reputation Points: 42 [?]
Q&As Helped to Solve: 60 [?]
Skill Endorsements: 9 [?]
 
0
 

this is how i got my images the last time i had to do that, theres plenty of ways to do it, i think i remember early classes teaching to use ImageIcon instead of Images and setting the icon of the component instead of drawing an image, but whatever way you choose is fine as long as it works right?

//selection du bon personage a dessiner et de son nombre de membre a dessiner
Image img = null;
try {
    img = ImageIO.read(this.getClass().getResource("Images\\pendu"+character[i]+"-"+nbErreurs[i]+".png"));
} catch (IOException e) {
    e.printStackTrace();
}

//dessiner pendu
gPanel.drawImage(img, 50, 50, null);

gPanel here is a Graphics object that i created with :

gPanel = jpn_pendu[i].getGraphics(); //jpn_pendu is a JPanel array

(not writing this post because zero's way isn't good, just giving OP some options :))

EDIT : Just realized that OP wants to OPEN and image not draw one, ill leave this code just incase he changes is mind but ignore my post for your current problem! sorry :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: