0% found this document useful (0 votes)
156 views

Race Track and Latest Java

This Java program creates a simple race track simulation with two cars racing around an image of a track. The program displays buttons and fields to allow the user to enter a bet amount and choose a car to race, and displays the current pot and bank amounts. It uses Swing components, images, and a custom JPanel subclass to render and animate the cars moving on the track.

Uploaded by

priyantha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Race Track and Latest Java

This Java program creates a simple race track simulation with two cars racing around an image of a track. The program displays buttons and fields to allow the user to enter a bet amount and choose a car to race, and displays the current pot and bank amounts. It uses Swing components, images, and a custom JPanel subclass to render and animate the cars moving on the track.

Uploaded by

priyantha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Race track Java program

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("Track.gif").getImage();
Image Car1 = new ImageIcon("Redcar.gif").getImage();
Image Car2 = new ImageIcon("Bluecar.gif").getImage();
animation = new RaceTrackPanel(this, Track, Car1, Car2);
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(700, 400);
f.show();
}
}
class RaceTrackPanel extends JPanel
{
RaceTrack holder;
Image Track, Car1, Car2;
int Xpos, Ypos;
public RaceTrackPanel(RaceTrack app, Image Track,
Image Car1, Image Car2)
{
this.Track = Track;
this.Car1 = Car1;
this.Car2 = Car2;
setSize(600,270);
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);
}
}
}//end class

Label Back ground

import java.awt.*;
import java.applet.*;
public class LabelBackground extends Applet
{
private Label lbl;
private TextField tfred;
private TextField tfgreen;
private TextField tfblue;
private Button b;
private Button btn;
public void init()
{
lbl=new Label(" ");
tfred=new TextField(5);
tfgreen=new TextField(5);
tfblue=new TextField(5);
btn=new Button("Change background Color");
BgColorHandler h=new BgColorHandler(lbl,tfred,tfgreen,tfblue);
btn.addActionListener(h); //This canbe used
add(tfred);
add(tfgreen);
add(tfblue);
add(btn);
add(lbl);
}
}
//<applet code="LabelBackground.class" Width=200 Height=200></applet>

BG Color

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class BgColor extends Applet implements ActionListener
{
private Button red;
private Button green;
public void init()
{
red=new Button("Red");
green=new Button("Green");
red.addActionListener(this);
green.addActionListener(this);
add(red);
add(green);
}
public void actionPerformed(ActionEvent e)
{
Object o;
o=e.getSource();
if(o==red)
setBackground(Color.red);
else if(o==green)
setBackground(Color.green);
}
}
//<applet code="BgColor.class" width=200 Height=200></applet>

Varshad
////////////////////////////////////////// Class A///////////////////////////////////////////
public class A
{
public int x=50;
public String name;
public String add;
public A()
{
name=" ";
add=" ";
System.out.println("A init(1)");
}
public A(String n, String a)
{
name=n;
add=a;
System.out.println("A init(2)");
}
public void display()
{
System.out.println("A");
}
}

//////////////////////////////////////////////Class B/////////////////////////////////////////////////////////
public class B extends A
{
public int x=20;
//shadows A's x
public int marks;
public B(String n, String a,int m)
{
//*super classes default constuctor*/
//super(n,a);
//name=n;
//add=a;

marks=m;
System.out.println("B init");
}
public void bb()
{
System.out.println(x);
System.out.println(super.x);
//accessing shadow variables
display();
//calling overriden method
super.display();
}
public void display()
{
System.out.println("B");
}
}

///////////////////////////Varhad/////////////////////////////////
public class varshad
{
public static void main(String[]ar)
{
B e=new B("a","b",45);
e.bb();
}
}

Java Race track And latest Java

www.alawathugoda.cjb.net
[email protected]

You might also like