Graphics color in java applet
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class Racing extends JApplet
implements ActionListener
{
private JTextField jtfRedCarSpeed = new JTextField(4);
private JTextField jtfGreenCarSpeed = new JTextField(4);
private Racing.RaceCar RedCar = new Racing.RaceCar();
private Racing.RaceCar GreenCar = new Racing.RaceCar();
public Racing() {
JPanel panel1 = new JPanel();
panel1.add(new JLabel("David's Car : "));
panel1.add(this.jtfRedCarSpeed);
panel1.add(new JLabel("Radwa's Car : "));
panel1.add(this.jtfGreenCarSpeed);
JPanel panel2 = new JPanel(new GridLayout(4, 1));
panel2.add(this.RedCar);
panel2.add(this.GreenCar);
add(panel1, "North");
add(panel2, "Center");
this.jtfRedCarSpeed.addActionListener(this);
this.jtfGreenCarSpeed.addActionListener(this);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
JApplet applet = new Racing();
frame.add(applet);
frame.setTitle("Car Racing");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.jtfRedCarSpeed) {
this.RedCar.setSpeed(Integer.parseInt(this.jtfRedC arSpeed.getText()));
}
else if (e.getSource() == this.jtfGreenCarSpeed)
{
this.GreenCar.setSpeed(Integer.parseInt(this.jtfGr eenCarSpeed.getText()));
}
}
class RaceCar extends JPanel
implements ActionListener
{
private int RBase = 0;
private int GBase = 0;
private Timer timer = new Timer(10, this);
public RaceCar() {
setBorder(BorderFactory.createLineBorder(Color.BLA CK));
this.timer.start();
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void setSpeed(int speed) {
this.timer.setDelay(speed);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int RedCar = getHeight();
if (this.RBase > getWidth()) {
this.RBase = -20;
}
else {
this.RBase += 1;
}
int GreenCar = getHeight();
if (this.GBase > getWidth()) {
this.GBase = -20;
}
else {
this.GBase += 1;
}
//red color
g.setColor(Color.red);
g.fillOval(this.RBase + 10, RedCar - 10, 10, 10);
g.fillOval(this.RBase + 30, RedCar - 10, 10, 10);
g.setColor(Color.red);
g.fillRect(this.RBase, RedCar - 20, 50, 10);
g.setColor(Color.red);
Polygon polygon1 = new Polygon();
polygon1.addPoint(this.RBase + 10, RedCar - 20);
polygon1.addPoint(this.RBase + 20, RedCar - 30);
polygon1.addPoint(this.RBase + 30, RedCar - 30);
polygon1.addPoint(this.RBase + 40, RedCar - 20);
g.fillPolygon(polygon1);
//green car
g.setColor(Color.green);
g.fillOval(this.GBase + 10, GreenCar - 10, 10, 10);
g.fillOval(this.GBase + 30, GreenCar - 10, 10, 10);
g.setColor(Color.green);
g.fillRect(this.GBase, GreenCar - 20, 50, 10);
g.setColor(Color.green);
Polygon polygon2 = new Polygon();
polygon2.addPoint(this.GBase + 10, GreenCar - 20);
polygon2.addPoint(this.GBase + 20, GreenCar - 30);
polygon2.addPoint(this.GBase + 30, GreenCar - 30);
polygon2.addPoint(this.GBase + 40, GreenCar - 20);
g.fillPolygon(polygon2);
}
}
}
I am trying to change one of the cars color but doesn't seem like it's working and I can't figure out the problem
|