to lecture slides for week 2
Download
Report
Transcript to lecture slides for week 2
SD2071 Games Programming
Aaron Kans
Abstraction, inheritance and interfaces
Exceptions
Two dimensional arrays
Java collections framework
Files
Abstraction, inheritance and interfaces
By the end of this lecture you should be able to:
•
explain the terms abstraction and abstract data type;
•
create your own interfaces in Java;
•
make use of adapters in programs;
•
explain the purpose of inner classes and use these
classes appropriately;
•
explain the purpose of the toString method.
Abstraction
the idea of focussing on what an object does, without worrying
about the detail of how it does it.
what
how
a class template is often referred to as an abstract data type;
in OO development it is possible to broadly define a class,
specifying its fundamental behaviour, and concentrating on the
important details that make the class what it is.
Abstract classes and interfaces
An interface is a class in which all methods are abstract;
An example of an interface is ActionListener,
It is perfectly possible to create our own interfaces.
Example
•
we wish to place the organization's logo somewhere on the screen;
•
it would be useful if we could produce a class that we could
customize later with a particular logo;
•
that way the class is re-usable, because we just have to attach the
right logo at the time.
•
to make a class attachable it would need to have an attach method.
The Attachable interface
import javax.swing.*;
public interface Attachable
{
public void attach(JFrame frameIn, int xPos, int yPos);
}
implementing an interface is very much like inheriting a class;
the class that implements Attachable becomes a kind of Attachable;
it will "inherit" and redefine the attach method.
The CAndKLogo class
import java.awt.*;
import javax.swing.*;
public class CAndKLogo implements Attachable
{
public void attach(JFrame frameIn, int xPos, int yPos)
{
Graphics g = frameIn.getContentPane().getGraphics();
g.setFont(new Font("Serif", Font.BOLD,15));
g.setColor(Color.red);
g.fillRect(xPos,yPos,125,20);
g.setColor(Color.yellow);
g.drawString("Charatan & Kans", xPos + 3, yPos + 15);
}
}
import java.awt.*;
import javax.swing.*;
public class LogoFrame extends JFrame
{
private Attachable logo;
private int xPos;
private int yPos;
public LogoFrame(Attachable logoIn, int xIn, int yIn)
{
setTitle("Logo Frame");
logo = logoIn;
xPos = xIn;
yPos = yIn;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(250,250);
setLocation(200,200);
setVisible(true);
}
.
.
.
public void paint(Graphics g)
{
super.paint(g);
logo.attach(this, xPos, yPos);
}
}
public class LogoTester
{
public static void main(String[] args)
{
new LogoFrame(new CAndKLogo(), 3, 5);
}
}
RUN
public class LogoTester2
{
public static void main(String[] args)
{
new LogoFrame(new BAndBLogo(), 110, 5);
}
}
RUN
The RedCircle program
The methods of MouseMotionListener
mouseMoved
mouseDragged
Specifies the behaviour that occurs
when the mouse is moved when no
button is depressed.
Specifies the behaviour that occurs
when the mouse is moved with the
left-hand button depressed.
The methods of MouseListener
mousePressed
mouseReleased
mouseClicked
mouseEntered
mouseExited
Specifies the behaviour that occurs
when the left-hand button is pressed.
Specifies the behaviour that occurs
when the left-hand button is released.
Specifies the behaviour that occurs when the
left-hand button is clicked on a component.
Specifies the behaviour that occurs when
the cursor enters a component.
Specifies the behaviour that occurs when
the cursor leaves a component.
The RedCircle class
import
import
import
public
java.awt.*;
javax.swing.*;
java.awt.event.*;
class RedCircle extends JFrame
implements MouseMotionListener, MouseListener
{
private
private
private
private
private
int xPos;
int yPos;
int width;
int height;
boolean mouseDown;
public RedCircle(int widthIn, int heightIn)
{
setTitle("Red Circle Game");
addMouseMotionListener(this);
addMouseListener(this);
width = widthIn;
height = heightIn;
xPos = width/2 -20;
yPos = height/2 - 20;
setSize(width, height);
setLocation(300,300);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.clearRect(0,0, width, height);
g.drawString("Click on the red circle", width/2 - 60, 50);
g.setColor(Color.red);
g.fillOval(xPos,yPos,20,20);
if(mouseDown)
{
g.drawString("Keep trying!!!", width/2 - 40, height -10);
}
}
public void mouseMoved(MouseEvent e)
{
xPos = e.getX() - 50;
yPos = e.getY() - 50;
repaint();
}
public void mouseDragged(MouseEvent e)
{
xPos = e.getX() - 50;
yPos = e.getY() - 50;
repaint();
}
public void mousePressed(MouseEvent e)
{
mouseDown = true;
repaint();
}
public void mouseReleased(MouseEvent e)
{
mouseDown = false;
repaint();
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
Adapters
An adapter is a special class that acts as an intermediary
between our class and the interface, making it
unnecessary to code all the methods because the class
can be extended in the normal way using inheritance;
An adapter is provided for every interface that comes with
the standard Java packages.
Adapters
class MouseAdapter implements MouseListener
{
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
Using an adapter in the RedCircle class
The following line in the constructor indicates the class
where the program can find the instructions for processing
the mouse click events.
addMouseListener(this);
Instead, we could write a class (eg RedCircleAdapter) for this
express purpose;
This class would extend MouseAdapter and would code the
mousePressed and mouseReleased methods;
The above line would then have to be changed to:
addMouseListener(new RedCircleAdapter());
Using an inner class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RedCircleWithAdapter extends JFrame
implements MouseMotionListener
{
private int xPos;
private int yPos;
private int width;
private int height;
private boolean mouseDown;
class RedCircleAdapter extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
mouseDown = true;
repaint();
}
public void mouseReleased(MouseEvent e)
{
mouseDown = false;
repaint();
}
}
public RedCircleWithAdapter(int widthIn, int heightIn)
{
setTitle("Red Circle Game");
addMouseMotionListener(this);
addMouseListener(new RedCircleAdapter());
width = widthIn;
height = heightIn;
xPos = widthIn/2 -20;
yPos = heightIn/2 - 20;
setSize(width, height);
setLocation(300,300);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.clearRect(0,0,width, height);
g.drawString("Click on the red circle", width/2 - 60, 50);
g.setColor(Color.red);
g.fillOval(xPos,yPos,20,20);
if(mouseDown)
{
g.drawString("Keep trying!!!", width/2 - 40, height -10);
}
}
public void mouseMoved(MouseEvent e)
{
xPos = e.getX() - 50;
yPos = e.getY() - 50;
repaint();
}
public void mouseDragged(MouseEvent e)
{
xPos = e.getX() - 50;
yPos = e.getY() - 50;
repaint();
}
}
The toString method
Object
toString(): String
BankAccount
toString(): String
A toString method for the BankAccount class
public String toString()
{
return "Account Number: "
+ accountNumber
+ "\nAccount Name: "
+ accountName
+ "\nCurrent Balance: "
+ balance
+ "\n";
}
Account
public
class
Number:
RunAccount
001
{
Account
Name: Sarah Patel
Current
public Balance:
static void
0.0main(String[] args)
{
Account
BankAccount
Number:account1
002
= new BankAccount("001", "Sarah Patel");
Account
BankAccount
Name: Robinder
account2Grewel
= new BankAccount("002", "Robinder Grewel");
Current
System.out.println(account1);
Balance: 0.0
System.out.println(account2);
}
}
RUN