Objects First With Java - Chapter 1

36 slides
2.51 MB
679 views

Similar Presentations

Presentation Transcript

1

COS 260 DAY 24 Tony Gauvin

2

AgendaQuestions? 10th Mini Quiz Corrected So-so results Capstone progress report over due Course evaluations Assignment 6 Due Assignment 7 Posted Discussion on Building Graphic User Interfaces

3

Final CountdownDec 3 Finish Chapter 11 Assignment 6 due Dec 7 11th mini quiz Begin Chapter 12 Dec 10 Finish Chapter 12 Assignment 7 Due Dec 14 @ 10 AM 12th mini quiz Capstone Presentations Copyright © 2014 Pearson Education, Inc.Slide 1-*

4

Building Graphical User Interfaces5.0

5

OverviewConstructing GUIs Interface components GUI layout Event handlingObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

6

The imageviewer projectObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7

Image processingObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8

Class responsibilitiesImageViewer Sets up the GUI structure. ImageFileManager Static methods for image file loading and saving. ImagePanel Displays the image within the GUI. OFImage Models a 2D image.Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9

Important Support ClassesColor http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html BufferedImage http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html Graphics http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

10

OFImageOur subclass of BufferedImage. Represents a 2D array of pixels. Important methods: getPixel, setPixel getWidth, getHeight Each pixel has a color. We use java.awt.Color.Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11

Adding an ImagePanelObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Köllingpublic class ImageViewer { private JFrame frame; private ImagePanel imagePanel; … private void makeFrame() { Container contentPane = frame.getContentPane(); imagePanel = new ImagePanel(); contentPane.add(imagePanel); } … }

12

Loading an imageObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Köllingpublic class ImageViewer { private JFrame frame; private ImagePanel imagePanel; … private void openFile() { File selectedFile = …; OFImage image = ImageFileManager.loadImage(selectedFile); imagePanel.setImage(image); frame.pack(); } … }

13

Things to Do Add two labels Image file name over the image Status under below the image Add Filter menu Add Help menu Dialog with “About Image viewers” Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14

Layout managersManage limited space for competing components. FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout. Manage Container objects, e.g. a content pane. Each imposes its own style. https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15

FlowLayoutObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

16

BorderLayoutObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17

GridLayoutObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18

BoxLayoutObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael KöllingNote: no component resizing.

19

Struts and GlueInvisible components used as spacing. Available from the Box class. Strut: fixed size. Component createHorizontalStrut(int width) Component createVerticalStrut(int height) Glue: fills available space. Component createHorizontalGlue() Component createVerticalGlue() https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

20

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21

Nested containersSophisticated layouts can be obtained by nesting containers. Use JPanel as a basic container. Each container will have its own layout manager. Often preferable to using a GridBagLayout.Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

22

DialogsModal dialogs block all other interaction. Forces a response from the user. Non-modal dialogs allow other interaction. This is sometimes desirable. May be difficult to avoid inconsistencies. https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

23

JOptionPane standard dialogsMessage dialog Message text plus an OK button. Confirm dialog Yes, No, Cancel options. Input dialog Message text and an input field. Variations are possible.Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

24

A message dialogObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Köllingprivate void showAbout() { JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About ImageViewer", JOptionPane.INFORMATION_MESSAGE); }

25

Image filtersFunctions applied to the whole image.Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Köllingint height = getHeight(); int width = getWidth(); for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { Color pixel = getPixel(x, y); alter the pixel's color value; setPixel(x, y, pixel); } }

26

Adding further filtersObjects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Köllingprivate void makeLighter() { if(currentImage != null) { currentImage.lighter(); frame.repaint(); showStatus("Applied: lighter"); } else { showStatus("No image loaded."); } } private void threshold() { if(currentImage != null) { currentImage.threshold(); frame.repaint(); showStatus("Applied: threshold"); } else { showStatus("No image loaded."); } }Code duplication? Refactor!

Browse More Presentations

Last Updated: 8th March 2018

Recommended PPTs