Sunday, June 23, 2013

Creating windows in Java and drawing in them

A very basic one (note that I am just learning, so, some of the usage may not be very correct):
 import java.awt.event.*;  
 import javax.swing.*;  
 public class Main {  
   /**  
    * @param args  
    */  
   public static void main(String s[]) {       
     JFrame f = new JFrame("Jumbled Image");  
     f.addWindowListener(new WindowAdapter() {  
       public void windowClosing(WindowEvent e) {System.exit(0);}  
     });  
     f.pack();  
     f.setSize(200,200);  
     f.setVisible(true);  
   }  
  }  

Nice explanation on JFrame (heavyweight) and JPanel (lightweight) here
Follow those links to understand why you need both...
 import java.awt.*;  
 import javax.swing.*;  
 public class Panel extends JPanel{  
   // Create a constructor method  
   public Panel(){  
     super(); // Calls the parent constructor  
   }  
   public void paintComponent(Graphics g){  
    g.drawLine(10,10,150,150); // Draw a line from (10,10) to (150,150)  
   }  
   public static void main(String arg[]){  
    JFrame frame = new JFrame("BasicPanel");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setSize(200,200);  
    Panel panel = new Panel();  
    frame.setContentPane(panel);       
    frame.setVisible(true);           
  }  
 }  

Basically, you also create a frame here (heavyweight) but then add inside a panel (lightweight) that gets drawn automatically by the GUI when needed (like when created, or moved...) by calling paintComponent. If you want to force it draw you can do "repaint()".

Now we load an image and present it in the window:
 //Import the basic graphics classes.  
 import java.awt.*;  
 import javax.swing.*;  
 public class Panel extends JPanel{  
   String image_name="resources/IMAG0107.jpg";  
   Image image;  
   // Create a constructor method  
   public Panel(){  
     super(); // Calls the parent constructor  
     image = Toolkit.getDefaultToolkit().getImage(image_name);  
   }  
   public void paintComponent(Graphics g){  
      g.drawImage(image,50,10,200,200, this);  
   }  
   public static void main(String arg[]){  
    JFrame frame = new JFrame("BasicPanel");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setSize(200,200);  
    Panel panel = new Panel();  
    frame.setContentPane(panel);       
    frame.setVisible(true);           
  }  
 }  

We had the picture inside the "resources" directory. We can do the same with Java io library, not the toolkit:
 //Import the basic graphics classes.  
 import java.awt.*;  
 import javax.imageio.ImageIO;  
 import javax.swing.*;  
 import java.io.File;  
 import java.io.IOException;  
 public class panel extends JPanel{  
   private static final long serialVersionUID = 1L;  
   String image_name="resources/DSC01642.jpg";  
   Image image;  
   // Create a constructor method  
   public panel(){  
     super(); // Calls the parent constructor  
     //image = Toolkit.getDefaultToolkit().getImage(image_name);  
     try {  
       image=ImageIO.read(new File(image_name));  
     } catch (IOException e) {  
       // TODO Auto-generated catch block  
       System.out.println("Error loading the image");  
       e.printStackTrace();  
     }      
   }  
   public void paintComponent(Graphics g){  
      g.drawImage(image,50,10,200,200, this);  
   }  
   public static void main(String arg[]){  
    JFrame frame = new JFrame("BasicPanel");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setSize(200,200);  
    panel panel = new panel();  
    frame.setContentPane(panel);       
    frame.setVisible(true);           
  }  
 }  

On the next post, we will learn how to do some of this with OpenCV, so, that we can start applying some image processing to it... Also, click here to see the index of these series of posts on OpenCV

No comments:

Post a Comment