|
|
Исходный текст программы MoveBox.java
|
Оглавление |
Назад
// ==========================================
// MoveBox.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.*;
import java.awt.*;
public class MoveBox extends Applet
{
int nxCurrent;
int nyCurrent;
Dimension dm;
final static int nBlockSize = 10;
// ============================================
// getAppletInfo
// ============================================
public String getAppletInfo()
{
return "Name: MoveBox";
}
// ============================================
// init
// ============================================
public void init()
{
dm = size();
nxCurrent = (dm.width / 2) - nBlockSize;
nyCurrent = (dm.height / 2) - nBlockSize;
setBackground(Color.yellow);
setForeground(Color.black);
}
// ============================================
// paint
// ============================================
public void paint(Graphics g)
{
g.setColor(Color.red);
g.setFont(new Font(
"Helvetica", Font.BOLD, 24));
g.drawString("Move it whith keyboard!",
10, 30);
DrawIt(g);
}
// ============================================
// DrawIt
// ============================================
void DrawIt(Graphics g)
{
g.setColor(Color.black);
g.fillRect(nxCurrent, nyCurrent,
2 * nBlockSize, 2 * nBlockSize);
}
// ============================================
// keyDown
// ============================================
public boolean keyDown(Event evt, int key)
{
switch(key)
{
case Event.LEFT:
{
if(nxCurrent > 0)
nxCurrent -= nBlockSize;
else
nxCurrent =
dm.width - nBlockSize * 2;
break;
}
case Event.RIGHT:
{
if(nxCurrent < dm.width - nBlockSize)
nxCurrent += nBlockSize;
else
nxCurrent = 0;
break;
}
case Event.DOWN:
{
if(nyCurrent < dm.height - nBlockSize)
nyCurrent += nBlockSize;
else
nyCurrent = 0;
break;
}
case Event.UP:
{
if(nyCurrent > 0)
nyCurrent -= nBlockSize;
else
nyCurrent =
dm.height - nBlockSize * 2;
break;
}
}
repaint();
return true;
}
// ============================================
// keyUp
// ============================================
public boolean keyUp(Event evt, int nKey)
{
return true;
}
}
|
|
|