210 likes | 295 Views
Computer Science 209. Graphics and GUIs. 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).
E N D
Computer Science 209 Graphics and GUIs
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)
Example: Label and Text Field public 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); } }
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)
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
Example: An Empty Panel import 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.
Example: An Empty Panel import 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.
Example: Draw a Shape import 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.
Painting the Shape public 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); } }
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()
Example: Adjust the Shape’s Position import 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
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
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){} }); }
Example: Positioning and Sizing public 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
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(); } }
Example: Multiple Shapes import 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()); }
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(); } }
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)