001.
import java.applet.*;
002.
import java.awt.event.*;
003.
import java.awt.*;
004.
import javax.swing.*;
005.
006.
public class testkey extends JApplet implements KeyListener,ActionListener {
007.
private ImageIcon image1, image2, image3;
008.
private int width, height;
009.
private JPanel guiPanel, graphicsPanel;
010.
private JButton startBtn, stopBtn;
011.
private int xMin, xMax, yMin, yMax,xMin1, xMax1, yMin1, yMax1;
012.
private int x, y,x1,y1 ,size,size1, xSpeed, ySpeed, xSpeed1, ySpeed1,p=0;
013.
Color fgcolor;
014.
private char typeShape=
'C'
;
015.
016.
Timer swTimer;
017.
018.
public void init () {
019.
020.
Container c = getContentPane();
021.
c.setLayout(
new
FlowLayout());
022.
startBtn =
new
JButton(
"Play"
);
023.
startBtn.addActionListener(
this
);
024.
startBtn.addKeyListener(
this
);
025.
c.add(startBtn);
026.
stopBtn =
new
JButton(
"Stop"
);
027.
stopBtn.addActionListener(
this
);
028.
stopBtn.addKeyListener(
this
);
029.
c.add(stopBtn);
030.
width = 480; height = 320;
031.
xMin = 50; xMax = 350;
032.
yMin = 50; yMax = 90;
033.
xSpeed = 1; ySpeed = 1;
034.
035.
x = 50; y = 50; size = 40;
036.
fgcolor=Color.BLUE;
037.
image1 =
new
ImageIcon( getClass().getResource(
"/images/htb-1.png"
) );
038.
swTimer =
new
Timer(10,
this
);
039.
addKeyListener(
this
);
040.
}
041.
public void paint(Graphics g) {
042.
super
.paint(g);
043.
g.drawLine(50, 70, 350, 70);
044.
g.drawLine(350, 70, 350, 200);
045.
g.drawString(
"xspeed : "
+xSpeed,80,80);
046.
g.drawString(
"x : "
+x,180,180);
047.
g.drawString(
"y : "
+y,190,190);
048.
if
(typeShape==
'R'
){
049.
g.setColor(fgcolor);
050.
g.fillRect(x,y,size,size);
051.
g.setColor(Color.BLACK);
052.
g.drawRect(x,y,size,size);
053.
}
054.
055.
056.
057.
058.
059.
else
{
060.
g.setColor(fgcolor);
061.
g.fillOval(x,y,size,size);
062.
g.setColor(Color.BLACK);
063.
g.drawOval(x,y,size,size);
064.
}
065.
066.
}
067.
068.
public void keyPressed(KeyEvent event) {
069.
if
( event.getKeyChar() ==
'p'
)
070.
{
071.
swTimer.start();
072.
}
073.
else
if
( event.getKeyChar() ==
's'
)
074.
{
075.
swTimer.stop();
076.
}
077.
if
(event.getKeyChar()==
'c'
) typeShape=
'C'
;
078.
if
(event.getKeyChar()==
'r'
) typeShape=
'R'
;
079.
if
(event.getKeyChar()==
'l'
) typeShape=
'L'
;
080.
081.
}
082.
083.
084.
public void actionPerformed(ActionEvent e) {
085.
if
(e.getSource() == startBtn) { swTimer.start(); }
086.
else
if
(e.getSource() == stopBtn) { swTimer.stop(); }
087.
else
{
088.
move();
089.
repaint();
090.
}
091.
}
092.
public void move() {
093.
x = x + xSpeed;
094.
y = y + ySpeed;
095.
096.
097.
098.
if
(x < xMin) {
099.
x = xMin;
100.
xSpeed = -xSpeed;
101.
}
102.
103.
104.
else
if
(x+size > xMax) {
105.
x = xMax - size;
106.
xSpeed = -xSpeed;
107.
}
108.
109.
if
(y < yMin) {
110.
y = yMin;
111.
ySpeed = -ySpeed;
112.
}
113.
else
if
(y+size > yMax) {
114.
y = yMax - size;
115.
ySpeed = -ySpeed;
116.
}
117.
118.
}
119.
120.
121.
public void keyReleased(KeyEvent event){
122.
repaint();
123.
}
124.
125.
public void keyTyped(KeyEvent event){
126.
Graphics g = getGraphics();
127.
g.drawString(
"keyTyped : "
+event.getKeyChar(),50,50);
128.
}
129.
}