140 likes | 551 Views
JFrame vs Graphics Color class Like the String class, and Integer class, does not use the new keyword. holds a color value (nothing more) e.g Color boxColor = new Color( ); boxColor = Color.blue; or Color boxColor = Color.blue
E N D
Color class • Like the String class, and Integer class, does not use the new keyword. • holds a color value (nothing more) e.g Color boxColor = new Color( ); boxColor = Color.blue; or Color boxColor = Color.blue then boxColor can be used to set System properties in Classes/Objects that need color (more later).
Returns an Object? • JColorChooser fills in all of the information in a blank object of the Color class, and copies it to the Color object in the calling statement: boxColor = JColorChooser.showDialog( null, Greeting, default color );
Font Defined (needs constructor info): Font myFont = new Font( “Arial”, Font.PLAIN, 12); Used: g.setFont( myFont ); g.setColor( Color.red); g.drawString( “hello" ,160, 220); Type Style Size
Polygon Defined: int xValues[ ] = {20, 40, 50, 30, 20, 15}; int yValues[ ] = {50, 50, 60, 80, 80, 60}; Polygon shape1 = new Polygon( xValues, yValues, 6 ); Used: g.drawPolygon( shape1 ); Number of points
Graphics method summary paint (Graphics g) { g.setColor( boxColor ); g.fillArc( x, y, width, height, 0, 360 ); g.fillRect( x, y, width, height ); g.setFont( myFont ); g.drawString( “hello" ,160, 220 ); g.drawPolygon( shape1 ); g.drawLine( x1,y1,x2,y2); }
A push-button object JButton helloButton = new JButton( "Hello" ); Where can we place the pushbutton? On anything, really… remember that!
import javax.swing.*; import java.awt.*; public class Button extends JFrame { public Button() { setSize(400,400); JButton myButton = new JButton(“push"); add( myButton); setVisible(true); } public static void main(String args [ ]) { Button app = new Button(); } } // end class
JFrame methods summary • setSize (400, 400); • setLocation (50, 75); • add( someObject ) • setDefaultCloseOperation( EXIT_ON_CLOSE ); • setVisible( true );
the JFrame… layouts and panels • Holds a whole window “context”: frames, scroll bars, buttons, menus, pop-ups, and operator actions: mouse clicks, dragging & dropping. • Operator actions are called events. • "Controls" can be added to a frame. • JFrames are a type of Container.
A “Container” manages all PC resources • It’s brilliant, really JFrame and other Containers Layout – presentation Events – operator actions Container
what does this do? setLayout( new FlowLayout( ) );
more JFrame methods setSize( w, h ); setLocation( x, y ); setDefaultCloseOperation( EXIT_ON_CLOSE ); setLayout( new FlowLayout( ) ); JButton helloButton = new JButton( "Hello" ); add( helloButton ); setVisible( true );