Skip to main content

Image Viewer - Netbeans Java Codes

Run-Shot of Image Viewer displaying sumikuma logo

Java Code:

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import static javax.swing.JOptionPane.INFORMATION_MESSAGE;
import static javax.swing.JOptionPane.WARNING_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

/**
 *
 * [Find more Netbeans Java Code at www.sumikuma.tk]
 */
public class ImageViewer implements ActionListener {
 private BufferedImage image;
 private JFrame frame;
 private String fileName;
 private JMenuItem menuItem[] = new JMenuItem[2];
 private JLabel area = new JLabel();
 private JScrollPane JSp = new JScrollPane(area,
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 private JFileChooser dialog = new JFileChooser();

 public ImageViewer() {
  if (frame == null) {
   frame = new JFrame();
   JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("File");
            frame.add(menuBar,BorderLayout.NORTH);
            frame.add(JSp);
            menuBar.add(menu);
            menuItem[0] = new JMenuItem("Open...");
            menuItem[1] = new JMenuItem("Exit");
            menuItem[0].addActionListener(this);
            menuItem[1].addActionListener(this);
            menuItem[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
                   Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            menuItem[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
                                     Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            menu.add(menuItem[0]);
            menu.add(menuItem[1]);
            frame.setJMenuBar(menuBar);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setTitle(fileName);
            frame.setResizable(false);
            frame.pack();
            frame.setVisible(true);
  }
  frame.repaint();
 }
 public void readInFile(String fileName) {
  this.fileName = fileName;
  File file = new File(fileName);
  if(file.isFile()) {
            try {
    image = ImageIO.read(file);
   } catch (IOException e) {
    showMessageDialog(frame,"Does not compute !","No file read or found",INFORMATION_MESSAGE);
    e.printStackTrace();
   }
        }
  else {
            URL url = getClass().getResource(fileName);
            if (url == null) { 
             try {
     url = new URL(fileName);
    } catch (MalformedURLException e) {
     showMessageDialog(frame,"Does not compute !","No Image file or found",INFORMATION_MESSAGE);
     e.printStackTrace();
    } 
            }
            try {
    image = ImageIO.read(url);
   } catch (IOException e) {
    showMessageDialog(frame,"Does not compute !","No Image file",WARNING_MESSAGE);
    e.printStackTrace();
   }
       }
}
 public void setImage(JLabel area){
  ImageIcon icon = new ImageIcon(image);
  area.setIcon(icon);
  frame.setSize(icon.getIconWidth(),icon.getIconHeight());
 }
 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==menuItem[0]) {
   if(dialog.showOpenDialog(null)==(JFileChooser.APPROVE_OPTION)) {
     readInFile(dialog.getSelectedFile().getAbsolutePath());
    if(image==null) {
     showMessageDialog(frame,"Does not compute !","No Image file",INFORMATION_MESSAGE);
    }
    else {
     setImage(area);
    }
   }
  }
  else if(e.getSource()==menuItem[1]) {
   System.exit(0);
  }
 }
 public static void main(String[] arg) {
  new ImageViewer();
 }
}

You can also download text file version of this code: Image_Viewer.txt

Comments

Popular posts from this blog

Javax Code for Playing Music:

Javax Code for Playing Music: OUTPUT: ________________________________________________________________ Just Copy and Paste and Change the variable name respectively: ________________________________________________________________ ________________________________________________________________ Buttons: 1. jButton1 : Open Music File 2. jButton2 : Play 3, jButton3 : Stop 4. jButton4 : Pause ________________________________________________________________ import java.io.File;     import java.io.IOException;     import java.net. MalformedURLException;     import javax.sound.sampled. AudioInputStream;     import javax.sound.sampled. AudioSystem;     import javax.sound.sampled.Clip;     import javax.sound.sampled. LineUnavailableException;     import javax.sound.sampled. UnsupportedAudioFileException; public class Music extends javax.swing.JFrame {     /** ...

Get Current System Time & Date - Netbeans Java Code

Run Shot of Current System Time & Date Java Code: import java.text.SimpleDateFormat; import java.util.Date; public class Datetimemonth { public static void main(String[] args) { Date now = new Date(); System.out.println("toString(): " + now); // dow mon dd hh:mm:ss zzz yyyy // SimpleDateFormat can be used to control the date/time display format: // E (day of week): 3E or fewer (in text xxx), >3E (in full text) // M (month): M (in number), MM (in number with leading zero) // 3M: (in text xxx), >3M: (in full text full) // h (hour): h, hh (with leading zero) // m (minute) // s (second) // a (AM/PM) // H (hour in 0 to 23) // z (time zone) SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z"); System.out.println("Format 1: " + dateFormatter.format(now)); dateFormatter = new Sim...

Get Prime Numbers - Netbeans Java Codes

Run-Shot of Prime Numbers Java Code: import java.util.Scanner; public class FirstnPrimeNumbers { public static void main(String args[]) { Scanner ObjPrime = new Scanner(System.in); int n; System.out.println("Enter the number of prime numbers required."); n=ObjPrime.nextInt(); int k =1; int i,noPrime=0; int num =2 ; do{ for(i=2;i<num;i++){ if(num%i==0) noPrime++; } i=2; if(noPrime==0){ System.out.println("Prime number "+k+" is "+num); k++; } noPrime=0; num++; }while(k<=n); } } [Find more Netbeans Java Code at www.sumikuma.tk] You can also download text file version of this code: Prime-Numbers_javacode.txt