Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Graphics color in java applet (https://www.askmehelpdesk.com/showthread.php?t=777893)

  • Dec 6, 2013, 06:07 PM
    Radwa89
    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
  • Dec 8, 2013, 04:14 PM
    Scleros
    1 Attachment(s)
    The problem is paintComponent() does not distinguish which car is being painted. The code is painting a car red and then immediately painting it green. Two race cars are declared. Think object oriented-ly. Why not make the color of the car a member variable of the race car class and pass the color the car should be when declaring it? Then paintComponent() can generically process all the cars the same way whether there is one car or a hundred and without needing to figure out which car it is painting simplifying the code.

    Modified code follows below. Key modifications are in red.
    Code:

    /*
    ** Compiled using Oracle JDK 1.7.0_45
    */

    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(Color.RED);
            private Racing.RaceCar GreenCar = new Racing.RaceCar(Color.GREEN);

            public Racing() {
                    JPanel panel1 = new JPanel();
                    panel1.add(new JLabel("David's Car : "));
                    panel1.add(jtfRedCarSpeed);
                    panel1.add(new JLabel("Radwa's Car : "));
                    panel1.add(jtfGreenCarSpeed);

                    JPanel panel2 = new JPanel(new GridLayout(4, 1));
                    panel2.add(RedCar);
                    panel2.add(GreenCar);

                    add(panel1, "North");
                    add(panel2, "Center");

                    jtfRedCarSpeed.addActionListener(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() == jtfRedCarSpeed) {
                            RedCar.setSpeed(Integer.parseInt(jtfRedCarSpeed.getText()));
                    }
                    else if (e.getSource() == jtfGreenCarSpeed) {
                            GreenCar.setSpeed(Integer.parseInt(jtfGreenCarSpeed.getText()));
                    }
            }

            class RaceCar extends JPanel implements ActionListener {
                    private int xBase = 0;
                    private Color PaintColor = Color.BLACK;
                    private Timer timer = new Timer(10, this);

                    public RaceCar(Color CarColor) {
                            setBorder(BorderFactory.createLineBorder(Color.BLACK));

                            PaintColor = CarColor;

                            timer.start();
                    }

                    public void actionPerformed(ActionEvent e) {
                            repaint();
                    }

                    public void setSpeed(int speed) {
                            timer.setDelay(speed);
                    }

                    public void paintComponent(Graphics g) {
                            super.paintComponent(g);

                            int yBase = getHeight();
                            if (xBase > getWidth()) {
                                    xBase = -20;
                            }
                            else {
                                    xBase += 1;
                            }


                            // Paint car...
                            g.setColor(PaintColor);
                           
                            // ...Wheels
                            g.fillOval(xBase + 10, yBase - 10, 10, 10);
                            g.fillOval(xBase + 30, yBase - 10, 10, 10);

                            // ...Body
                            g.fillRect(xBase, yBase - 20, 50, 10);

                            // ...Roof
                            Polygon polygon1 = new Polygon();
                            polygon1.addPoint(xBase + 10, yBase - 20);
                            polygon1.addPoint(xBase + 20, yBase - 30);
                            polygon1.addPoint(xBase + 30, yBase - 30);
                            polygon1.addPoint(xBase + 40, yBase - 20);
                            g.fillPolygon(polygon1);
                    }
            }
    }


  • All times are GMT -7. The time now is 10:10 PM.