September 04, 2010, 02:42:25 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Loading an image based on user input  (Read 1432 times)
0 Members and 1 Guest are viewing this topic.
deAppel
Favorite member
Member
*

Reputation: 0
HappyFace plants one on you
Offline Offline
Posts: 45
Referrals: 0

Awards
« on: July 16, 2007, 08:51:52 AM »

Code
GeSHi (java):
  1. import java.awt.Container;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10.  
  11. /*--------------------------------------------------------------------------
  12. //Author List:
  13. // deAppel <Creator>
  14. //
  15. //Description:
  16. // My demo that displays an image using the commandline interface.
  17. //
  18. //Environment:
  19. // This software was developed using Eclipse and Java 1.6
  20. //
  21. //Copyright Information:
  22. // Copyright (C) 2007      <Institution><None>
  23. // Ark de Appel www.engineeringserver.com
  24. //
  25. //----------------------------------------------------------------------*/
  26.  
  27. public class ChooseImage  extends JFrame{
  28. static JLabel showImage;
  29. static File f;
  30.  
  31.  
  32. public void testImage(){ //show demo image, not needed now..
  33.  
  34. //Container c = getContentPane();
  35. showImage = new JLabel(new ImageIcon("C:\\42.gif"));
  36. add(showImage);
  37. setTitle("My Image number: DEMO");
  38. pack();
  39. setLocationRelativeTo(null);
  40. setVisible(true);
  41. }
  42.  
  43. public void showImageGui(int a){
  44. //Container c = getContentPane();
  45. String myString  = "c:\\" + Integer.toString(a)+".gif";
  46. showImage = new JLabel(new ImageIcon(myString));
  47. add(showImage);
  48. setTitle("My Image number: " + a);
  49. pack();
  50. setLocationRelativeTo(null);
  51. setVisible(true);
  52. toFront(); //show image to front.
  53. }
  54.  
  55. public static void main(String[] args){
  56. /* Not needed anymore
  57. * System.out.println("First version:");
  58. * MyString m = new MyString();
  59. * m.testImage();
  60. */
  61. //second version
  62. System.out.println("Second version:");
  63. try {
  64.  
  65. for(;;){
  66. System.out.print("# Enter image number: ");
  67.  
  68. try{
  69. String askNumber = br.readLine();
  70. //System.out.println("get: " + askNumber); Extra output not needed anymore
  71. int b = Integer.parseInt(askNumber);
  72. ChooseImage mm = new ChooseImage();
  73. String myString  = "c:\\" + Integer.toString(b)+".gif";
  74.  
  75. f = new File("" + myString); //Check file existence before continuing.
  76. if (f.exists()){
  77. System.out.println("# loading: " + myString + "\n");
  78. mm.showImageGui(b);
  79. }
  80.  
  81. else{
  82. System.out.println("# Image not found.\n");
  83. }
  84. }
  85. System.out.println("# Please use a number");
  86. }
  87. }//end infinite forloop
  88. }
  89. catch(IOException IOE){
  90. }
  91. }
  92. }
  93.  
  94.  
Created by GeSHI 1.0.7.20
Logged

Earn $0.04 or more for each search you make! http://www.myhpf.co.uk/apply001.asp?Friend=77889

If you need homework help, just post your assignment and i'll code it before your deadline =) i'll provide you "t3h c0d3z" but only if you show me that you have done something and not just ask for me to write everything for you. Just tell us what the problem is and i'm sure we can fix it for you!

I was born into the Hakka lineage, A bloodline that
traces it's roots back to the original Han emperors of
China. During the rise of the 3 Kingdoms (China's
famous warring period), the Hans were overthrown
and exiled. Forced to flee, they headed south finding
refuge within the mountains of Guilin Province where
they lived under their new title, the Hakka and patiently
wait for their chance to return to the throne as the
Sons Of Heaven...
Javaforums.net :: a community about Java software development.
« on: July 16, 2007, 08:51:52 AM »

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.355 seconds with 34 queries.

Google visited last this page August 25, 2010, 07:43:59 AM