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

MATCH REPORT: BNI INDONESIA ALL-STARS 1 CHELSEA 8

MATCH REPORT: BNI INDONESIA ALL-STARS 1 CHELSEA 8 Summary  An enormous and vociferous crowd of Chelsea fans came to see the team on our Indonesian debut and they were rewarded with goals galore, including some quality strikes, as the Asia tour ended in style. The Chelsea goals were split equally between the halves - Eden Hazard, Ramires, Demba Ba and John Terry the scorers before half-time, with Ramires again finding the net after the interval and Romelu Lukaku scoring two as well. Bertrand Traore added to his goal in Malaysia shortly after coming on at half-time. The sole goal from the Indonesian All-Stars was an own-goal, conceded when we were already 8-0 up. Gary Cahillwas the one Chelsea man to play 90 minutes. Chelsea  (4-3-3): Blackman (Schwarzer h-t); Wallace (Ivanovic h-t), Cahill, Terry (c) (Kalas h-t), Bertrand (Cole h-t);Ramires (Feruz 59), Chalobah (Essien h-t), Van Ginkel (McEachran 59); Moses (Traore h-t), Ba (Lukaku h-t), Hazard (Piazon 59). Scorers

ARJUN COOM's Kabhi-Kabhi [feat. Shivali & Natasha] Song Lyrics

MUSIC Written, produced and performed by Arjun Hindi chorus performed by Shivali and Natasha Originally written by Khayyam  Mixing - Elliot Bradley, Soho Studios ------------------------------------------------------------------------------------- Chorus: Kabhi Kabhi Mere Dil Mein Khayal ata Hai Kabhi Kabhi Mere Dil Mein Khayal Ata Hai I feel like a plane on a runaway about to lift off I feel like an ocean so deep, deep as my love You should know that it's you that I've chosen and I'm ready to give you my all Yes I'm right open Come and get, come and get it. You know that I could be. The one that makes you complete 'Cause I've fallen head over heels, and you're the only one. Chorus: Kabhi Kabhi Mere Dil Mein Khayal Aata Hai Kabhi Kabhi Mere Dil Mein Khayal Aata Hai Kii Jaise Tujhko Banaya Gyaa Hai Mere Liye Kii Jaise Tujhko Banaya Gyaa Hai Mere Liye I used to be lost in the darkness, but you were my star. I feel like a king when I'm in your arms, and

Calculator - Netbeans Java Code

Run Shot of Calculator Java Code: public class Calculator { public static void main(String[] args) { boolean go = true; //sets up loop while(go) //creates loop to top { System.out.println("Hello this is my calculator!"); System.out.println("To add, type a, to subtract, type s."); System.out.println("To multiply, type m, to divide, type d."); Scanner scan = new Scanner(System.in); //sets up scanners Scanner scan1 = new Scanner(System.in); String action = scan.nextLine(); //tells comp. to take user input if("a".equals(action)) //addition { System.out.println("Now type in the first number you would like to add."); int add1 = scan.nextInt(); System.out.println("Now type the second number."); int add2 = scan.nextInt();