a new loop

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

96 εμφανίσεις

a new loop

int x = 0;

while (x < 100)

{

x = x+1;


}


int x;



for
( x=0; x<100; x=x+1)

{


}


Introducing, the JFrame

Gives us a work area beside
System.out.println

Three ways to make use of classes
(after importing)


One step instantiation:

JFrame myWindow = new JFrame( );



Two step Instantiation:

public JFrame myWindow
;

public static void method( int a)


{


myWindow = new JFrame( );





}

third way:

public class myClass
extends

JFrame

{

// your class inherits all of JFrame’s

// methods




}

Extension, Hierarchy, and
Inheritance

Library
Class
Your Class
Extend
20 Methods
14 Variables
"Parent"
add 2 Methods
4 Variables
Result 22 Methods
18 Variables
"Child"
Extension, Hierarchy, and
Inheritance


Extends


in the class statement, tells
which larger class serves as the “parent”.


Inheritance


the child class inherits
methods and variables from the parent
class


Hierarchy
-

parent to child relationship

Extend keyword

public class myClass extends someOtherClass


{


// I can use all the methods and


// variables from someOtherClass in myClass.



}

Other terms


Parent / Child


Base class / Sub class


Super Class / Sub class


Any class (not just an imported library
class) can be a parent or base class.



Write classes


then write new inherited
classes for targeted capabilities.


Java common inherited classes


public class myClass extends JFrame


-

myClass gets a whole bunch of methods for
displaying windows on
-
screen


including the ability to launch a
paint( )

method.


public class myClass extends Applet


-

applet and browser graphics


public class myClass extends Object


-

the parent of ALL classes, usually omitted

import javax.swing.*;

public class MainClass
extends JFrame

{


// stuff here, including Constructor

public MainClass()

{


}


public static void main (String [ ] args)


{


MainClass application = new MainClass( );


}

}

What is this?


Main method instantiates the class that it’s
in?


Because main is not considered a member
of the class that contains it.


The Main class serves a place
-
holding
function for the computer, by keeping main
in a usable place.


Instantiating the Main class causes the
constructor (if there is one) to be run.


What other reasons?


the constructor

in any instantiation...

Someclass object = new Someclass( );


actually runs the constructor in the class file:


public class Someclass

{

public Someclass( )


{



}

}

this

why?


It is meant to run code that is desired
when an object is created


it's like any other method

JFrame


Puts graphics
-
capable windows on screen.


JOptionPane uses JFrame


Colors


Fonts


Drawings


A frame for intuitive GUI


Adds a paint method

Use these helper libraries

import javax.swing.JFrame ;

// for JFrame



import java.awt.* ;

// Graphics, for painting
and animation



import java.util.Random ;

// always useful

some JFrame methods



setLocation(100,200);


setSize(250,250);


setVisible(true);


setDefaultCloseOperation(
EXIT_ON_CLOSE
);


paint (Graphics g)


public class MainClass extends JFrame

{

public MainClass ( )


{


setLocation(100,200);


setSize(250,250);


setVisible(true);


}

// end MainClass constructor

public static void main (String[ ] args)


{


MainClass application = new MainClass( );


}
// end main method

}
// end class

public class MainClass extends JFrame

{

public MainClass (
int x, int y
)


{


setLocation(
x, y
);


setSize(250,250);


setVisible(true);


}

// end MainClass constructor

public static void main (String[] args)


{


MainClass application = new MainClass(
100, 200
);


}
// end main method

}
// end class

public class MainClass extends JFrame

{

public MainClass (
int x, int y
)


{


setLocation(
x, y
);


setSize(250,250);


setVisible(true);


}

// end MainClass constructor

public static void main (String[] args)


{


MainClass appl1 = new MainClass(
100, 200
);


MainClass appl2 = new MainClass(
200, 300
);


}
// end main method

}
// end class

Demo

If you don’t extend JFrame…. you can still use it
as an instantiated object

public class MainClass

{

public MainClass ( )


{


JFrame myFrame = new JFrame( );


myFrame.setLocation(100,200);


myFrame.setSize(250,250);


myFrame.setVisible(true);


}

// end MainClass constructor

public static void main (String[] args)


{


MainClass application = new MainClass( );


}
// end main method

}
// end class

graphics on “your computer”


Since there are many computers (types,
instances)....


and since the programs that you write
should run anywhere....


then the programs that you write, should
retrieve and use information native to the
computer that it is running on.


This is called “context awareness”

The paint Method


Computer runs it repeatedly (you don’t),
but then you can run it by calling repaint();.


The computer passes it an object of the
Graphics Class, of it’s own making.

public void paint (Graphics g)

{

// the computer calls the paint program

// automatically whenever an event requires

// the screen to be redrawn





}

the computer supplies this object

Graphics!


g.setColor


g.fillRect


g.drawString


g.drawPolygon



hundreds of methods that use your
computer’s graphics capability

import javax.swing.*;

public class MainClass
extends JFrame

{

public MainClass()

{

setSize(400, 400);

setLocation(100,100);

setDefaultCloseOperation( EXIT_ON_CLOSE );

setVisible(true);

}

public void paint( Graphics g)


{


// GRAPHICS!


}

public static void main (String [ ] args)


{


MainClass application = new MainClass( );


}

}


try something simple

public void paint( Graphics g )

{

g.setColor( Color.RED );

g.fillRect( x, y, width, height );


g.setColor( Color.GREEN );

g.fillArc( x, y, width, height, 0, 360 );

}

How to get CUSTOM colors


Create a Color OBJECT


must supply RGB values


holds a color value (nothing more)


e.g
Color boxColor = new Color( r, g, b);



then boxColor can be used to set System
properties in Classes/Objects that need
color (more later).


Color boxColor = new Color( 0,0,0 );

Color boxColor = new Color( 255,255,255);

Color boxColor = new Color( 255, 0, 0);

Color boxColor = new Color( 0, 255, 0);

Color boxColor = new Color( 0, 0, 255);

how might we get a random
color?

Random myGen = new Random();

int rd,gr,bl;

rd = myGen.nextInt(255);

gr = myGen.nextInt(255);

bl = myGen.nextInt(255);

Color boxColor =
new Color( rd,gr,bl )

);



A method to get Color

public
Random myGen = new Random();

public
Color

get
Random
Color( )


{


int rd,gr,bl;


rd = myGen.nextInt(255);


gr = myGen.nextInt(255);


bl = myGen.nextInt(255);


return(

new Color( rd,gr,bl )

);

// return?


}

using the getColor method

Color boxColor = new Color( 0, 0, 0 );

boxColor = getRandomColor( );


remember:

public
Random myGen = new Random();

public
Color

getColor( )


{


int rd,gr,bl;


rd = myGen.nextInt(255);


gr = myGen.nextInt(255);


bl = myGen.nextInt(255);



return
(

new Color( rd,gr,bl )

);

// return?


}