1 / 14

JFrame vs Graphics

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

emily
Download Presentation

JFrame vs Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JFrame vs Graphics

  2. 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).

  3. JColorChooser – returns a Color object to the caller

  4. 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 );

  5. 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

  6. 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

  7. 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); }

  8. A push-button object JButton helloButton = new JButton( "Hello" ); Where can we place the pushbutton? On anything, really… remember that!

  9. 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

  10. JFrame methods summary • setSize (400, 400); • setLocation (50, 75); • add( someObject ) • setDefaultCloseOperation( EXIT_ON_CLOSE ); • setVisible( true );

  11. 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.

  12. A “Container” manages all PC resources • It’s brilliant, really JFrame and other Containers Layout – presentation Events – operator actions Container

  13. what does this do? setLayout( new FlowLayout( ) );

  14. 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 );

More Related