Download Lecture 4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
6/4/15
Lecture 4 Using Containers and Components java.awt.Component <abstract>
«constructor»
# Component()
«update»
+ void setBounds( int x, int y, int w, int h )
+ void setLocation( int x, int y )
+ void setSize( in w, int h )
+ void setBackground( Color c )
+ void setForeground( Color c )
+ void repaint()
«query»
+ int getX()
+ int getY()
+ int getWidth()
+ int getHeight()
+ Color getBackground()
+ Color getForeground()
+ Container getParent()
...
java.awt.Container
«constructor»
+ JComponent()
«update»
+ void add( Component q, int p)
+ void remove( Component q )
«query»
+ Component getComponentAt(int x, int y)
...
Many descendant classes, including:
javax.swing.JFrame,
Oval, Rectangle,…
Descendant classes include:
java.awt.Label
Line …
1
6/4/15
container.add(thing, 0);
The above statement establishes container as the Container of thing.
This means:
• thing is now visible within the boundaries of container
• thing.getParent() == container
• thing has no other parent
When a component, such as thing, is not added:
thing.getParent() == null
container.remove(thing);
The above statement removes thing from container.
This means:
• thing.getParent() == null
• thing is no longer visible
Removing a Component that has not been added doesn't do anything,
but is not an error.
2
6/4/15
java.awt.Container
Container
is an awt class well suited to inheritance.
A Container object is a transparent rectangle. It is useful
for grouping other graphical objects into a single unit.
A Container subclass can reuse...
• add and remove
• setBounds and setLocation
• getX, getY, getWidth, getHeight, getBackground
• repaint
A Container subclass may want to override...
• setSize and setBounds
• setBackground
import java.awt.Color;
import java.awt.Container;
/** @invariant (for each TeaCup t)
*
t fills a space of getWidth()==40 and getHeight()==2
*
with the image of a teacup filled with some color */
public class TeaCup extends Container{
}
/** @post the color of the TeaCup is c */
public TeaCup(Color c)
{
super();
setBounds(0, 0, 40, 20);
Oval handle = new Oval(28, 1, 8, 13);
handle.setBackground(c);
Oval handleCenter = new Oval(2, 2, 4, 9);
handleCenter.setBackground(Color.white);
handle.add(handleCenter, 0);
add(handle, 0);
Container cupBackground = new Container();
cupBackground.setBounds(0, 5, 30, 15);
Oval cup = new Oval(0, -18, 30, 35);
cup.setBackground(c);
cupBackground.add(cup, 0);
add(cupBackground, 0);
Oval top = new Oval(0, 1, 30, 6);
add(top, 0);
}
3
6/4/15
Objects used drawing the cup body:
public class Driver extends ThreeButtons
private ThreeButtonFrame window;
private TeaCup redCup, blueCup;
{
/** @post a window has been created
*
and redCup is positioned at its top center
*
and blueCup is positioned at its bottom center
*/
public Driver()
{
window = new ThreeButtonFrame( "Move the Cups" );
window.getContentPane().setBackground(Color.white);
redCup = new TeaCup(Color.red);
window.add(redCup, 0);
blueCup = new TeaCup(Color.blue);
window.add(blueCup, 0);
midAction();
}
4
6/4/15
/** @pre redCup != null and blueCup != null
* @post redCup.getY() == redCup.getY()@pre + 5
*
and blueCup.getY() == blueCup.getY()@pre - 5
*/
public void leftAction()
{
redCup.setLocation(redCup.getX(), redCup.getY() + 5);
redCup.repaint();
blueCup.setLocation(blueCup.getX(), blueCup.getY() - 5);
blueCup.repaint();
}
/** @pre redCup != null and blueCup != null
* @post redCup is repositioned at the top center of window
*
and blueCup is repositioned at the bottom center
*/
public void midAction()
{
redCup.setLocation(280, 30);
redCup.repaint();
blueCup.setLocation(280, 400);
blueCup.repaint();
}
java.awt.Component <abstract>
«constructor»
# Component()
«update»
+ void setBounds( int x, int y, int w, int h )
+ void setLocation( int x, int y )
+ void setSize( in wt, int h )
+ void setBackground( Color c )
+ void setForeground( Color c )
+ void repaint()
«query»
+ int getX()
+ int getY()
+ int getWidth()
+ int getHeight()
+ Color getBackground()
+ Color getForeground()
+ Container getParent()
...
java.awt.Container
«constructor»
+ JComponent()
«update»
+ void add( Component q, int p)
+ void remove( Component q )
«query»
+ Component getComponentAt(int x, int y)
...
javax.swing.JComponent
«constructor»
+ JComponent()
The keys to drawing
«update»
+ void paint( Graphics )
+ void paintChildren( Graphics )
5
6/4/15
The JVM refreshes visible windows and Components that need
repainting by calling their paint method(s).
However, the paint method in JComponent does nothing.
This is an example of polymorphism, because each different
type of JComponent (Oval, Rectangle, and so forth) must
have its own paint method that draws its image.
Note that the JVM passes a Graphics parameter to the paint method.
This parameter is key to writing a paint.
import java.awt.*;
import javax.swing.JComponent;
public class LittleBlackTriangle extends JComponent {
/** @post getX()==100 and getY()==100
*
and getWidth()==10 and getHeight()==10
*/
public LittleBlackTriangle() {
setBounds(100, 100, 20, 20);
}
}
/** @post draws the outline of a black triangle pointing
*
right and filling a 20 by 20 square
*/
public void paint( Graphics g ) {
g.setColor( Color.black );
g.drawLine(0, 0, 0, 19);
g.drawLine(0, 19, 19, 10);
g.drawLine(19, 10, 0, 0);
}
6
6/4/15
The paint method must use the Graphics parameter for all drawing.
Locations used within paint are relative to the JComponent's
boundaries.
java.awt.Graphics
«update»
+ void drawLine( int x1, int y1, int x2, int y2 )
+ void drawOval( int x, int y, int w, int h )
+ void drawRect( int x, int y, int w, int h )
+ void drawString( String s )
+ void drawPolygon( int[ ] xPts, int[ ] yPts, int ptCount )
+ void fillOval( int x, int y, int w, int h )
+ void fillRect( int x, int y, int w, int h )
+ void fillString( String s )
+ void fillPolygon( int[ ] xPts, int[ ] yPts, int ptCount )
+ void setColor( Color c )
+ void setFont( java.awt.Font f )
...
7
6/4/15
paintChildren(g);
This causes paint(g) to be called upon every Component c
for which c.getParent() == this.
If the paint method does not call paintChildren, then added
Components are not visible.
import java.awt.*;
import javax.swing.JComponent;
public class Oval extends JComponent
public Oval( int x, int y, int w, int h )
super();
setBounds(x, y, w, h);
setBackground( Color.black );
}
}
{
{
public void paint( Graphics g )
{
g.setColor( getBackground() );
g.fillOval(0, 0, getWidth()-1, getHeight()-1);
paintChildren(g);
}
8
6/4/15
import java.awt.*;
import javax.swing.JComponent;
/** @invariant (for each TeaCup t)
*
t fills a space of getWidth()==40 and getHeight()==2
*
with the image of a teacup filled in getBackground() color */
public class TeaCup extends JComponent {
/** @post the color of the TeaCup is c */
public TeaCup(Color c) {
super();
setBounds(0, 0, 40, 20);
setBackground(c);
}
}
public void paint(Graphics g)
{
g.setColor( getBackground() );
g.fillOval(28, 1, 8, 13); //handle
g.setColor( Color.white );
g.fillOval(30, 3, 4, 9); //handle center
g.setColor( getBackground() );
g.fillOval(0, -13, 30, 35); //cup
g.setColor( Color.white );
g.fillRect(0, 0, 30, 5); //cover top of cup
g.setColor( Color.black );
g.fillOval(0, 1, 30, 6); //black tea
}
9