21
Computer Science 209 Graphics and GUIs

Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Embed Size (px)

Citation preview

Page 1: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Computer Science 209

Graphics and GUIs

Page 2: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Working with Color

• The class java.awt.Color includes constants for typical color values and also supports the RGB set of colors

• Each component has a foreground and background color (methods setForeground and setBackground can change the defaults)

Page 3: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Label and Text Fieldpublic class ColorView extends JFrame{

public ColorView(){ setTitle("Color Example"); JLabel label = new JLabel("A label"); label.setForeground(Color.red); JTextField field = new JTextField("Some text"); field.setForeground(Color.blue); Container c = getContentPane(); c.add(label, BorderLayout.NORTH); c.add(field, BorderLayout.SOUTH); }

}

Page 4: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Graphical Elements in GUIs

• A view consists of controls for users (buttons, menu items, etc.) and areas to display a model (text fields, list boxes, etc.)

• A model can be displayed as a set of graphical images

• A controller can respond to various mouse events in a graphical area (clicks, movements, drags)

Page 5: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Representing a Graphical Area

• We could draw images in the frame’s visible rectangular area, but they might collide with other controls in the frame

• Add panels to the frame and draw in them

• A panel is an empty rectangular area

Page 6: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: An Empty Panelimport javax.swing.*;import java.awt.*;

public class MainView extends JFrame{

public MainView(){ setTitle("Simple Drawing"); JPanel panel = new Panel1(Color.pink); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); } }

The frame instantiates the panel and adds it to its content pane.

Page 7: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: An Empty Panelimport javax.swing.*;import java.awt.*;

public class Panel1 extends JPanel{ public Panel1(Color backColor){ setBackground(backColor); }}

The panel sets its attributes, such as the background color.

Page 8: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Draw a Shapeimport javax.swing.*;import java.awt.*;

public class MainView extends JFrame{

public MainView(){ setTitle("Simple Drawing"); JPanel panel = new Panel2(Color.pink, Color.blue); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); } }

Pass the background and foreground colors to the panel.

Page 9: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Painting the Shapepublic class Panel2 extends JPanel{

private Color foreColor;

public Panel2(Color backColor, Color foreColor){ setBackground(backColor); this.foreColor = foreColor; }

public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(foreColor); g.fillOval(10, 10, getWidth() / 2, getHeight() / 2); }}

Page 10: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Responsibilities of Methods

• The panel’s constructor sets up its state, including its background color

• The method paintComponent is triggered at startup and whenever the user modifies the window (resize, etc.)

• paintComponent is also run when the programmer calls repaint()

Page 11: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Adjust the Shape’s Positionimport javax.swing.*;import java.awt.*;import java.awt.event.*;

public class Panel3 extends JPanel{

private Color foreColor; private int x, y;

public Panel3(Color backColor, Color foreColor){ setBackground(backColor); this.foreColor = foreColor; x = 10; y = 10; addMouseListener(new PositionListener()); }

The position (x, y) changes in response to a mouse press in the panel

Page 12: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Position the Shape public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(foreColor); g.fillOval(x, y, getWidth() / 2, getHeight() / 2); }

private class PositionListener extends MouseAdapter{ public void mousePressed(MouseEvent e){ x = e.getX(); y = e.getY(); repaint(); } }

The MouseAdapter class implements the MouseListener interface by defining method stubs

Page 13: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Responding to Mouse Events

Event Method

Click mouseClicked(MouseEvent e)

Press mousePressed(MouseEvent e)

Release mouseReleased(MouseEvent e)

Drag mouseDragged(MouseEvent e)

Move mouseMoved(MouseEvent e)

Enter mouseEntered(MouseEvent e)

Exit mouseExited(MouseEvent e)

Page 14: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Interfaces

Interface MethodMouseListener mouseClicked

mousePressed

mouseReleased

mouseEntered

mouseExited

MouseMotionListener mouseDragged

mouseMoved

Page 15: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Adapter Classes

Interface MethodMouseAdapter mouseClicked

mousePressed

mouseReleased

mouseEntered

mouseExited

MouseMotionAdapter mouseDragged

mouseMoved

Page 16: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Using an Anonymous Class public Panel3(Color backColor, Color foreColor){ setBackground(backColor); this.foreColor = foreColor; x = 10; y = 10; addMouseListener(new MouseListener(){ public void mousePressed(MouseEvent e){ x = e.getX(); y = e.getY(); repaint(); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} }); }

Page 17: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Positioning and Sizingpublic class Panel4 extends JPanel{

private Color foreColor; private int x, y, width, height;

public Panel4(Color backColor, Color foreColor){ setBackground(backColor); this.foreColor = foreColor; x = 0; y = 0; width = 0; height = 0; addMouseListener(new PositionListener()); }

Allow the user to specify the position and the dimensions of the oval with a mouse press, drag, and release

Page 18: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Positioning and Sizing public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(foreColor); g.fillOval(x, y, width, height); }

private class PositionListener extends MouseAdapter{ public void mousePressed(MouseEvent e){ x = e.getX(); y = e.getY(); }

public void mouseReleased(MouseEvent e){ width = e.getX() – x; height = e.getY() – y; repaint(); } }

Page 19: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Multiple Shapesimport javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.awt.geom.*;

public class Panel5 extends JPanel{

private Color foreColor; private int x, y, width, height; private java.util.List<Shape> shapes;

public Panel5(Color backColor, Color foreColor){ setBackground(backColor); this.foreColor = foreColor; x = 0; y = 0; width = 0; height = 0; shapes = new ArrayList<Shape>(); addMouseListener(new PositionListener()); }

Page 20: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Example: Multiple Shapes public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(foreColor); for (Shape s : shapes) ((Graphics2D)g).fill(s); }

private class PositionListener extends MouseAdapter{ public void mousePressed(MouseEvent e){ x = e.getX(); y = e.getY(); }

public void mouseReleased(MouseEvent e){ width = e.getX() – x; height = e.getY() – y; shapes.add(new Ellipse2D.Double(x, y, width, height)); repaint(); } }

Page 21: Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the

Panels and Data Models

• Maintain a data model entirely within the panel, as in the shape drawing application

• Or send the model to the panel to draw it

• The model must provide some information about its visual representation (position and size or perhaps a shape or an image)