In my code I need to use if statements depending on what the user inputs to draw a certain amount of pawns. so if the user enters 4 pawns, i need three of them to be in a random spot. So i need to generate random x and y coordinated between the size of my canvas. I know i need to use a random number generator but i'm not exactly sure how it works. Please help me out. Thanks Here is my code...
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ChessSet extends JPanel
{
public int numPawns;
public int numBishops;
public static void main()
{
JFrame ourFrame = new JFrame();
ourFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
ourFrame.setSize(1000,1000);
ChessSet ourChessSet = new ChessSet();
Scanner key = new Scanner( System.in );
System.out.println( "How many pawns would you like? (0-4): " );
ourChessSet.numPawns = key.nextInt();
System.out.println( "How many bishops would you like? (0-4): " );
ourChessSet.numBishops = key.nextInt();
ourFrame.add( ourChessSet );
ourFrame.setBackground( Color.white );
ourFrame.setVisible(true);
}
public void paint( Graphics canvas )
{
ChessSet inst1 = new ChessSet();
this.drawPawn( canvas, 100, 140, Color.black );
this.drawKing( canvas, 375, 50, Color.black );
this.drawRook( canvas, 600, 110, Color.black );
this.drawQueen( canvas, 100, 535, Color.black );
this.drawKnight( canvas, 350,480, Color.black );
this.drawBishop( canvas, 700, 480, Color.black );
canvas.drawString( "King 1", 10, 10 );
canvas.drawString( "Rook 1", 10, 30 );
canvas.drawString( "Queen 1", 10, 20 );
canvas.drawString( "Pawn " + numPawns, 100, 10 );
canvas.drawString( "Bishop " + numBishops, 100, 30 );
canvas.drawString( "Knight 1", 100, 20 );
if( numPawns == 1 )
{
this.drawPawn( canvas, 100, 140, Color.black );
}
else if( numPawns == 2 )
{
this.drawPawn( canvas, 100, 140, Color.black );
this.drawPawn( canvas, 800, 800, Color.black );
}
else if( numPawns == 3 )
{
}
else if( numPawns == 4 )
{
}
else
{
}
if( numBishops == 1 )
{
this.drawBishop( canvas, 100, 140, Color.cyan );
}
else if( numPawns == 2 )
{
this.drawBishop( canvas, 100, 140, Color.black );
}
else if( numBishops == 3 )
{
}
else if( numBishops == 4 )
{
}
else
{
}
}
public void drawPawn( Graphics canvas, int x, int y, Color pawnColor )
{
canvas.setColor( pawnColor );
canvas.fillOval( x-10, y-45, 120, 120 );//top oval
canvas.fillRect( x+0, y+50, 100, 150 );//middle rect
canvas.fillRoundRect( x-35, y+200, 170, 70, 10, 10 );//base rect
canvas.setColor( Color.white );
canvas.fillOval( x-25 , y+75, 50, 125 );//oval out of middle
canvas.fillOval( x+75, y+75, 50, 125 );//oval out of middle
}
public void drawKing( Graphics canvas, int x, int y, Color kingColor )
{
canvas.setColor( kingColor );
canvas.setColor( Color.black );
canvas.fillRect( x-60, y+180, 210, 120 );//seconds rect from the bottom
canvas.setColor( Color.white );
canvas.fillOval( x-90, y+165, 60, 120 );//ovals out of second from bot
canvas.fillOval( x+120, y+165, 60, 120 );//ovals out of second from bot
canvas.setColor( Color.black );
canvas.fillRoundRect( x-90, y+300, 270, 60, 10, 10 );//base rect
canvas.fillRect( x+30, y-0 , 30, 90 );//cross rect
canvas.fillRect( x+0, y+30, 90, 30 );//cross rect
canvas.fillRect( x-15, y+90, 120, 60 );//first rect from top
canvas.fillRect( x-15, y+150, 120, 30 );//first rect from top
canvas.setColor( Color.white );
canvas.fillOval( x-30, y+30, 30, 120 );//oval out of top rect
canvas.fillOval( x+90, y+30, 30, 120 );//oval out of top rect
canvas.setColor( Color.black );
}
public void drawRook( Graphics canvas, int x, int y, Color rookColor )
{
canvas.setColor( rookColor );
canvas.setColor( Color.black );
canvas.fillRect( x+15, y+90, 150, 180 );//middle rect
canvas.setColor( Color.white );
canvas.fillOval( x-30, y+30, 60, 240);//ovals on side of bottom rect
canvas.fillOval( x+150, y+30, 60, 240);//ovals on bottom of rect
canvas.setColor( Color.black );
canvas.fillRoundRect( x+0, y+0, 180, 90, 20, 20);//top rec
canvas.fillRoundRect( x+0, y+240, 180, 60, 10, 10);//base rectangle
canvas.setColor( Color.white );
canvas.fillRect( x+40, y+0, 15, 30);//next 3 lines are slits in top
canvas.fillRect( x+80, y+0, 15, 30);
canvas.fillRect( x+120, y+0, 15, 30);
}
public void drawQueen( Graphics canvas, int x, int y, Color queenColor )
{
canvas.setColor( queenColor );
canvas.setColor( Color.black );
canvas.fillRect( x+0, y+0, 150, 240 ); //main body rectangle
canvas.fillOval(x-30, y+180, 60, 60);//circles added to base
canvas.fillOval(x+120, y+180, 60, 60);//circles added to base
canvas.fillOval(x+55, y-45, 40, 40);//circle on top
canvas.setColor( Color.white );
canvas.fillOval( x-45, y-30, 90, 210);//ovals out of middle rect
canvas.fillOval( x+105, y-30, 90, 210);//ovals out of middle rect
canvas.setColor( Color.black );
canvas.fillRoundRect( x+30, y+60, 90, 30, 10, 10 );//rect in middle of body
}
public void drawKnight( Graphics canvas, int x, int y, Color knightColor )
{
canvas.setColor( Color.black );
Polygon tri = new Polygon();
tri.addPoint( x+0, y+75 );
tri.addPoint( x+210, y+75 );
tri.addPoint( x+105, y+165 );
canvas.fillPolygon( tri );
canvas.fillRoundRect( x-0, y+240, 210, 60, 10, 10 );//base rectangle
canvas.fillOval( x+30, y+130, 60, 120 );//left oval
canvas.fillOval( x+120, y+130, 60, 120 );//right oval
canvas.setColor( Color.white );
canvas.fillOval( x+100, y+105, 15, 15 );
canvas.fillOval( x+100, y+135, 15, 15 );
}
public void drawBishop( Graphics canvas, int x, int y, Color bishopColor )
{
canvas.setColor( Color.black );
canvas.fillOval( x+0, y+0, 90, 90 );//head
canvas.fillRoundRect( x-30, y+90, 150, 30, 15, 15 );//shoulders
canvas.fillRect( x-30, y+120, 150, 120 );//body
canvas.fillOval( x+30, y-30, 30, 30 );
canvas.setColor( Color.white );
canvas.fillOval( x-75, y+120 , 90, 120 );//ovals out of body
canvas.fillOval( x+75, y+120 , 90, 120 );//ovals out of body
canvas.fillRect( x+20, y+0, 15, 50 );//slit out of head
canvas.setColor( Color.black );
canvas.fillRoundRect( x-60, y+240, 210, 60, 10, 10 );//base rect
}
}