Using JAVA display current status like major, position, money, and name. public
ID: 3829685 • Letter: U
Question
Using JAVA display current status like major, position, money, and name.
public class Player {
private static int currentPlayer;
private String playerName;
private int position;
private int money;
public Player()
{
this.playerName = "";
this.position=position;
this.money = 200;
}
public Player(String name)
{
this.playerName= name;
}
public static int getCurrentPlayer() {
return currentPlayer;
}
public static void setCurrentPlayer(int currentPlayer) {
Player.currentPlayer = currentPlayer;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public void setPlayerTotalMoney(double playerMoney) {
// TODO Auto-generated method stub
}
public int getPlayerTotalMoney() {
// TODO Auto-generated method stub
return 0;
}
public int placement[][] =
{
//Positions for the player as he travels around the board
{ 10, 450 },
{ 10, 369 },
{ 10, 336 },
{ 10, 303 },
{ 10, 269 },
{ 10, 236 },
{ 10, 203 },
{ 10, 169 },
{ 10, 136 },
{ 10, 103 },
{ 10, 10 },
{ 103, 10 },
{ 136, 10 },
{ 169, 10 },
{ 203, 10 },
{ 236, 10 },
{ 269, 10 },
{ 303, 10 },
{ 336, 10 },
{ 369, 10 },
{ 426, 10 },
{ 426, 103 },
{ 426, 136 },
{ 426, 169 },
{ 426, 203 },
{ 426, 236 },
{ 426, 269 },
{ 426, 303 },
{ 426, 336 },
{ 426, 369 },
{ 426, 403 },
{ 369, 426 },
{ 336, 426 },
{ 303, 426 },
{ 269, 426 },
{ 236, 426 },
{ 203, 426 },
{ 169, 426 },
{ 136, 426 },
{ 103, 426 }
};
public void DrawPlayer( Graphics g )
{
//Draw the player according to the position on the board
int y;
if( currentPlayer == 1 )
{
y = 510;
g.setColor( Color.blue );
if( inJail )
{
g.fillOval( 34, 34, 15, 15 );
}
else
{
g.fillOval( placement[position][0],placement[position][1],15, 15 );
}
if (currentPlayer == 1)
{
g.setFont( new Font( "Arial", Font.BOLD, 16 ) );
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(playerName);
g.drawString( playerName, 50, y );
w = fm.stringWidth("$"+money);
g.drawString( "$" + money, 50, y+20 );
}
}
else
{
y = 510;
g.setColor( Color.red );
if( inJail )
g.fillOval( 39, 39, 15, 15 );
else
g.fillOval( placement[position][0]+9,placement[position][1]+9,15, 15 );
if (currentPlayer == 2)
{
g.setFont( new Font( "Arial", Font.BOLD, 16 ) );
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(playerName);
g.drawString( playerName, 300, y );
w = fm.stringWidth("$"+money);
g.drawString( "$" + money, 300, y+20 );
}
}
}
}
Explanation / Answer
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
public class Player {
private static int currentPlayer;
private String playerName;
private int position;
private int money;
public Player()
{
this.playerName = "";
this.position=position;
this.money = 200;
}
public Player(String playerName, int position, int money) {
super();
this.playerName = playerName;
this.position = position;
this.money = money;
}
public int[][] getPlacement() {
return placement;
}
public void setPlacement(int[][] placement) {
this.placement = placement;
}
public boolean isInJail() {
return inJail;
}
public void setInJail(boolean inJail) {
this.inJail = inJail;
}
public static int getCurrentPlayer() {
return currentPlayer;
}
public static void setCurrentPlayer(int currentPlayer) {
Player.currentPlayer = currentPlayer;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public void setPlayerTotalMoney(double playerMoney) {
// TODO Auto-generated method stub
}
public int getPlayerTotalMoney() {
// TODO Auto-generated method stub
return 0;
}
public int placement[][] =
{
//Positions for the player as he travels around the board
{ 10, 450 },
{ 10, 369 },
{ 10, 336 },
{ 10, 303 },
{ 10, 269 },
{ 10, 236 },
{ 10, 203 },
{ 10, 169 },
{ 10, 136 },
{ 10, 103 },
{ 10, 10 },
{ 103, 10 },
{ 136, 10 },
{ 169, 10 },
{ 203, 10 },
{ 236, 10 },
{ 269, 10 },
{ 303, 10 },
{ 336, 10 },
{ 369, 10 },
{ 426, 10 },
{ 426, 103 },
{ 426, 136 },
{ 426, 169 },
{ 426, 203 },
{ 426, 236 },
{ 426, 269 },
{ 426, 303 },
{ 426, 336 },
{ 426, 369 },
{ 426, 403 },
{ 369, 426 },
{ 336, 426 },
{ 303, 426 },
{ 269, 426 },
{ 236, 426 },
{ 203, 426 },
{ 169, 426 },
{ 136, 426 },
{ 103, 426 }
};
private boolean inJail;
public void DrawPlayer( Graphics g )
{
//Draw the player according to the position on the board
int y;
if( currentPlayer == 1 )
{
y = 510;
g.setColor( Color.blue );
if( inJail )
{
g.fillOval( 34, 34, 15, 15 );
}
else
{
g.fillOval( placement[position][0],placement[position][1],15, 15 );
}
if (currentPlayer == 1)
{
g.setFont( new Font( "Arial", Font.BOLD, 16 ) );
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(playerName);
g.drawString( playerName, 50, y );
w = fm.stringWidth("$"+money);
g.drawString( "$" + money, 50, y+20 );
}
}
else
{
y = 510;
g.setColor( Color.red );
if( inJail )
g.fillOval( 39, 39, 15, 15 );
else
g.fillOval( placement[position][0]+9,placement[position][1]+9,15, 15 );
if (currentPlayer == 2)
{
g.setFont( new Font( "Arial", Font.BOLD, 16 ) );
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(playerName);
g.drawString( playerName, 300, y );
w = fm.stringWidth("$"+money);
g.drawString( "$" + money, 300, y+20 );
}
}
}
public static void main(String [] args){
Player pl = new Player("Kevin Pete", 300, 80);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.