Docsity

GUI Features - Introduction to Programming in Java - Lecture Slides, Slides of Network security

Partial preview of the text

Download GUI Features - Introduction to Programming in Java - Lecture Slides and more Slides Network security in PDF only on Docsity!

Objectives

  • describe some more GUI features: JPanel,

and mouse listeners/adapters

  1. GUI Examples II

Contents

1. Reminder on the 3-step GUI

2. Painting with JPanel

3. The Final ImageViewer

4. Listener Interfaces

5. Mouse Example: Doodle

6. Adapter Classes

continued

1. Reminder of the 3-Step GUI

• The three steps in writing GUIs:

– 1. Declare the GUI components ;

– 2. Implement the event handlers for the

components;

– 3. Position the components on the screen by using

layout managers and/or containers.

2. Painting with JPanel

• One of the uses of JPanel is as a 'canvas'

(painting surface).

• Its paintComponent() method can be

overridden, and then draw/paint operations

can be added to it.

The Graphics Context

• g links to the panel's drawing area on

screen.

application

JPanel

drawing

area

g

void paintComponent(Graphics g)

{ // drawing operations, e.g.

g.drawImage(...);

3. The Final ImageViewer

The image is drawn onto

a JPanel, which is in the

content area of the JFrame.

(ImageViewer)

a JFrame

(ImagePanel)

a JPanel

  • The menu items now have keyboard

shortcuts:

  • "open" ==> -o
  • "quit" ==> -q

Class Diagrams

uses

extends

extends

private void makeMenuBar() // Create menu bar; add shortcuts. { int shortcut_mask = Toolkit.getDefaultToolkit(). getMenuShortcutKeyMask();

JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar);

// create the File menu JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); :

JMenuItem openItem = new JMenuItem("Open");

openItem. setAccelerator (KeyStroke.getKeyStroke( KeyEvent.VK_O, shortcut_mask)); // ctrl-o

openItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { File f = chooseImage (); if (f != null) imagePanel. displayImage (f); pack(); // triggers resizing to fit image }

});

fileMenu.add(openItem);

:

choose, display image

private File chooseImage()

// open file chooser and let the user select // an image file

{

int returnVal = fileChooser.showOpenDialog(null); if (returnVal != JFileChooser.APPROVE_OPTION) return null; // cancelled

return fileChooser.getSelectedFile();

} // end of chooseImage()

public static void main(String[] args) { new ImageViewer(); }

} // end of ImageViewer class

public void displayImage(File f)

// load and set the image for this panel

{

BufferedImage image = loadImage (f); if (image != null) { width = image.getWidth(); height = image.getHeight(); panelImage = image;

invalidate(); repaint(); // triggers a panel redrawing // with the new image }

} // end of displayImage()

private BufferedImage loadImage(File imageFile) // load an image file and returns it as a // BufferedImage. { try { BufferedImage image = ImageIO.read(imageFile); if (image == null ||(image.getWidth() < 0)) // probably bad file format return null; return image; } catch (IOException e) { return null; } } // end of loadImage()