Your SlideShare is downloading. ×
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
04 classes
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

04 classes

404

Published on

Hariprasanna V (9843824677)

Hariprasanna V (9843824677)

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
404
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • A JFrame is the principal way in Java to represent a titled, bordered graphical window. Class JFrame is part of the standard package swing. The swing package is one of several APIs (application programmer interfaces) that comprise the Java Foundations Classes ( JFC ). Although now part of the Java standard, the Java Foundation Classes are extensions of the original Java specification. Because it is an extension, the swing package is found at javax.swing.
  • A JFrame is the principal way in Java to represent a titled, bordered graphical window. Class JFrame is part of the standard package swing. The swing package is one of several APIs (application programmer interfaces) that comprise the Java Foundations Classes ( JFC ). Although now part of the Java standard, the Java Foundation Classes are extensions of the original Java specification. Because it is an extension, the swing package is found at javax.swing.
  • A JFrame is the principal way in Java to represent a titled, bordered graphical window. Class JFrame is part of the standard package swing. The swing package is one of several APIs (application programmer interfaces) that comprise the Java Foundations Classes ( JFC ). Although now part of the Java standard, the Java Foundation Classes are extensions of the original Java specification. Because it is an extension, the swing package is found at javax.swing.
  • A JFrame is the principal way in Java to represent a titled, bordered graphical window. Class JFrame is part of the standard package swing. The swing package is one of several APIs (application programmer interfaces) that comprise the Java Foundations Classes ( JFC ). Although now part of the Java standard, the Java Foundation Classes are extensions of the original Java specification. Because it is an extension, the swing package is found at javax.swing.
  • The second section of Listing defines a default constructor for class ColoredRectangle. A constructor is a special type of Java method. When a constructor is used, Java automatically creates a new object with all the attributes of its class. It is the job of the constructor to assign those attributes sensibly. A default constructor configures the new instance of the class without any external guidance.
  • If your computing system is like our system, the two windows produced during the ColoredRectangle construction are placed one on the top of other. Before providing some input in reaction to the prompt, we arrange the windows so that they can both be seen. After doing so, the Enter key is typed. Its value is extracted by System.in method read() and the program continues with the invocations of the paint() method. If during the rendering, one window is on top of the other, the bottom window will not show its rectangle after the top window is movedùthe reason being the code displays the rectangle only once. Chapter considers how to have an image rerendered automatically on an exposure event.
  • As with objects of the standard classes, a ColoredRectangle member method invocation sends a message to the object targeted by its invocation. For example, the BoxFun.java invocation r1.paint() instructs the ColoredRectangle referenced by variable r1 to display itself; and the invocation r2.paint() instructs the ColoredRectangle referenced by variable r2 to display itself.   
  • Each instance variable is defined without specifying an initial value. Therefore, whenever a new ColoredRectangle object is to be constructed, Java first initializes the new instance variables to default values. By default, numeric instance variables are initialized to 0, boolean instance variables are initialized to false , and reference-type instance variables are initialized to null . Thus, every time a new ColoredRectangle object is to undergo construction, new width, height, x, y, window, and color instance variables are created and default initialized for the new object. The numeric attributes width, height, x, and y are initialized to zero and the class-type attributes window and color are initialized to null . The instance variable definitions specify each of the variable to be private . This modifier indicates that direct access to the instance variables is limited to the class itself. Thus, class ColoredRectangle practices information hiding by encapsulating its attributes. Defining instance variables to be private is a standard practice. When attributes are private , other classes are forced to use the class’s interface methods to manipulate its attributes. Those interface methods normally are programmed to ensure that the requested manipulations are valid. Because the initial definition of class ColoredRectangle does not provide any methods to give access to the attributes, once a ColoredRectangle is constructed it is immutable. In Section , we introduce several ColoredRectangle methods for accessing and modifying the attributes of a ColoredRectangle object.
  • Thus, every time a new ColoredRectangle object is to undergo construction, new width, height, x, y, window, and color instance variables are created and default initialized for the new object. The numeric attributes width, height, x, and y are initialized to zero and the class-type attributes window and color are initialized to null . private int width Represents width of the rectangle to be displayed private int height Represents height of the rectangle to be displayed private int x Represents x-coordinate of the upper-left-hand corner of the rectangle private int y Represents y-coordinate of the upper-left-hand corner of the rectangle private JFrame window Represents the window containing the rectangle drawing private Color color Represents the color of the rectangle to be drawn
  • The second section of Listing defines a default constructor for class ColoredRectangle. A constructor is a special type of Java method. When a constructor is used, Java automatically creates a new object with all the attributes of its class. It is the job of the constructor to assign those attributes sensibly. A default constructor configures the new instance of the class without any external guidance. Unlike other methods (e.g., method main() of program BoxFun.java), a constructor does not have a return type. A return type for a constructor is considered superfluous—a constructor always returns a reference to the newly built object of its class. Furthermore, the name of a constructor is always the same as the name of the class. Therefore, ColoredRectangle() indicates a constructor for class ColoredRectangle
  • The statement list or body of the ColoredRectangle constructor begins with an assignment to the window instance variable for the ColoredRectangle object under construction. As a result of the assignment, window references a new JFrame. The JFrame constructor creating that window object titles the window using its String parameter "Box Fun". window = new JFrame("Box Fun"); The ColoredRectangle constructor then sizes the window to the desired dimensions. For this purpose, the ColoredRectangle constructor signals the window through JFrame member method setSize(). Method setSize() expects two parameters giving the new width and height of the window in pixels. window.setSize(200, 200); In this invocation, both parameters have value 200. The ColoredRectangle constructor then initializes the width, height, and coordinate position of the rectangle to be displayed. width = 40; height = 20; x = 80; y = 90; Color constants. Color.BLACK Color.GREEN Color.PINK Color.BLUE Color.LIGHT_GRAY Color.RED Color.CYAN Color.MAGENTA Color.WHITE Color.DARK_GRAY Color.ORANGE Color.YELLOW Color.GRAY
  • Methods such as paint() are sometimes referred to as facilitators because they support an expected use of the object. Thus, when an instance method is being executed, the attributes of the object associated with the invocation are accessed and manipulated . This property of instance methods is of paramount importance in the object-oriented programming paradigm. It is what enables objects to have behaviors. Therefore, it is crucial when examining code that you understand what object is being manipulated. The third section of Listing 4.2 defines an instance method paint() that displays its ColoredRectangle object. Method paint() is known as a member method or instance method because it must be invoked in conjunction with an instance of a ColoredRectangle (i.e., a ColoredRectangle object). A member method definition includes both a header , which describes its invocation interface, and a body , which comprises the method’s actions. The actions are given as a statement list nested within matching braces. A method header always must supply the return type, method name, and a specification of its parameters. Supplying an access right is optional (e.g., public , private )
  • The rendering is accomplished by first accessing the graphical context in which the drawing commands are to be issued. A graphical context is of type java.awt.Graphics. A Graphics object provides all the necessary information for carrying out a rendering request (e.g., color, component on which the rendering is to occur, font, etc.). Besides encapsulating such information, Graphics objects have a variety of methods for drawing rectangles, lines, polygons, arcs, ovals, and strings. Access to the graphical context of the window is obtained through JFrame instance method getGraphics(), Graphics g = window.getGraphics(); Variable g is a variable whose scope of use is local to method paint(). Such a limitation is not imposed on instance variables. Their scope of use is the entire class.
  • The access to the window’s graphic context through local variable g, enables the rendering color to be set with Graphics method setColor(). g.setColor(color); The actual display within the window is accomplished by using the position and size attributes of the associated object as parameters in the invocation of Graphics method fillRect(). g.fillRect(x, y, width, height); Method fillRect() renders a filled-in rectangle. The method expects four parameters. The first two parameters specify respectively the locations of the x- and y-coordinates for the upper left corner of the rectangle to be drawn. The rendering origin is the upper left hand corner of the window’s container. The next two parameters specify respectively the width and height of the rectangle to be filled. (See Figure for a depiction of the Java coordinate system properties.) All four values are given in pixels. The rectangle is rendered using the current color.
  • Thus, when an instance method is being executed, the attributes of the object associated with the invocation are accessed and manipulated . This property of instance methods is of paramount importance in the object-oriented programming paradigm. It is what enables objects to have behaviors. Therefore, it is crucial when examining code that you understand what object is being manipulated.
  • In a transfer of control to method setWidth(), its single statement method body is executed. The statement assigns the value of formal parameter w to instance variable width. The particular instance variable that is updated, is the one belonging to the ColoredRectangle object initiating the invocation. After executing the statement body, Java transfers the flow of control back to the code segment that invoked the method. A depiction of an invocation process is given in Figure .
  • There is a subtlety to recognize about the ColoredRectangle mutator setWidth(). While the mutator updates the width attribute of its ColoredRectangle object, the mutator does not invoke method paint() to reflect that change in its window. Therefore, the rendering of a ColoredRectangle is unaffected by a setWidth() invocation. A ColoredRectangle object must be repainted for the update to be visible in the window.
  • Transcript

    • 1. Classes
    • 2. Preparation
      • Scene so far has been background material and experience
        • Computing systems and problem solving
        • Variables
        • Types
        • Input and output
        • Expressions
        • Assignments
        • Objects
        • Standard classes and methods
    • 3. Ready
      • Experience what Java is really about
        • Design and implement objects representing information and physical world objects
    • 4. Object-oriented programming
      • Basis
        • Create and manipulate objects with attributes and behaviors that the programmer can specify
      • Mechanism
        • Classes
      • Benefits
        • An information type is design and implemented once
          • Reused as needed
            • No need reanalysis and re-justification of the representation
    • 5. First class – ColoredRectangle
      • Purpose
        • Represent a colored rectangle in a window
        • Introduce the basics of object design and implementation
    • 6. Background
      • JFrame
        • Principal Java class for representing a titled, bordered graphical window.
        • Standard class
          • Part of the swing library
            • import javax .swing.* ;
    • 7. Example
      • Consider
          • JFrame w1 = new JFrame("Bigger");
          • JFrame w2 = new JFrame("Smaller");
          • w1.setSize(200, 125);
          • w2.setSize(150, 100);
          • w1.setVisible(true);
          • w2.setVisible(true);
    • 8. Example
      • Consider
          • JFrame w1 = new JFrame("Bigger");
          • JFrame w2 = new JFrame("Smaller");
          • w1.setSize(200, 125);
          • w2.setSize(150, 100);
          • w1.setVisible(true);
          • w2.setVisible(true);
    • 9. Example
      • Consider
          • JFrame w1 = new JFrame("Bigger");
          • JFrame w2 = new JFrame("Smaller");
          • w1.setSize(200, 125);
          • w2.setSize(150, 100);
          • w1.setVisible(true);
          • w2.setVisible(true);
    • 10. Example
      • Consider
          • JFrame w1 = new JFrame("Bigger");
          • JFrame w2 = new JFrame("Smaller");
          • w1.setSize(200, 125);
          • w2.setSize(150, 100);
          • w1.setVisible(true);
          • w2.setVisible(true);
    • 11. Class ColoredRectangle – initial version
      • Purpose
        • Support the display of square window containing a blue filled-in rectangle
          • Window has side length of 200 pixels
          • Rectangle is 40 pixels wide and 20 pixels high
          • Upper left hand corner of rectangle is at (80, 90)
        • Limitations are temporary
          • Remember BMI.java preceded BMICalculator.java
          • Lots of concepts to introduce
    • 12. ColoredRectangle in action
      • Consider
          • ColoredRectangle r1 = new ColoredRectangle();
          • ColoredRectangle r2 = new ColoredRectangle();
          • System.out.println("Enter when ready");
          • System.in.read();
          • r1.paint(); // draw the window associated with r1
          • r2.paint(); // draw the window associated with r2
    • 13. ColoredRectangle in action
      • Consider
          • ColoredRectangle r1 = new ColoredRectangle();
          • ColoredRectangle r2 = new ColoredRectangle();
          • System.out.println("Enter when ready");
          • System.in.read();
          • r1.paint(); // draw the window associated with r1
          • r2.paint(); // draw the window associated with r2
    • 14. ColoredRectangle in action
      • Consider
          • ColoredRectangle r1 = new ColoredRectangle();
          • ColoredRectangle r2 = new ColoredRectangle();
          • System.out.println("Enter when ready");
          • System.in.read();
          • r1.paint(); // draw the window associated with r1
          • r2.paint(); // draw the window associated with r2
    • 15. ColoredRectangle in action
      • Consider
          • ColoredRectangle r1 = new ColoredRectangle();
          • ColoredRectangle r2 = new ColoredRectangle();
          • System.out.println("Enter when ready");
          • System.in.read();
          • r1.paint(); // draw the window associated with r1
          • r2.paint(); // draw the window associated with r2
    • 16. ColoredRectangle.java outline
      • import javax.swing.*;
      • import java.awt.*;
      • public class ColoredRectangle {
      • // instance variables for holding object attributes
      • private int width;
      • private int height;
      • private int x;
      • private int y;
      • private JFrame window;
      • private Color color;
      • // ColoredRectangle(): default constructor
      • public ColoredRectangle() { // ...
      • }
      • // paint(): display the rectangle in its window
      • public void paint() { // ...
      • }
      • }
    • 17. Instance variables and attributes
      • Data field
        • Java term for an object attribute
      • Instance variable
        • Symbolic name for a data field
        • Usually has private access
          • Assists in information hiding by encapsulating the object’s attributes
        • Default initialization
          • Numeric instance variables initialized to 0
          • Logical instance variables initialized to false
          • Object instance variables initialized to null
    • 18.
      • public class ColoredRectangle {
      • // instance variables for holding object attributes
      • private int width; private int x;
      • private int height; private int y;
      • private JFrame window; private Color color;
      • // ColoredRectangle(): default constructor
      • public ColoredRectangle() {
      • window = new JFrame("Box Fun");
      • window.setSize(200, 200);
      • width = 40; x = 80;
      • height = 20; y = 90;
      • color = Color.BLUE;
      • window.setVisible(true);
      • }
      • // paint(): display the rectangle in its window
      • public void paint() {
      • Graphics g = window.getGraphics();
      • g.setColor(color);
      • g.fillRect(x, y, width, height);
      • }
      • }
    • 19.
      • public class ColoredRectangle {
      • // instance variables for holding object attributes
      • private int width; private int x;
      • private int height; private int y;
      • private JFrame window; private Color color;
      • // ColoredRectangle(): default constructor
      • public ColoredRectangle() {
      • window = new JFrame("Box Fun");
      • window.setSize(200, 200);
      • width = 40; x = 80;
      • height = 20; y = 90;
      • color = Color.BLUE;
      • window.setVisible(true);
      • }
      • // paint(): display the rectangle in its window
      • public void paint() {
      • Graphics g = window.getGraphics();
      • g.setColor(color);
      • g.fillRect(x, y, width, height);
      • }
      • }
    • 20. ColoredRectangle default constructor
    • 21.
      • public class ColoredRectangle {
      • // instance variables for holding object attributes
      • private int width; private int x;
      • private int height; private int y;
      • private JFrame window; private Color color;
      • // ColoredRectangle(): default constructor
      • public ColoredRectangle() {
      • window = new JFrame("Box Fun");
      • window.setSize(200, 200);
      • width = 40; x = 80;
      • height = 20; y = 90;
      • color = Color.BLUE;
      • window.setVisible(true);
      • }
      • // paint(): display the rectangle in its window
      • public void paint() {
      • Graphics g = window.getGraphics();
      • g.setColor(color);
      • g.fillRect(x, y, width, height);
      • }
      • }
    • 22. Color constants
      • Color.BLACK
      • Color.BLUE
      • Color.CYAN
      • Color.DARK_GRAY
      • Color.GRAY
      • Color.GREEN
      • Color.LIGHT_GRAY
      • Color.MAGENTA
      • Color.ORANGE
      • Color.PINK
      • Color.RED
      • Color.WHITE
      • Color.YELLOW
    • 23.  
    • 24.
      • public class ColoredRectangle {
      • // instance variables for holding object attributes
      • private int width; private int x;
      • private int height; private int y;
      • private JFrame window; private Color color;
      • // ColoredRectangle(): default constructor
      • public ColoredRectangle() {
      • window = new JFrame("Box Fun");
      • window.setSize(200, 200);
      • width = 40; x = 80;
      • height = 20; y = 90;
      • color = Color.BLUE;
      • window.setVisible(true);
      • }
      • // paint(): display the rectangle in its window
      • public void paint() {
      • Graphics g = window.getGraphics();
      • g.setColor(color);
      • g.fillRect(x, y, width, height);
      • }
      • }
    • 25. Graphical context
      • Graphics
        • Defined in java.awt.Graphics
        • Represents the information for a rendering request
          • Color
          • Component
          • Font
        • Provides methods
        • Text drawing
          • Line drawing
          • Shape drawing
            • Rectangles
            • Ovals
            • Polygons
    • 26. Java coordinate system
    • 27.
      • public class ColoredRectangle {
      • // instance variables for holding object attributes
      • private int width; private int x;
      • private int height; private int y;
      • private JFrame window; private Color color;
      • // ColoredRectangle(): default constructor
      • public ColoredRectangle() {
      • window = new JFrame("Box Fun");
      • window.setSize(200, 200);
      • width = 40; x = 80;
      • height = 20; y = 90;
      • color = Color.BLUE;
      • window.setVisible(true);
      • }
      • // paint(): display the rectangle in its window
      • public void paint() {
      • Graphics g = window.getGraphics();
      • g.setColor(color);
      • g.fillRect(x, y, width, height);
      • }
      • }
    • 28. Method invocation
      • Consider
          • r1.paint(); // display window associated with r1
          • r2.paint(); // display window associated with r2
      • Observe
        • When an instance method is being executed, the attributes of the object associated with the invocation are accessed and manipulated
        • Important that you understand what object is being manipulated
    • 29. Method invocation
    • 30. Improving ColoredRectangle
      • Analysis
        • A ColoredRectangle object should
          • Be able to have any color
          • Be positionable anywhere within its window
          • Have no restrictions on its width and height
          • Accessible attributes
          • Updateable attributes
    • 31. Improving ColoredRectangle
      • Additional constructions and behaviors
        • Specific construction
          • Construct a rectangle representation using supplied values for its attributes
        • Accessors
          • Supply the values of the attributes
          • Individual methods for providing the width, height, x-coordinate position, y-coordinate position, color, or window of the associated rectangle
        • Mutators
          • Manage requests for changing attributes
          • Ensure objects always have sensible values
          • Individual methods for setting the width, height, x-coordinate position, y-coordinate position, color, or window of the associated rectangle to a given value
    • 32. A mutator method
      • Definition
          • // setWidth(): width mutator
          • public void setWidth(int w) {
          • width = w;
          • }
      • Usage
    • 33. Mutator setWidth() evaluation
    • 34. Subtleties
      • Consider
            • ColoredRectangle r = new ColoredRectangle();
            • r.paint();
            • r.setWidth(80);
            • r.paint();
      • What is the width is the rectangle on the screen after the mutator executes?
    • 35. Other mutators
      • public void setHeight(int h) {
      • height = h;
      • }
      • public void setX(int ulx) {
      • x = ulx;
      • }
      • public void setY(int uly) {
      • y = uly;
      • }
      • public void setWindow(JFrame f) {
      • window = f;
      • }
      • public void setColor(Color c) {
      • color = c;
      • }
    • 36. Mutator usage
    • 37. Accessors
      • Properties
        • Do not require parameters
        • Each accessor execution produces a return value
          • Return value is the value of the invocation
    • 38. Accessor usage
    • 39. Specific construction
      • public ColoredRectangle(int w, int h, int ulx, int uly, JFrame f, Color c) {
      • setWidth(w);
      • setHeight(h);
      • setX(ulx);
      • setY(uly);
      • setWindow(f);
      • setColor(c);
      • }
      • Requires values for each of the attributes
          • JFrame display = new JFrame("Even more fun");
          • display.setSize(400, 400);
          • ColoredRectangle w = new ColoredRectangle(60, 80,
          • 20, 20, display, Color.YELLOW);
    • 40. Specific construction
      • public ColoredRectangle(int w, int h, int ulx, int uly, JFrame f, Color c) {
      • setWidth(w);
      • setHeight(h);
      • setX(ulx);
      • setY(uly);
      • setWindow(f);
      • setColor(c);
      • }
      • Advantages to using mutators
        • Readability
        • Less error prone
        • Facilitates enhancements through localization
    • 41. Seeing double
      • import java.io.*;
      • import java.awt.*;
      • public class SeeingDouble {
      • public static void main(String[] args)
      • throws IOException {
      • ColoredRectangle r = new ColoredRectangle();
      • System.out.println("Enter when ready");
      • System.in.read();
      • r.paint();
      • r.setY(50);
      • r.setColor(Color.RED);
      • r.paint();
      • }
      • }
    • 42. Seeing double

    ×