package core;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Main extends JFrame implements KeyListener
{
private static final long serialVersionUID = 1L;
/**
* Test-Class.
* A Key-Press emulating a Point of "Player 0".
*/
private static PlayerPoint[] playerPoint;
public Main()
{
super("Test");
playerPoint = new PlayerPoint[2];
playerPoint[0] = new PlayerPoint((byte)0, this);
playerPoint[1] = new PlayerPoint((byte)1, this);
this.addKeyListener(this);
this.setSize(640, 480);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.black);
playerPoint[0].draw(g);
playerPoint[1].draw(g);
}
public void keyPressed(KeyEvent e)
{
playerPoint[0].makePoint();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args)
{
new Main();
}
}
/////////////////////////////////////////////////////////////////
package core;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JFrame;
public class PlayerPoint extends Thread
{
private static final Point[] TEXT_COORDINATES = {
new Point(100, 100),
new Point(200, 200)
};
private static final String[] TEXT_STRINGS = {
"Player 0: Point is Make",
"Player 1: Point is Make"
};
private static final long DELAY_TIME = 50L;
private static final long POINT_WATCH_TIME = 1000L;
private byte id;
private boolean pointIsMake;
private boolean threadRun;
private JFrame jframe;
public PlayerPoint(byte id, JFrame jframe)
{
super("PlayerPoint");
this.id = id;
threadRun = true;
this.jframe = jframe;
this.start();
}
public void run()
{
while(threadRun)
{
try
{
if(pointIsMake)
{
Thread.sleep(POINT_WATCH_TIME);
pointIsMake = false;
jframe.repaint();
}
Thread.sleep(DELAY_TIME);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void draw(Graphics g)
{
if(pointIsMake)
g.drawString(TEXT_STRINGS[id], (int)TEXT_COORDINATES[id].getX(),
(int)TEXT_COORDINATES[id].getY());
}
public void makePoint()
{
pointIsMake = true;
jframe.repaint();
}
public void breakThread()
{
threadRun = false;
}
}