1 / 14

Object-Oriented Programming in Java

Object-Oriented Programming in Java. Object-Oriented Programming. Objects are the basic building blocks in an O-O program. A program consists of one or more classes that define object type. The First Program. p ublic class Driver { public static void main(String[] args ) {

agrata
Télécharger la présentation

Object-Oriented Programming in Java

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. Object-Oriented Programming in Java

  2. Object-Oriented Programming • Objects are the basic building blocks in an O-O program. • A program consists of one or more classes that define object type

  3. The First Program public class Driver { public static void main(String[] args) { System.out.println(“Hello world”); } }

  4. Class Name attributes/instance variables operations/methods Definition • Each class has two kinds of parts: • state - the object’s attributes or characteristics • behavior - any action that is performed by or upon the object

  5. javax.swing.JFrame - int x - int y - int width - int height - ColorbackColor «constructor» + JFrame(String) «update» + void add(java.awt.Component, int) + void remove(java.awt.Component) + void repaint( ) + void setBackground(java.awt.Color) + void setBounds(int, int, int, int) + void setLayout(java.awt.LayoutManager) + void setVisible(boolean) . . . JFrame

  6. JFrame

  7. Class Specifications JFrame Class Specifications Invariant A JFrame object … • is a rectangular window placed upon a computer display. • is positioned so that its upper left corner is x pixels from the left and y pixels from the top of the display • has a visible region that is width pixels wide, height pixels high, and with a background color of backColor. (Below is a JFrame with backColorof gray on a white display.)

  8. JFrame Class Specifications (continued) Constructor Methods public JFrame(String s) post:a new JFrame (window) object is created ands is displayed in the window’s title bar note:this method call needs to be followed by calls to setBounds and setVisible Update Methods public void add(java.awt.Component pic, int j) pre:j ==0 for best results post:image pic will be drawn upon this JFrame public void remove(java.awt.Component pic) post:image pic will be removed from this JFrame (assuming it was previously added) public void repaint() post: this JFrame is marked to be redrawn as soon as possible public void setBounds(int newX, int newY, int w, int h) pre: w >= 0 andh >= 0 post:x==newXandy==newY andwidth==wand height==h

  9. JFrame Class Specifications (continued) Update Methods public void setBackground(java.awt.Color c) post:backColor==c public void setLayout(java.awt.LayoutManager m) pre:m == null (for our purposes) post:added objects are rearranged via m public void setVisible(boolean b) post:b == true impliesthis JFrame is made visible and brought to the front

  10. JLabel - intx - inty - intwidth - intheight - ColorbackColor - Color foreColor «constructor» + JLabel(String) «update» + void repaint( ) + void setBackground(java.awt.Color) + void setForeground(java.awt.Color) + void setBounds(int, int, int, int) . . . JLabel

  11. Rectangle - int x - int y - int width - int height - ColorbackColor «constructor» + Rectangle(int, int, int, int) «update» + void add(java.awt.Component, int) + void repaint( ) + void setBackground(java.awt.Color) + void setLocation(int, int) + void setSize(int, int) . . . Rectangle class is written by Dave Riley

  12. Oval - int x - int y - int width - int height - ColorbackColor «constructor» + Oval(int, int, int, int) «update» + void add(java.awt.Component, int) + void repaint( ) + void setBackground(java.awt.Color) + void setLocation(int, int) + void setSize(int, int) . . . Oval class is written by Dave Riley

  13. import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFramewin; private Rectangle grayRect; public static void main(String[] args) { new Driver(); } public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 100); win.setLayout(null); win.getContentPane().setBackground( Color.lightGray ); win.setVisible(true); grayRect = new Rectangle(40, 40, 40, 50); grayRect.setBackground( Color.darkGray ); win.add(grayRect, 0); Oval whiteDot= new Oval(10, 10, 80, 80); whiteDot.setBackground( Color.white ); grayRect.add(whiteDot, 0); win.repaint(); } }

More Related