|
|
Исходный текст программы MouseBox.java
|
Оглавление |
Назад
// ==========================================
// MouseBox.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class MouseBox extends Applet
implements MouseListener
{
Dimension dm;
int nxBlockSize;
int nyBlockSize;
static final int nBlocks = 10;
boolean[][] bState;
int[][] nClickCount;
int[][] nMod;
// ============================================
// init
// ============================================
public void init()
{
int i, j;
dm = getSize();
nxBlockSize = dm.width / nBlocks;
nyBlockSize = dm.height / nBlocks;
bState = new boolean[nBlocks][nBlocks];
nClickCount = new int[nBlocks][nBlocks];
nMod = new int[nBlocks][nBlocks];
for(i = 0; i < nBlocks; i++)
{
for(j = 0; j < nBlocks; j++)
{
bState[i][j] = false;
nClickCount[i][j] = 0;
nMod[i][j] = 0;
}
}
setBackground(Color.yellow);
setForeground(Color.black);
this.addMouseListener(this);
}
// ============================================
// getAppletInfo
// ============================================
public String getAppletInfo()
{
return "Name: MouseBox";
}
// ============================================
// paint
// ============================================
public void paint(Graphics g)
{
g.setColor(Color.red);
for(int x = 0; x <= nBlocks; x++)
{
g.drawLine(x * nxBlockSize, 0,
x * nxBlockSize, dm.height);
}
for(int y = 0; y <= nBlocks; y++)
{
g.drawLine(0, y * nyBlockSize,
dm.width, y * nyBlockSize);
}
for(int x = 0; x < nBlocks; x++)
{
for(int y = 0; y < nBlocks; y++)
{
if(nClickCount[x][y] == 2)
g.setColor(Color.red);
else if(bState[x][y])
{
if(nClickCount[x][y] == 2)
g.setColor(Color.red);
else
{
if(nMod[x][y] == InputEvent.META_MASK)
g.setColor(Color.black);
else
g.setColor(Color.green);
}
}
else
g.setColor(Color.yellow);
g.fillRect(
x * nxBlockSize + 1,
y * nyBlockSize + 1,
nxBlockSize - 1, nyBlockSize - 1);
}
}
}
// ============================================
// mousePressed
// ============================================
public void mousePressed(MouseEvent e)
{
int xClick = e.getX() / nxBlockSize;
int yClick = e.getY() / nyBlockSize;
bState[xClick][yClick] =
!bState[xClick][yClick];
nClickCount[xClick][yClick] =
e.getClickCount();
nMod[xClick][yClick] = e.getModifiers();
repaint();
}
// ============================================
// mouseReleased
// ============================================
public void mouseReleased(MouseEvent e)
{
}
// ============================================
// mouseClicked
// ============================================
public void mouseClicked(MouseEvent e)
{
}
// ============================================
// mouseEntered
// ============================================
public void mouseEntered(MouseEvent e)
{
}
// ============================================
// mouseExited
// ============================================
public void mouseExited(MouseEvent e)
{
}
}
|
|
|