Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.

Similar presentations


Presentation on theme: "Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details."— Presentation transcript:

1 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects

2 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 2 l Earlier chapters explain how to make graphical user interfaces (GUIs). l This chapter explains a different way of using graphics in Java: drawing pictures. l We will learn how to basic figures such as lines, ovals, and rectangles. l Basic figures can be combined to create elaborate pictures. l An optional section explains how to draw polygons. Basic Figures

3 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Screen Coordinate System (0, 0) origin Y-coordinate is positive and increasing in the downward direction. X-coordinate is positive and increasing to the right. All coordinates are normally positive.

4 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Screen Coordinate System (100, 75) (0, 0) origin X-coordinate: 100 pixels from left edge of screen Y-coordinate: 75 pixels from top edge of screen Location of a rectangle is specified by coordinates of upper left corner.

5 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Screen Coordinate System (100, 75) (0, 0) origin Location of an oval is specified by a tightly fitting rectangle.

6 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 6 The paint Method Most Swing and Swing related components have a method named paint. The paint method draws the component on the screen. To draw basic figures such as ovals and rectangles, you need to redefine the paint method. The paint method is called automatically and should not be invoked in the programmer’s code.

7 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 7 public void paint(Graphics g) { super.paint(g); g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT); g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW, X2_LEFT_BROW, Y2_LEFT_BROW); g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW, X2_RIGHT_BROW, Y2_RIGHT_BROW); //Draw Mouth: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); } The complete paint method from Madeleine.java The program draws this picture.

8 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 8 public void paint(Graphics g) { super.paint(g); g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT); g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW, X2_LEFT_BROW, Y2_LEFT_BROW); g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW, X2_RIGHT_BROW, Y2_RIGHT_BROW); //Draw Mouth: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); } The face, nose, and eyes are ovals. The eyebrows are lines, and the mouth is an arc. The method for drawing each shape has numeric parameters telling where to draw the shape and how big to make it.

9 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 9 public void paint(Graphics g) { super.paint(g); g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT); g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW, X2_LEFT_BROW, Y2_LEFT_BROW); g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW, X2_RIGHT_BROW, Y2_RIGHT_BROW); //Draw Mouth: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); } The paint method receives one parameter, which is an object of the Graphics class. The Graphics parameter is the calling object for all of the methods that draw lines and shapes. Calling the paint method of the parent class is a good programming practice.

10 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 10 import javax.swing.*; import java.awt.*; public class Madeleine extends JFrame { public static final int FRAME_WIDTH = 400; public static final int FRAME_HEIGHT = 400; public static final int FACE_DIAMETER = 200; public static final int X_FACE = 100; public static final int Y_FACE = 50; public static final int NOSE_DIAMETER = 10; public static final int X_NOSE = X_FACE + 95; public static final int Y_NOSE = Y_FACE + 95; public static final int EYE_WIDTH = 20; public static final int EYE_HEIGHT = 10; public static final int X_LEFT_EYE = X_FACE + 55; public static final int Y_LEFT_EYE = Y_FACE + 45; public static final int X_RIGHT_EYE = X_FACE + 130; public static final int Y_RIGHT_EYE = Y_FACE + 45; public static final int X1_LEFT_BROW = X_FACE + 55; public static final int Y1_LEFT_BROW = Y_FACE + 38; public static final int X2_LEFT_BROW = X1_LEFT_BROW + 20; public static final int Y2_LEFT_BROW = Y1_LEFT_BROW + 2; Display 15.2 Drawing a Happy Face (1/3) Display 15.2 Drawing a Happy Face (1/3)

11 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 11 public static final int X1_RIGHT_BROW = X_FACE + 130; public static final int Y1_RIGHT_BROW = Y2_LEFT_BROW; public static final int X2_RIGHT_BROW = X1_RIGHT_BROW + 20; public static final int Y2_RIGHT_BROW = Y1_LEFT_BROW; public static final int MOUTH_WIDTH = 100; public static final int MOUTH_HEIGHT = 50; public static final int X_MOUTH = X_FACE + 50; public static final int Y_MOUTH = Y_FACE + 125; public static final int MOUTH_START_ANGLE = 180; public static final int MOUTH_ARC_SWEEP = 180; public static final int X_TEXT = X_FACE; public static final int Y_TEXT = Y_FACE + 250; public static void main(String[] args) { Madeleine picture = new Madeleine(); picture.setVisible(true); } public Madeleine() { setSize(FRAME_WIDTH, FRAME_HEIGHT); addWindowListener(new WindowDestroyer()); setTitle("Madeleine by Java"); getContentPane().setBackground(Color.white); } Display 15.2 Drawing a Happy Face (2/3) Display 15.2 Drawing a Happy Face (2/3)

12 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 12 public void paint(Graphics g) { super.paint(g); g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT); g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW, X2_LEFT_BROW, Y2_LEFT_BROW); g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW, X2_RIGHT_BROW, Y2_RIGHT_BROW); //Draw Mouth: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); } Display 15.2 Drawing a Happy Face (3/3) Display 15.2 Drawing a Happy Face (3/3)

13 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Execution Result of Medeleine.java

14 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 14 The Graphics class l Contains methods used to draw basic shapes and lines l Is part of the AWT package (or library) so the AWT must be imported: import java.awt.*; l Most methods for painting shapes have two versions: »A draw version which only draws the outline of the shape drawOval »A fill version which fills in the shape fillOval

15 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Some Methods in the Graphics Class (1/3) l drawLine(int x1, int y1, int x2, int y2) - Draws a line between points (x1, y1) and (x2, y2) l drawRect(int x, int y, int width, int height) - Draws the outline of the specified rectangle. (x, y) is the location of the upper left-hand corner. l fillRect(int x, int y, int width, int height) - Fills the specified rectangle(with color). (x, y) is the location of the upper left- hand corner of the rectangle. l draw3DRect(int x, int y, int width, int height, boolean raised) - Draws the outline of the specified rectangle. (x, y) is the location of the upper left-hand corner. - If raised is true, the highlight makes the rectangle appear to stand out from the background. - If raised is false, the highlight makes the rectangle appear to be sunken into the background.

16 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Some Methods in the Graphics Class (2/3) l fill3DRect(int x, int y, int width, int height, boolean raised) - Fills (with color) the rectangle specified by draw3DRec(x, y, width, height, raised) l drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) - Draws the outline of the specified round cornered rectangle. (x, y) is the location of the upper left-hand corner of the enclosing regular rectangle. - arcWidth and arcHeight specify the shape of the round corners. l fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) - Fills (with color) the round rectangle specified by drawRoundRed(x, y, width, height, arcWidth, arcHeight)

17 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 17 Some Methods in the Graphics Class (3/3) l drawOval(int x, int y, int width, int height) - Draws the outline of the oval with smallest enclosing rectangle that has the specified width and height. The rectangle has its upper left-hand corner located at (x, y). l fillOval(int x, int y, int width, int height) - Fills (with color) the oval specified by drawOval(x, y, width, height) l drawArc(int x, int y, int width, int height, int startAngle, int arcSweep) - Draws part of an oval that just fits into an invisible rectangle described by the first four arguments. The portion of the oval drawn is given by the last two arguments. l fillArc(int x, int y, int width, int height, int startAngle, int arcSweep) - Fills (with color) the oval specified by drawArc(x, y, width, height, startAngle, arcSweep)

18 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Drawing Rectangles and Ovals (100, 120) (0, 0) g.fillOval(100, 120, 40, 20); (100, 50) width height 40 20 Graphics object 20 40 g.fillRect(100, 50, 40, 20); X and Y coordinates of upper left corner

19 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 19 Drawing Arcs l An arc is specified by giving an oval and then specifying what portion of the oval will be used for the arc. l To tell which part of the oval will be used, specify the beginning angle and the degrees of the sweep. positive direction 0 degrees width height (x,y) g.drawArc(x, y, width, height, 0, 360);

20 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 20 Arc Examples positive direction 0 degrees negative direction 0 degrees 180 degrees g.drawArc(x, y, width, height, 0, -90); g.drawArc(x, y, width, height, 0, 360); g.drawArc(x, y, width, height, 0, 90); g.drawArc(x, y, width, height, 180, 90); plus 90 degrees

21 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 21 Round Rectangles l Round rectangle is a rectangle where corners have been replaced with arcs. l Specify by giving rectangle information and then height and width of corner arcs. arcWidth width height (x,y) gdrawRoundRect(x, y, width, height, arcWidth, arcHeight); arcHeight

22 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 22 Polygons drawPolygon allows a program to draw shapes with any number of sides. public void drawPolygon(int[] x, int[] y, int point) l Each point in the polygon will have an x coordinate from the first parameter and a y coordinate from the corresponding element of the second parameter. l The third parameter tells how many points the polygon will have. l Always draws a closed polygon. If first and last points are not equal, draws a line from last to first. drawPolyline is similar but can draw an open figure. fillPolygon is similar but fills with color instead of drawing outline.

23 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 23 Polygon Methods in the Class Graphics l public void drawPolygon(int[] x, int[] y, int points) - Draws a polygon through the point (x[0], y[0]), (x[1], y[1])…(x[points-1], y[points-1]) - If the first and last points are not equal, it draws a line from the last to the first point. l public void drawPolyline(int[] x, int[] y, int points) - Draws a polygon through the point (x[0], y[0]), (x[1], y[1])…(x[points-1], y[points-1]) l public void fillPolygon(int[] x, int[] y, int points) - Fills (with color) the polygon specified by drawPolygon(x, y, points)

24 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 24 import javax.swing.*; import java.awt.*; public class PaintComponentDemo extends JFrame { public static final int FRAME_WIDTH = 400; public static final int FRAME_HEIGHT = 400; public static void main(String[] args) { PaintComponentDemo w = new PaintComponentDemo(); w.setVisible(true); } public PaintComponentDemo() { setSize(FRAME_WIDTH, FRAME_HEIGHT); addWindowListener(new WindowDestroyer()); setTitle("The Oval Is in a Panel"); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); SamplePanel p = new SamplePanel(); contentPane.add(p, BorderLayout.CENTER); } Display 15.7 paintComponent(1/2) Display 15.7 paintComponent(1/2)

25 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 25 private class SamplePanel extends JPanel { public SamplePanel() { //Does nothing except create a JPanel. } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval(FRAME_WIDTH/4, FRAME_HEIGHT/4, FRAME_WIDTH/2, FRAME_HEIGHT/3); } Display 15.7 paintComponent(2/2) Display 15.7 paintComponent(2/2)

26 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 26 Execution Result of paintComponent.java

27 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 27 Action Drawings and repaint The SadMadeleine program demonstrates how a program can change a picture. l In the original picture, the face has a frown. l When the user clicks on the button, the picture changes to a smiling face and moves up on the screen.

28 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 28 Action Drawings and repaint The actionPerformed method changes the smile variable that the paint method uses. When the smile variable is changed to true, the paint method knows to draw a smile instead of a frown. public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Click for a Smile.")) smile = true; else System.out.println("Error in button interface."); repaint(); }

29 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 29 Action Drawings and repaint Unless the repaint method is called, the change will not be shown on screen. The repaint method calls paint to update the picture. The paint method should not be called directly. public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Click for a Smile.")) smile = true; else System.out.println("Error in button interface."); repaint(); }

30 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 30 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SadMadeleine extends JFrame implements ActionListener { public static final int FRAME_WIDTH = 400; public static final int FRAME_HEIGHT = 400; public static final int FACE_DIAMETER = 200; public static final int EYE_WIDTH = 20; public static final int EYE_HEIGHT = 10; public static final int NOSE_DIAMETER = 10; public static final int MOUTH_WIDTH = 100; public static final int MOUTH_HEIGHT = 50; public static final int X_SAD_FACE = 190; public static final int Y_SAD_FACE = 150; public static final int X_HAPPY_FACE = 100; public static final int Y_HAPPY_FACE = 50; public static final int MOUTH_START_ANGLE = 180; public static final int MOUTH_ARC_SWEEP = 180; private boolean smile = false; private int xFace = X_SAD_FACE; private int yFace = Y_SAD_FACE; Display 15.8 Action Drawing(1/5) Display 15.8 Action Drawing(1/5)

31 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 31 private int xNose = xFace + 95; private int yNose = yFace + 95; private int xLeftEye = xFace + 55; private int yLeftEye = yFace + 45; private int xRightEye = xFace + 130; private int yRightEye = yFace + 45; private int x1LeftBrow = xFace + 55; private int y1LeftBrow = yFace + 38; private int x2LeftBrow = x1LeftBrow + 20; private int y2LeftBrow = y1LeftBrow + 2; private int x1RightBrow = xFace + 130; private int y1RightBrow = y2LeftBrow; private int x2RightBrow = x1RightBrow + 20; private int y2RightBrow = y1LeftBrow; private int xMouth = xFace + 50; private int yMouth = yFace + 125; public static void main(String[] args) { SadMadeleine picture = new SadMadeleine(); picture.setVisible(true); } Display 15.8 Action Drawing(2/5) Display 15.8 Action Drawing(2/5)

32 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 32 public SadMadeleine() { setSize(FRAME_WIDTH, FRAME_HEIGHT); addWindowListener(new WindowDestroyer()); setTitle("Sad Madeleine"); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.setBackground(Color.white); JButton smileButton = new JButton("Click for a Smile."); smileButton.addActionListener(this); contentPane.add(smileButton, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Click for a Smile.")) smile = true; else System.out.println("Error in button interface."); repaint(); } Display 15.8 Action Drawing(3/5) Display 15.8 Action Drawing(3/5)

33 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 33 public void paint(Graphics g) { super.paint(g); if (smile) { xFace = X_HAPPY_FACE; yFace = Y_HAPPY_FACE; } else { xFace = X_SAD_FACE; yFace = Y_SAD_FACE; } xNose = xFace + 95; yNose = yFace + 95; xLeftEye = xFace + 55; yLeftEye = yFace + 45; xRightEye = xFace + 130; yRightEye = yFace + 45; x1LeftBrow = xFace + 55; y1LeftBrow = yFace + 38; x2LeftBrow = x1LeftBrow + 20; y2LeftBrow = y1LeftBrow + 2; x1RightBrow = xFace + 130; y1RightBrow = y2LeftBrow; x2RightBrow = x1RightBrow + 20; y2RightBrow = y1LeftBrow; xMouth = xFace + 50; yMouth = yFace + 125; Display 15.8 Action Drawing(4/5) Display 15.8 Action Drawing(4/5)

34 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 34 g.drawOval(xFace, yFace, FACE_DIAMETER, FACE_DIAMETER); //Draw Nose: g.fillOval(xNose, yNose, NOSE_DIAMETER, NOSE_DIAMETER); //Draw Eyes: g.fillOval(xLeftEye, yLeftEye, EYE_WIDTH, EYE_HEIGHT); g.fillOval(xRightEye, yRightEye, EYE_WIDTH, EYE_HEIGHT); //Draw eyebrows: g.drawLine(x1LeftBrow, y1LeftBrow, x2LeftBrow, y2LeftBrow); g.drawLine(x1RightBrow, y1RightBrow, x2RightBrow, y2RightBrow); //Draw Mouth: if (smile) g.drawArc(xMouth, yMouth, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); else //Note minus sign: g.drawArc(xMouth, yMouth, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, -MOUTH_ARC_SWEEP); } Display 15.8 Action Drawing(5/5) Display 15.8 Action Drawing(5/5)

35 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 35 Execution Result of SadMadeleine.java

36 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 36 ColorsColors Draw and fill methods like fillOval will use the current color. You can use the setColor method of a Graphics object to change the current color. l To draw a red mouth you could change the paint method of the Madeleine program to include these lines: //Draw Mouth: g.setColor(Color.red); g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);

37 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 37 Defining Colors The Color class mixes amounts of red, green, and blue to produce a new color. l Since colors are made by combing different amounts of red, green, and blue, this is called the RGB color system. The parameters to the Color constructor must be type int or float. When integers are used as parameters to the Color constructor they should be in the range 0 to 255. When floats are used as parameters to the Color constructor they should be in the range 0.0 to 1.0. Color brown = new Color(204, 102, 0);

38 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 38 Some Methods in The Class Color (1/2) l public Color(int r, int g, int b) - Constructor that creates a new Color with the specified RGB values. - The parameters r, g, and b must each be in the range 0 to 255(inclusive). l public Color(float r, float g, float b) - Constructor that creates a new Color with the specified RGB values. - The parameters r, g, and b must each be in the range 0.0 to 1.0(inclusive). l public int getRed() - Returns the red component of the calling object. The returned value is in the range 0 to 255(inclusive). l public int getGreen() - Returns the green component of the calling object. The returned value is in the range 0 to 255(inclusive).

39 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 39 Some Methods in The Class Color (2/2) l public int getBlue() - Returns the blue component of the calling object. The returned value is in the range 0 to 255(inclusive). l public Color brighter() - Returns a brighter version of the calling object. l public Color darker() - Returns a darker version of the calling object. l public boolean equals(Object c) - Returns true if c is equal to the calling object; otherwise, returns false.

40 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 40 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ColorChangeDemo extends JFrame implements ActionListener { public static final int WIDTH = 400; public static final int HEIGHT = 200; private JPanel colorPanel; private Color panelColor; private int redValue = 0; private int greenValue = 0; private int blueValue = 0; public static void main(String[] args) { ColorChangeDemo gui = new ColorChangeDemo(); gui.setVisible(true); } Display 15.11 Color Changes(1/3) Display 15.11 Color Changes(1/3)

41 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 41 public ColorChangeDemo() { Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); setTitle("Color Change Demo"); setSize(WIDTH, HEIGHT); addWindowListener(new WindowDestroyer()); colorPanel = new JPanel(); panelColor = new Color(0, 0, 0); colorPanel.setBackground(panelColor); contentPane.add(colorPanel, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.white); buttonPanel.setLayout(new FlowLayout()); JButton redButton = new JButton("More Red"); redButton.setBackground(Color.red); redButton.addActionListener(this); buttonPanel.add(redButton); JButton greenButton = new JButton("More Green"); greenButton.setBackground(Color.green); greenButton.addActionListener(this); buttonPanel.add(greenButton); JButton blueButton = new JButton("More Blue"); blueButton.setBackground(Color.blue); blueButton.addActionListener(this); buttonPanel.add(blueButton); contentPane.add(buttonPanel, BorderLayout.SOUTH); } Display 15.11 Color Changes(2/3) Display 15.11 Color Changes(2/3)

42 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 42 public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("More Red")) { if (redValue <= 250) redValue = redValue + 5; } else if (actionCommand.equals("More Green")) { if (greenValue <= 250) greenValue = greenValue + 5; } else if (actionCommand.equals("More Blue")) { if (blueValue <= 250) blueValue = blueValue + 5; } else System.out.println("Unexplained Error"); panelColor = new Color(redValue, greenValue, blueValue); colorPanel.setBackground(panelColor); } Display 15.11 Color Changes(3/3) Display 15.11 Color Changes(3/3)

43 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 43 Execution Result of ColorChangeDemo.java

44 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 44 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JColorChooserDemo extends JFrame implements ActionListener { public static final int WIDTH = 400; public static final int HEIGHT = 200; private Container contentPane; private Color changingColor = Color.lightGray; public static void main(String[] args) { JColorChooserDemo gui = new JColorChooserDemo(); gui.setVisible(true); } public JColorChooserDemo() { contentPane = getContentPane(); contentPane.setBackground(changingColor); contentPane.setLayout(new BorderLayout()); setTitle("JColorChooser Demo"); setSize(WIDTH, HEIGHT); addWindowListener(new WindowDestroyer()); Display 15.12 JColorChooser(1/2) Display 15.12 JColorChooser(1/2)

45 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 45 JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.white); buttonPanel.setLayout(new FlowLayout()); JButton changeButton = new JButton("Choose a Color"); changeButton.addActionListener(this); buttonPanel.add(changeButton); contentPane.add(buttonPanel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Choose a Color")) { changingColor = JColorChooser.showDialog( this, "JColorChooser", changingColor); if (changingColor != null)//If a color was chosen contentPane.setBackground(changingColor); } else System.out.println("Unexplained Error"); } Display 15.12 JColorChooser(2/2) Display 15.12 JColorChooser(2/2)

46 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 46 Execution Result of JColorChooser.java

47 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 47 Fonts and Other Text Details l A font is a style of text. A program can use the drawString method to display text on the screen. l The first parameter tells which characters to display. The last two parameters to drawString are coordinates that tell where on the screen to put the text. g.drawString(theText, X_START, Y_START);

48 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 48 Setting the Font The method setFont in a Graphics object can be used to change the current font. l Java guarantees that at least three fonts will be available: »Monospaced »SanSerif »Serif

49 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 49 Using the Font Constructor To specify a font with a particular name, style, and size, use the Font constructor. Size of the font is specified in points. Each point is 1/72 of an inch, but point sizes are only approximate. Font f = new Font(“Serif”, Font.BOLD|Font.ITALIC, POINT_SIZE); g.setFont(f); Name of the font Style of the font: bold and italic Size of the font in points (an integer)

50 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 50 Some Details About The Class Font l public Font(String fontName, int style, int size) - Constructor that creates a version of the font named by fontName in the specified style and size. l Font.BOLD - Specifies bold style. l Font.ITALIC - Specifies italic style. l Font.PLAIN - Specifies plain style. l public abstract void setFont(Font f) - Sets the current font to f. FontName - “ Monospaced ” “ SansSerif ” “ Serif ”

51 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 51 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class FontSampler extends JFrame { public static final int WIDTH = 450; public static final int HEIGHT = 350; public static final int X_START = 20; public static final int Y_START = 50; public static void main(String[] args) { FontSampler w = new FontSampler(); w.setVisible(true); } public FontSampler() { setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); addWindowListener(new WindowDestroyer()); setTitle("Font Samples"); contentPane.setBackground(Color.white); } Display 15.14 FontSampler(1/2) Display 15.14 FontSampler(1/2)

52 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 52 public void paint(Graphics g) { super.paint(g); g.setFont(new Font("Serif", Font.PLAIN, 10)); g.drawString("Serif, Plain, 10 Points", X_START, Y_START); g.setFont(new Font("SansSerif", Font.PLAIN, 12)); g.drawString("SansSerif, Plain, 12 Points", X_START, 2*Y_START); g.setFont(new Font("Monospaced", Font.PLAIN, 14)); g.drawString("Monospaced, Plain, 14 Points", X_START, 3*Y_START); g.setFont(new Font("Monospaced", Font.ITALIC, 18)); g.drawString("Monospaced, Italic, 18 Pts.", X_START, 4*Y_START); g.setFont(new Font("SansSerif", Font.BOLD, 24)); g.drawString("SansSerif, Bold, 24 Points", X_START, 5*Y_START); g.setFont(new Font("Serif", Font.BOLD|Font.ITALIC, 32)); g.drawString("Serif, Bold & Italic, 32 Points", X_START, 6*Y_START); } Display 15.14 FontSampler(2/2) Display 15.14 FontSampler(2/2)

53 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 53 Execution Result of FontSampler.java

54 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 54 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawStringDemo extends JFrame implements ActionListener { public static final int WIDTH = 350; public static final int HEIGHT = 200; public static final int X_START = 20; public static final int Y_START = 100; public static final int POINT_SIZE = 24; private String theText = "Push a button!"; public static void main(String[] args) { DrawStringDemo w = new DrawStringDemo(); w.setVisible(true); } Display 15.13 DrawStringDemo(1/3) Display 15.13 DrawStringDemo(1/3)

55 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 55 public DrawStringDemo() { setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); addWindowListener(new WindowDestroyer()); setTitle("drawString Demonstration"); contentPane.setBackground(Color.white); contentPane.setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); Button helloButton = new Button("Hello"); helloButton.addActionListener(this); buttonPanel.add(helloButton); Button byeButton = new Button("Goodbye"); byeButton.addActionListener(this); buttonPanel.add(byeButton); contentPane.add(buttonPanel, BorderLayout.SOUTH); } public void paint(Graphics g) { super.paint(g); Font f = new Font("Serif", Font.BOLD|Font.ITALIC, POINT_SIZE); g.setFont(f); g.drawString(theText, X_START, Y_START); } Display 15.13 DrawStringDemo(2/3) Display 15.13 DrawStringDemo(2/3)

56 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 56 public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Hello")) theText = "How are you."; else if (e.getActionCommand().equals("Goodbye")) theText = "It was good talking with you."; else theText = "Error in button interface."; repaint(); } Display 15.13 DrawStringDemo(3/3) Display 15.13 DrawStringDemo(3/3)

57 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 57 Execution Result of DrawStringDemo.java

58 Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 58 Summary You can draw figures such as lines, ovals, and rectangles using methods in the class Graphics. You can specify the color of each figure drawn using the method setColor of the Graphics class. You can define your own colors using the class Color. l Colors are defined using the RGB (Red/Green/Blue) system. You can add text to a a graphics drawing by using the Graphics method drawString. You can set the font and point size for a text written using drawString with the Graphics method setFont.


Download ppt "Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details."

Similar presentations


Ads by Google