21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 15.1 Cyberspace invaders<br />

15.2 Encapsulation 201<br />

the screen – an alien, a laser, a bomb and the user. We will represent each of these as<br />

an object.<br />

We will represent the alien as an object, but to create an object, we have to write a<br />

class (a blueprint <strong>for</strong> any number of aliens). In Java, we write the class as follows:<br />

class Alien {<br />

private int x, y;<br />

private int size;<br />

private ImageIcon alienImage;<br />

public Alien(int newX, int newY, int newSize) {<br />

x = newX;<br />

y = newY;<br />

size = newSize:<br />

alienImage = new ImageIcon("c:/alien.jpg");<br />

}<br />

public void display(JPanel panel) {<br />

Graphics paper = panel.getGraphics();<br />

alienImage.paintIcon(panel, paper, x, y);<br />

}<br />

>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!