【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-BingoClient.java,希望本篇文章对您学习有所帮助。
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
class BingoTile extends Object
{
protected Rectangle2D bounds;
protected Point2D textOrigin;
protected int value;
protected boolean filled;
protected Font font;
public static Color fillColor;
public BingoTile(Rectangle2D r, Font f)
{
bounds = r;
value = 0;
font = f;
filled = false;
textOrigin = null;
}
public void paint(Graphics2D g2d)
{
if(textOrigin == null)
{
// 获得Graphics2D 容器的FontRenderContext
FontRenderContext frc = g2d.getFontRenderContext();
// 使用上面的FontRenderContext,获取消息和字体的布局
TextLayout layout = new TextLayout(""+value, font, frc);
// 得到布局的边界
Rectangle2D fontBounds = layout.getBounds();
textOrigin = new Point2D.Double(bounds.getX() + bounds.getWidth()/2 - fontBounds.getWidth()/2,
bounds.getY() + bounds.getHeight()/2 + fontBounds.getHeight()/2);
}
if(filled)
{
g2d.setPaint(fillColor);
}
else
{
g2d.setPaint(g2d.getBackground());
}
g2d.fill(bounds);
g2d.setPaint(Color.BLACK);
g2d.draw(bounds);
g2d.drawString(""+value, (int) textOrigin.getX(), (int) textOrigin.getY());
}
public void setValue(int n) { value = n; }
public int getValue() { return value; }
public boolean contains(Point2D p)
{
return bounds.contains(p);
}
public void reset()
{
filled = false;
textOrigin = null;
}
public void toggle()
{
filled = !filled;
}
}
class BingoGrid extends Object
{
public static final double TILE_WIDTH = 50;
public static final double TILE_HEIGHT = 50;
protected Rectangle2D bounds;
protected BingoTile[][] tiles;
protected BingoTile dirty;
public BingoGrid(Point2D upperLeft, Font font, Color fillColor)
{
bounds = new Rectangle2D.Double(upperLeft.getX(), upperLeft.getY(), 5*TILE_WIDTH, 5*TILE_HEIGHT);
BingoTile.fillColor = fillColor;
dirty = null;
tiles = new BingoTile[5][5];
double x = 0;
double y = bounds.getY();
Random random = new Random();
for(int i = 0; i < 5; i++)
{
x = bounds.getX();
for(int j = 0; j < 5; j++)
{
tiles[i][j] = new BingoTile(new Rectangle2D.Double(x, y, TILE_WIDTH, TILE_HEIGHT),
font);
x += TILE_WIDTH;
}
y += TILE_HEIGHT;
}
int value = 0;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
boolean unique = false;
while(!unique)
{
unique = true;
value = 1+i*15+random.nextInt(15);
for(int k = 0; k < 5; k++)
{
if(tiles[k][i].getValue() == value)
{
unique = false;
}
}
}
//System.out.println("Setting " + j + " " + i);
tiles[j][i].setValue(value);
}
}
}
public Rectangle2D getBounds() { return bounds; }
public boolean contains(Point2D p)
{
if(! bounds.contains(p)) return false;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(tiles[i][j].contains(p))
{
tiles[i][j].toggle();
dirty = tiles[i][j];
return true;
}
}
}
return false;
}
public void paintAll(Graphics2D g2d, Font font)
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
tiles[i][j].paint(g2d);
}
}
}
public void reset()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
tiles[i][j].reset();
}
}
int value = 0;
Random random = new Random();
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
boolean unique = false;
while(!unique)
{
unique = true;
value = 1+i*15+random.nextInt(15);
for(int k = 0; k < 5; k++)
{
if(tiles[k][i].getValue() == value)
{
unique = false;
}
}
}
tiles[j][i].setValue(value);
}
}
}
public void updateDirty(Graphics2D g2d)
{
if(dirty != null)
{
dirty.paint(g2d);
dirty = null;
}
}
}
public class BingoClient extends Applet implements ActionListener, MouseListener
{
protected MulticastConnection client;
protected Font font;
protected Font calledFont;
protected BingoGrid grid;
protected String currCall;
protected String[] calledNumbers;
protected final String BINGO = "BINGO";
protected Button bingo;
public void init()
{
font = new Font("Helvetica", Font.PLAIN, 30);
calledFont = new Font("Helvetica", Font.PLAIN, 16);
grid = new BingoGrid(new Point2D.Double(50, 50), font, Color.ORANGE);
addMouseListener(this);
calledNumbers = new String[5];
for(int i = 0; i < 5; i++)
{
calledNumbers[i] = BINGO.charAt(i) + " ";
}
currCall = " ";
setLayout(new BorderLayout());
Panel p = new Panel();
bingo = new Button("Bingo!");
bingo.addActionListener(this);
p.add(bingo);
add(p, BorderLayout.SOUTH);
String address = "224.0.0.21";[b