Presentation is loading. Please wait.

Presentation is loading. Please wait.

SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.

Similar presentations


Presentation on theme: "SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans."— Presentation transcript:

1 SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans

2 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.

3 Abstraction the idea of focussing on what an object does, without worrying about the detail of how it does it. 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. what how

4 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.

5 import javax.swing.*; public interface Attachable { public void attach(JFrame frameIn, int xPos, int yPos); } The Attachable interface 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.

6 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); } }

7 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); }

8 public void paint(Graphics g) { super.paint(g); logo.attach(this, xPos, yPos); } }......

9 public class LogoTester { public static void main(String[] args) { new LogoFrame(new CAndKLogo(), 3, 5); } } RUN

10 public class LogoTester2 { public static void main(String[] args) { new LogoFrame(new BAndBLogo(), 110, 5); } }

11 The RedCircle program

12 The methods of MouseMotionListener mouseMoved Specifies the behaviour that occurs when the mouse is moved when no button is depressed. mouseDragged Specifies the behaviour that occurs when the mouse is moved with the left-hand button depressed.

13 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.

14 The RedCircle class import java.awt.*; import javax.swing.*; import java.awt.event.*; public class RedCircle extends JFrame implements MouseMotionListener, MouseListener { private int xPos; private int yPos; private int width; private int height; private boolean mouseDown;

15 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); }

16 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); } }

17 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(); }

18 public void mousePressed(MouseEvent e) { mouseDown = true; repaint(); } public void mouseReleased(MouseEvent e) { mouseDown = false; repaint(); }

19 public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }

20 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.

21 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) { } }

22 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());

23 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;

24 class RedCircleAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) { mouseDown = true; repaint(); } public void mouseReleased(MouseEvent e) { mouseDown = false; repaint(); } }

25 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); }

26 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); }

27 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(); }

28 The toString method Object toString(): String BankAccount toString(): String

29 A toString method for the BankAccount class public String toString() { return "Account Number: " + accountNumber + "\nAccount Name: " + accountName + "\nCurrent Balance: " + balance + "\n"; }

30 public class RunAccount { public static void main(String[] args) { BankAccount account1 = new BankAccount("001", "Sarah Patel"); BankAccount account2 = new BankAccount("002", "Robinder Grewel"); System.out.println(account1); System.out.println(account2); } } RUN Account Number: 001 Account Name: Sarah Patel Current Balance: 0.0 Account Number: 002 Account Name: Robinder Grewel Current Balance: 0.0


Download ppt "SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans."

Similar presentations


Ads by Google