-3

so I created a Traffic light that has to be exactly like this one below:

https://www.youtube.com/watch?v=BUpatHRLarI&feature=emb_title

I am supposed to use the exact code from the above video, but for some reason, it does not work even though my code matches with the code above. I think it is due to the person in that video using eclipse to code whereas I used repl.it java swing to accomplish this task.

My code:


Main.java

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main{
   public static void main(String[]args){

      JFrame frame = new JFrame();
      JPanel panel = new StopLightPanel();

      frame.add(panel);
      frame.setSize(250, 350);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_ClOSE);
      frame.setVisible(true);
   }
}

StopLightPanel.java

public class StopLightPanel extends JPanel {

  StopLightDrawing light = new StopLightDrawing();

  public StopLightPanel() {
    JButton changebutton = new JButton("Switch");
    light.setPreferredSize(new Dimension(160, 260));

    buttonListener l = new buttonListener();
    changebutton.addActionlistener(l);

    add(light);
    add(changebutton);
  }
  public class buttonListener implements Actionlistener {

    @Override
    public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
      light.changeColor();
    }
  }

StopLightDrawing.java

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;

public class StopLightDrawing extends JComponent {
  Color go = Color.gray;
  Color slow = Color.gray;
  Color stop = Color.red;

  String activelight = "red";

  public void paintComponent(Graphics g) {
    g.setColor(Color.yellow);
    g.fillRect(0, 0, 150, 250);

    g.setColor(Color.black);
    g.drawRect(0, 0, 150, 250);

    g.setColor(stop);
  }
}

After running the main file I experience quite a bit of errors:

./StopLightPanel.java:22: error: reached end of file while parsing
} Λ
./StopLightPanel.java:1: error: cannot find symbol public class StopLightPanel extends JPanel{
symbol: class JPanel
./StopLightPanel.java:15: error: cannot find symbol public class buttonListener implements Actionlistener{ A
symbol: class Actionlistener location: class StopLightPanel
./StopLightPanel.java:18: error: cannot find symbol public void actionPerformed (ActionEvent e) {
symbol: class ActionEvent
location: class StopLightPanel.buttonListener
Main.java:8: error: incompatible types: StopLightPanel cannot be converted to JPanel JPanel panel = new StopLightPanel();
Λ
Main.java:12: error: cannot find symbol

I believe the issue lies in the conversion, and how I'm using the specified elements in the code, I tried to fix StopLightPanel.java as I think the issues lies from there, but it did not help.

  • 1
    Do not post pictures of code. Cut code and/or error messages and paste them directly into your question. Now as for your question, you're actually dealing with multiple errors, most of which are arguably caused by carelessness. You think the code is the same, but the evidence is right in front of your face -- it's not the same. You can google each and every one of those error messages and you'll find out exactly how to fix your problems -- each of them is very common and not terribly difficult to resolve. – MarsAtomic Oct 01 '20 at 21:35
  • 1
    Java import statements matter. A lot. Read the duplicate for the details – DontKnowMuchBut Getting Better Oct 01 '20 at 21:37
  • 2
    aside: a) stick to java naming conventions when showing java code publicly, b) put some effort into formatting the code properly (indentation matters for readability) – kleopatra Mar 31 '23 at 10:35
  • hmm .. so you edited the question without improving (still images of errors vs c&p of plain text, still no imports... ) – kleopatra Mar 31 '23 at 10:38
  • Im going to fix that really quick, I'm trying to find the snippets –  Mar 31 '23 at 10:39
  • thanks, good start - now go ahead: indentation, imports .. :) – kleopatra Mar 31 '23 at 10:45
  • @kleopatra, i have fixed the indentation –  Mar 31 '23 at 10:48
  • .... imports ;) btw: are all your classes in the same package? and: there's a missing closing bracket in StopLightPanel. Do you really want StopLightDrawing extend JComponent (vs. JPanel)? shouldn't matter too much, but unusual – kleopatra Mar 31 '23 at 10:56

1 Answers1

0

I fixed your typing errors, finished your drawing code, and came up with the following GUI.

Traffic Light

Pressing the switch button will change the light from green to yellow (orange), to red.

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

Most of the changes were in the StopLightDrawing class. The first line of a paintComponent override should always be super.paintComponent(g);.

Here's the complete runnable code. I made the additional classes inner classes so I could post the code as one block.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TrafficLightExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TrafficLightExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new StopLightPanel();
        frame.add(panel);
        frame.setSize(250, 350);
        frame.setVisible(true);
    }

    public class StopLightPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        StopLightDrawing light = new StopLightDrawing();

        public StopLightPanel() {
            JButton changebutton = new JButton("Switch");

            ButtonListener l = new ButtonListener();
            changebutton.addActionListener(l);

            add(light);
            add(changebutton);
        }

        public class ButtonListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                light.changeColor();
            }
        }
    }

    public class StopLightDrawing extends JComponent {

        private static final long serialVersionUID = 1L;

        private int activeLight = 2;
        private Color[] colors = { Color.green, Color.orange, Color.red };

        public StopLightDrawing() {
            this.setPreferredSize(new Dimension(160, 250));
        }

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

            g.setColor(Color.black);
            g.fillRect(0, 0, 160, 250);

            g.setColor(Color.yellow);
            g.fillRect(5, 5, 150, 240);

            int radius = 20;
            int diameter = radius + radius;
            int x = 80;
            int y = 60;
            setLightColor(g, 2);
            g.fillOval(x - radius, y - radius, diameter, diameter);

            y += 60;
            setLightColor(g, 1);
            g.fillOval(x - radius, y - radius, diameter, diameter);

            y += 60;
            setLightColor(g, 0);
            g.fillOval(x - radius, y - radius, diameter, diameter);
        }

        private void setLightColor(Graphics g, int lightPosition) {
            if (activeLight == lightPosition) {
                g.setColor(colors[lightPosition]);
            } else {
                g.setColor(Color.gray);
            }
        }

        public void changeColor() {
            activeLight++;
            activeLight %= colors.length;
            repaint();
        }
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111