-
Notifications
You must be signed in to change notification settings - Fork 0
/
javagame.java
95 lines (86 loc) · 1.93 KB
/
javagame.java
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
85
86
87
88
89
90
91
92
93
94
95
package javagame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class javagame extends JFrame{
int x,y;
private Image dbImage;
private Graphics dbg;
Font font= new Font("Arial", Font.BOLD | Font.ITALIC, 30);
Image face;
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode= e.getKeyCode();
if(keyCode == e.VK_LEFT){
if(x<=0){
x=0;
}else{
x=x-20;
}
}
if(keyCode == e.VK_RIGHT){
if(x>=480){
x=480;
}else{
x=x+20;
}
}
if(keyCode == e.VK_UP){
if(y<=20){
y=20;
}else{
y=y-20;
}
}
if(keyCode == e.VK_DOWN){
if(y>=480){
y=480;
}else{
y=y+20;
}
}
}
public void keyRelease(KeyEvent e){
}
}
public javagame(){
//load images
ImageIcon i= new ImageIcon("C:/Users/Administrator/Downloads/icon1.png");
face= i.getImage();
//game properties
addKeyListener(new AL());
setTitle("Java Game");
setSize(500,500);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x=150;
y=150;
setBackground(Color.BLUE);
}
public void paint(Graphics g){
dbImage= createImage(getWidth(), getHeight());
dbg= dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
//g.drawString("Hello Java", 75,100);\
//g.setFont(font);
g.setColor(Color.MAGENTA);
//g.drawString("Hello World!", 50, 50);
//g.setColor(Color.cyan);
//g.fillOval(x,y,15,15);
g.drawImage(face, x, y, this);
repaint();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new javagame();
}
}