bingoclient.java
来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 427 行
JAVA
427 行
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";
int port = 1234;
try
{
client = new MulticastConnection(address, port);
}
catch(Exception e)
{
}
}
public void start()
{
String msg = "";
while(true)
{
msg = client.recv();
if(msg.equals("Reset"))
{
for(int i = 0; i < 5; i++)
{
calledNumbers[i] = BINGO.charAt(i) + " ";
}
currCall = "Server calling new game...";
grid.reset();
firstPaint = true;
updateCalls = true;
repaint();
}
else
{
int n = Integer.parseInt(msg);
for(int i = 0; i < 5; i++)
{
if(n < i*15 + 16)
{
currCall = BINGO.charAt(i) + " " + n;
calledNumbers[i] = calledNumbers[i] + " " + n;
updateCalls = true;
repaint();
break;
}
}
}
try
{
Thread.sleep(5);
}
catch(InterruptedException e)
{
}
}
}
public void update(Graphics g)
{
paint(g);
}
protected boolean firstPaint = true;
protected boolean updateCalls = true;
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
if(firstPaint)
{
g2d.setPaint(Color.WHITE);
g2d.fill(new Rectangle2D.Double(0, 0, getSize().width, getSize().height));
g2d.setFont(font);
grid.paintAll(g2d, font);
g2d.setFont(font);
for(int i = 0; i < 5; i++)
{
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout = new TextLayout(""+BINGO.charAt(i), font, frc);
Rectangle2D fontBounds = layout.getBounds();
double x = grid.getBounds().getX() + (i * BingoGrid.TILE_WIDTH);
double y = grid.getBounds().getY() - fontBounds.getHeight()/2;
g2d.drawString(""+BINGO.charAt(i),
(int) (x + BingoGrid.TILE_WIDTH/2 - fontBounds.getWidth()/2), (int) y);
}
firstPaint = false;
}
if(updateCalls)
{
g2d.setFont(calledFont);
updateCalls = false;
int x = (int) grid.getBounds().getX() + 6 * (int) BingoGrid.TILE_WIDTH;
int y = (int) grid.getBounds().getY() + (int) BingoGrid.TILE_HEIGHT;
for(int i = 0; i < 5; i++)
{
g2d.drawString(calledNumbers[i], x, y);
y += 40;
}
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout = new TextLayout(currCall, font, frc);
Rectangle2D fontBounds = layout.getBounds();
x = (int) (getSize().width/2 - fontBounds.getWidth()/2);
y = (int) grid.getBounds().getY() + 6 * (int) BingoGrid.TILE_HEIGHT;
Rectangle2D clearRect = new Rectangle2D.Double(0, 315, getSize().width, 50);
g2d.setPaint(Color.WHITE);
g2d.fill(clearRect);
g2d.setPaint(Color.BLACK);
g2d.setFont(font);
g2d.drawString(currCall, x, y);
}
g2d.setFont(font);
grid.updateDirty(g2d);
}
public void actionPerformed(ActionEvent e)
{
// bingo按钮应该已经触发了这个事件;不过这个特性现在还没有实现
System.out.println("'Bingo' button not implemented");
}
public void mouseClicked(MouseEvent e)
{
if(grid.contains(e.getPoint()))
{
repaint();
}
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public static void main(String[] args)
{
Applet a = new BingoClient();
a.init();
Frame f = new Frame("Java BINGO Client");
f.setSize(800, 420);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
f.add(a);
f.show();
a.start();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?