f27sb2 software development 2 lecture 10: java guis 7

32
F27SB2 Software Development 2 Lecture 10: Java GUIs 7

Upload: mariano-pink

Post on 15-Dec-2015

235 views

Category:

Documents


1 download

TRANSCRIPT

F27SB2 Software Development 2

Lecture 10: Java GUIs 7

Displaying images

• how to display images in a GUI?BufferedImage– from AWT– low level representation as array of pixels– image processing is complex

Image– from AWT– displayable image– platform independent

Displaying images

Icon– interface– from Swing– object that can draw a graphic of specified

width/height at a specific locationsetIcon(Icon i)

• place Icon on JComponent

Displaying images

ImageIcon– implementation of Icon interface– uses an Image to draw an Icon

ImageIcon(String filename)

• constructs ImageIcon from image from a specified file

• then set ImageIcon on JComponentgetImage()

• returns Image from ImageIcon

Display single image

• specify file name via main• display in JLabel in JFrame

import javax.swing.*;import java.awt.*;import java.awt.event.*;

class Photo1 extends JFrame { JLabel l;

Display single image Photo1() { setLayout(new GridLayout(1,1)); l = new JLabel("picture",JLabel.CENTER); l.setVisible(true); l.setOpaque(true); add(l); }

void setPhoto(String s) { l.setIcon(new ImageIcon(s)); }}

Display single imageclass TestPhoto1{ public static void main(String [] args) { Photo1 p = new Photo1(); p.setSize(400,420); p.setVisible(true); p.setTitle("Photo1"); p.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);}}); p.setPhoto(args[0]); }}

Display single image

original image (EE5.jpg) display

• original image not same size/shape as display• centre of image is displayed• no scaling/resizing

Resizing image

getScaledInstance (int width,int height,int hints)

• resizes Image to be width*height• hints– determine how scaling is performed– we’ll use Image.SCALE_SMOOTH

• NB direct scaling to dimensions of JComponent may distort image if not same shape

Resizing image

• want resized image to retain original dimensions– new image width/new image height = old image width/old image height

• for given new image height:– new image width = new image height*old image

width/old image height• for given new image width:– new image height = new image width*old image

height/old image width

Resizing image

JLabel

Image 1

lh

lw

i1h

i1w

• image width/image height < JLabel width/JLabel height• new image height = JLabel height• new image width = JLabel height* image width/ image height

Resizing image

JLabelImage 2lh

lw

i2w

i2h

• image width/image height > JLabel width/JLabel height• new image width = JLabel width• new image height = JLabel width*image height/ image width

Resizing imageimport javax.swing.*;import java.awt.*;import java.awt.event.*;

class Photo2 extends JFrame { JLabel l;

Photo2() { setLayout(new GridLayout(1,1)); l = new JLabel("picture",JLabel.CENTER); l.setVisible(true); l.setOpaque(true); add(l); }

Resizing image void setPhoto(String s) { Image i1 = (new ImageIcon(s)).getImage(); int lw = l.getWidth(); int lh = l.getHeight(); int iw = i1.getWidth(this); int ih = i1.getHeight(this); int w = lw; int h = lh; if(iw>lw){ w = lw; h = lh*ih/iw; } else if(ih>lh){ w = lw*iw/ih; h = lh; } Image i2 = i1.getScaledInstance(w,h,Image.SCALE_SMOOTH); l.setIcon(new ImageIcon(i2)); }}

Resizing imageclass TestPhoto2{ public static void main(String [] args) { Photo2 p = new Photo2(); p.setSize(400,420); p.setVisible(true); p.setTitle("Photo2"); p.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);}}); p.setPhoto(args[0]); }}

Resizing image

original image (EE5.jpg) display

Photo Browser

• choose directory• scroll through images which are .jpg or .JPG

file name

photo< >

JFrame

JLabel

JButton

Photo Browser...

class Browser extends JFrame implements ActionListener{ JMenuBar jb; JMenu file; JMenuItem MNew,MOpen,MClose,MExit;

JFileChooser files;

JButton left,right; JLabel photo; JLabel fileName;

Photo Browser

• maintain array of File for contents of chosen directory

File [] directory;

• keep track of current File in directory

int current;

Photo BrowserBrowser() { jb = new JMenuBar(); file = new JMenu("Picture");

MOpen = new JMenuItem("Open"); MOpen.addActionListener(this); file.add(MOpen);

jb.add(file);

setJMenuBar(jb);

files = new JFileChooser();

Photo Browser

• need file chooser which will only select directories

• change file chooser modesetFileSelectionMode(int mode)

mode:

• DIRECTORIES_ONLY• FILES_AND_DIERCTORIES• FILES_ONLY

Photo Browser files.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);

Font f = new Font("Sans Serif",Font.BOLD,24);

left = new JButton("<"); left.setFont(f); left.addActionListener(this); add(left,BorderLayout.WEST);

photo = new JLabel(); add(photo,BorderLayout.CENTER);

Photo Browser right = new JButton(">"); right.setFont(f); right.addActionListener(this); add(right,BorderLayout.EAST);

fileName = new JLabel("",JLabel.CENTER); fileName.setFont (new Font("Sans Serif",Font.PLAIN,18)); add(fileName,BorderLayout.NORTH); }

Photo Browser

• directories may contain files of mixed type• check if file name ends with appropriate

extension boolean endsWith(String s1,String s2) { int d = s1.length()-s2.length(); if(d<0)return false; for(int i = s2.length()-1;i>=0;i--) if(s1.charAt(i+d)!=s2.charAt(i)) return false; return true; }

• built in String method endsWith(String s)

Photo Browser

• require name & path information from File String getName()

• returns file name in directoryString getPath()

• returns file path from root directory

Photo Browser boolean checkJPG(int i) { Image i1,i2; int ph,pw,ih,iw; String name = directory[i].getName(); if(endsWith(name,".jpg") || endsWith(name,".JPG")) { String path = directory[i].getPath(); i1 = (new ImageIcon(path)).getImage(); ph = photo.getHeight(); pw = photo.getWidth(); ih = i1.getHeight(this); iw = i1.getWidth(this); int w = pw; int h = ph;

Photo Browser if(iw>ih) { w = pw; h = ph*ih/iw; } else if(ih>iw) { w = pw*iw/ih; h = ph; } i2 = i1.getScaledInstance (w,h,Image.SCALE_SMOOTH); photo.setIcon(new ImageIcon(i2)); current = i; fileName.setText(name); return true; } return false; }

Photo Browser

• need list of directory contents from chooser selection

File [] listFiles()

• returns array of File from chooser• can then check each in turn to see if

appropriate file type

Photo Browser public void doOpen() { int response = files.showOpenDialog(this); if(response==JFileChooser.APPROVE_OPTION) { File f = files.getSelectedFile(); directory = f.listFiles(); for(int i=0;i<directory.length;i++) if(checkJPG(i)) return; } }

Photo Browser public void doRight() { for(int i=current+1;i<directory.length;i++) if(checkJPG(i)) return; }

public void doLeft() { for(int i=current-1;i>=0;i--) if(checkJPG(i)) return; }

Photo Browser public void actionPerformed(ActionEvent e) { if(e.getSource()==MOpen) doOpen(); else if(e.getSource()==left) doLeft(); else if(e.getSource()==right) doRight(); }}

class TestBrowser{...}

Photo Browser