1 / 14

Working with Graphics, Menu, Images

Working with Graphics, Menu, Images. All graphics are drawn relative to a window Origin of each window is at top left corner 0,0 coordinates in pixel. Graphics class is obtained in two ways Passed to an applet when one of its various methods such as paint() or update() is called.

ami
Télécharger la présentation

Working with Graphics, Menu, Images

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. Working with Graphics, Menu, Images

  2. All graphics are drawn relative to a window • Origin of each window is at top left corner 0,0 coordinates in pixel. • Graphics class is obtained in two ways • Passed to an applet when one of its various methods such as paint() or update() is called. • It is returned by getGraphics() method of Component.

  3. Drawing Lines • void drawLine(int startX, int startY,int endX, int endY) • Example

  4. Drawing Rectangles • void drawRect(int top,int left, int width, int height) • void fillRect(int top,int left,int width,int height) • void drawRoundRect(int top,int left,int width,int height,int xDiam,int yDiam) • void fillRoundRect(int top,int left,int width,int height,int xDiam, int yDiam) • Example

  5. Drawing Ellipses and Circles • void drawOval(int top,int left,int width,int height) • void fillOval(int top,int left,int width,int height) • Example

  6. Drawing Arcs • void drawArc(int top,int left,int width,int height,int startAngle, int sweepAngle) • void fillArc(int top,int left,int width,int height,int startAngle, int sweepAngle) • Start angle tells the angle from where arc is to be drawn and sweep angle tells the angle upto where the arc is to be drawn. • The arc is bounded by rectangle. • Angles are in degree • The arc is drawn counterclockwise if sweepAngle is +ve and clockwise is sweep angle is –ve. • Example

  7. Drawing Polygon • void drawPolygon(int x[], int y[], int numPoints) • void fillPolygon(int x[],int y[], int numPoints) • Example

  8. Menu Bars and Menus • Classes: • MenuBar • Menu • MenuItem • A menu bar contains one or more Menu objects. • Each Menu objects contains a list of MenuItem objects. • Each MenuItem object represent something that can be selected by the user. • Menu is a subclass of MenuItem, so sub menus can be created. • CheckboxMenuItem is used for checkable menu item.

  9. First create instance of MenuBar by using default constructor. • Then create instance of Menu: • Menu() • Menu(String optionName) • Menu(String optionName, boolean removable) If removable is true – menu can be removed.

  10. Create MenuItem: • MenuItem() • MenuItem(String itemName) • MenuItem(String itemName,MenuShortcut key) Disable [ false ] / Enable [ true ] menu item by void setEnabled(boolean enabledFlag) To know item’s status use boolean isEnabled()

  11. More on menu item: • void setLabel(String newName) • String getLabel()

  12. Checkable menu item • CheckboxMenuItem() • CheckboxMenuItem(String itemName) • CheckboxMenuItem(String itemName, boolean on) • To obtain status : • boolean getState() [ checked=true ] • void setState(boolean checked)

  13. After creating menu item, add the item to Menu object by: • MenuItem add(MenuItem item) • Then add Menu object to MenuBar by : Menu add(Menu menu)

  14. Each time a menu item is selected, an ActionEvent object is generated. • Each time a check box menu item is checked or unchecked, an ItemEvent object is generated. • So implement ActionListener and ItemListener interfaces to handle these menu events. • The getItem() method of ItemEvent returns a reference to the item that generated this event. • Object getItem() Example

More Related