1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
package game.ch5;
/*************************************************************
* Beginning Java Game Programming, 2nd Edition
* by Jonathan S. Harbour
* DrawImage program
*************************************************************/
import java.awt.*;
import java.applet.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.*;
public class DrawImage extends Applet implements KeyListener{
//image variable
private Image image;
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) { }
return url;
}
//applet init event
public void init() {
image = getImage(getURL("castle.png"));
addKeyListener(this);
}
//applet paint event
public void paint(Graphics g) {
//create an instance of Graphics2D
Graphics2D g2d = (Graphics2D) g;
//fill the background with black
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getSize().width, getSize().height);
//draw the image
g2d.drawImage(image, 0, 0, this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
//***************************************************
//*********** MON CODE **************************
if(e.getKeyChar()=='+')
{
System.out.println("ok");
// int height = image.getHeight(this);
// int width = image.getWidth(this);
// width+=10;
// height+=10;
int height = image.getHeight(this);
height+=50;
int hauteur = this.getSize().height;
int largeur = this.getSize().width;
hauteur+=500;
largeur+=500;
repaint();
}
//***************************************************
}
@Override
public void keyReleased(KeyEvent e) {
}
} |
Partager