Swing II - GUI Flashcards
addWindowListener method
register a window listener for a window event
about 7 methods
create empty methods for methods that you dont want to use
window adapter class
- when class doesn’t need most of the methods
- only heading defined
- inherit trivial implementation from windowadapter
can only be done when the JFrame doesnt need to be derived from any other class
Icons
- small picture
ImageIcon dukeIcon = new ImageIcon(“duke_waving.gif”);
images must be in same directory
scroll bars
when using a scroll bar the text is viewes through a view port that shows only part of the text at a time
ScrollPane scrolledText = newJScrollPane(memoDisplay);
the horizontal and vertical scroll bar options/policies
Default - as needed
Never
Always
where on the plane is the (0,0) co-ordinate
top left corner
positive x direction - left to right
positive y direction - top down
adding elements to a frame using co-ord system
- everything is added with a rectangle (add a circle - the circle will be in a rectangle)
- adding a rectangle - location of upper left corner is stated
bounding box
imaginary rectangle that encloses everything that is added to the frame
paint method (only to be used for frames)
The method paint draws the component or container on the screen
public void paint(g){ super.paint(g); g.drawLine(dimension, dimension….,…)
}
the graphics class
The object g of the class Graphics can be used as the calling object for a drawing method–The drawing will then take place inside the area of the screen specified by g
what happens when the paint(g) method is invoked
g is replaced by the graphics object associated with the JFrame - therefore the images are drawn inside the JFrame
draw an oval
g.drawOval(x,y,width,height)
draw an arc
g.drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
draw a rounded rectangle
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight)
what to use to draw figures on a panel
paintComponent
jfram, jpanel and component vs jcomponent
A JPanelis a JComponent, but a JFrameis a Component, not a JComponent
repaint method
The repaint method should be invoked when the graphics content of a window is changed
how does repaint work
- The repaintmethod takes care of some overhead, and then invokes the method paint, which redraws the screen
–Although the repaintmethod must be explicitly invoked, it is already defined
–The paintmethod, in contrast, must often be defined, but is not explicitly invoked
the validate method - google an explanation
causes a container to lay out its components again
It is a kind of “update” method that makes changes in the components shown on the screen–Every container class has the validatemethod, which has no arguments
g.setColor(Color.BLUE)
changes the colour of the pen
custom colours
Color brown = new Color(200, 150, 0);
only type int or type float (dont forget to use a type cast)
the drawString
g.drawString(theText, X_START, Y_Start);
fonts
the constructor for the Font class creaere a font in the given style and size
Font fontObject = new Font(“SansSerif”, Font.PLAIN, POINT_SIZE);
hoe to set the font for the drawString() within the paint()
g.setFont(fontObject);