Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River,

Similar presentations


Presentation on theme: "JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River,"— Presentation transcript:

1 JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53 Listing 5.24 Image filter demonstration using the Graphics2D Class import javax.swing.JFrame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.awt.image.RescaleOp; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; public class Graphics2DExample extends JFrame { public void paint(Graphics canvas) { super.paint(canvas); try { // Load image from default location on disk BufferedImage img = ImageIO.read(new File("java.jpg")); // Draw the image at coordinate 50,50 canvas.drawImage(img, 50, 50, null); // Copy the image to another buffer with a // color model (ARGB) to support alpha blending // that allows translucency int w = img.getWidth(null); int h = img.getHeight(null); BufferedImage img2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics g = img2.getGraphics(); g.drawImage(img, 0, 0, null);

54 JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved // Create a rescale filter operation that // makes the image 30% opaque float[] scales = { 1f, 1f, 1f, 0.3f }; float[] offsets = new float[4]; RescaleOp rop = new RescaleOp(scales, offsets, null); // Draw the image, applying the filter Graphics2D g2 = (Graphics2D) canvas; g2.drawImage(img2, rop, 150, 50); } catch (IOException e) { System.out.println("Error reading the image."); } public Graphics2DExample() { setSize(275,175); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Graphics2DExample guiWindow = new Graphics2DExample(); guiWindow.setVisible(true); }

55 JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Application Output

56 JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

57


Download ppt "JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River,"

Similar presentations


Ads by Google