We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,695 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Beginner image opening program help

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

4
Contributors
3
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
4
Views
jarograv
Light Poster
40 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 13

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);
JamesCherrill
... trying to help
Moderator
8,506 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29

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 :)

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0695 seconds using 2.76MB