Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes...
-
Upload
amberlynn-simmons -
Category
Documents
-
view
216 -
download
0
Transcript of Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes...
![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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/1.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/2.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/3.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/4.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/5.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/6.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/7.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/8.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/9.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/10.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/11.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/12.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/13.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/14.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/15.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/16.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/17.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/18.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/19.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/20.jpg)
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.](https://reader031.fdocuments.in/reader031/viewer/2022032723/56649d005503460f949d2dff/html5/thumbnails/21.jpg)
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)