import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
public class RaceTrack extends JFrame
{
private RaceTrackPanel animation;
private JLabel betLabel, potLabel, bankLabel, carLabel;
private JTextField betField, carField, potField, bankField, winnerField;
private JButton goButton;
private JPanel p;
public RaceTrack( )
{
Image Track = new ImageIcon("C:/Users/Daniel/Pictures/Track.gif").getImage();
Image Car1 = new ImageIcon("C:/Users/Daniel/Pictures/Red Car.png").getImage();
Image Car2 = new ImageIcon("C:/Users/Daniel/Pictures/Blue Car.png").getImage();
Image Car3 = new ImageIcon("C:/Users/Daniel/Pictures/Green Car.png").getImage();
animation = new RaceTrackPanel(this, Track, Car1, Car2, Car3);
Container c = getContentPane();
c.setLayout(new BorderLayout( ));
p = new JPanel( );
p.setBackground(Color.lightGray);
GridBagLayout grid = new GridBagLayout();
p.setLayout(grid);
betLabel = new JLabel("Enter Bet");
carLabel = new JLabel("Choose Car");
potLabel = new JLabel("Pot");
bankLabel = new JLabel("Bank");
betField = new JTextField(5);
carField = new JTextField(2);
potField = new JTextField(5);
bankField = new JTextField(7);
winnerField = new JTextField(20);
potField.setEditable(false);
bankField.setEditable(false);
goButton = new JButton("Start Race");
GridBagConstraints gbc = new GridBagConstraints( );
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
gblAdd(goButton,grid,gbc,0,0,4,1);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
gblAdd(betLabel,grid,gbc,0,1,1,1);
gbc.anchor = GridBagConstraints.WEST;
gblAdd(betField,grid,gbc,1,1,1,1);
gbc.anchor = GridBagConstraints.EAST;
gblAdd(carLabel,grid,gbc,2,1,1,1);
gbc.anchor = GridBagConstraints.WEST;
gblAdd(carField,grid,gbc,3,1,1,1);
gbc.anchor = GridBagConstraints.EAST;
gblAdd(potLabel,grid,gbc,0,2,1,1);
gbc.anchor = GridBagConstraints.WEST;
gblAdd(potField,grid,gbc,1,2,1,1);
gbc.anchor = GridBagConstraints.EAST;
gblAdd(bankLabel,grid,gbc,2,2,1,1);
gbc.anchor = GridBagConstraints.WEST;
gblAdd(bankField,grid,gbc,3,2,1,1);
gbc.anchor = GridBagConstraints.WEST;
gblAdd(winnerField,grid,gbc,0,3,4,1);
betField.setText("");
potField.setText("100");
bankField.setText("1000");
c.add("Center", animation);
c.add("South", p);
}
private void gblAdd(Component c, GridBagLayout
grid,GridBagConstraints gbc, int x, int y, int w, int h)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
p.add(c);
}
public static void main(String[] args)
{
JFrame f = new RaceTrack( );
f.setSize(600,480 );
f.show();
}}
class RaceTrackPanel extends JPanel
{
RaceTrack holder;
Image Track, Car1, Car2, Car3;
int Xpos, Ypos;
public RaceTrackPanel(RaceTrack app, Image Track,
Image Car1, Image Car2, Image Car3)
{
this.Track = Track;
this.Car1 = Car1;
this.Car2 = Car2;
this.Car3 = Car3;
setSize(600,250);
setBackground(Color.darkGray);
holder = app;
Xpos = 30;
Ypos = 30;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int w;
int h;
w = Track.getWidth(this);
h = Track.getHeight(this);
if ((w>0) && (h>0))
{
g.drawImage(Track, Xpos, Ypos, this);
}
w = Car1.getWidth(this);
h = Car2.getHeight(this);
if ((w>0) && (h>0))
{
g.drawImage(Car1, Xpos, Ypos +25, this);
g.drawImage(Car2, Ypos, Ypos +130, this);
g.drawImage(Car3, Ypos, Ypos +235, this);
}}}