Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a semantic error in my code. in the TrafficLightPanel the trafficLight inner class doesn't execute what I want. this is java the lab

I have a semantic error in my code. in the TrafficLightPanel the trafficLight inner class doesn't execute what I want.

this is java

the lab task is:

image text in transcribed

image text in transcribed

My code:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TrafficLight { public static void main (String[] args){ JFrame frame = new JFrame ("Traffic Light Panel"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new TrafficLightPanel()); frame.pack(); frame.setVisible(true); } }

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TrafficLightPanel extends JPanel{ private JButton redButton, amberButton, greenButton; private JLabel laspressedLabel1; private JPanel buttonPanel; public TrafficLightPanel(){ redButton = new JButton("Red"); amberButton = new JButton("Amber"); greenButton = new JButton("Green"); ButtonListener listener = new ButtonListener(); redButton.addActionListener(listener); amberButton.addActionListener(listener); greenButton.addActionListener(listener); JPanel LightPanel = new JPanel(); laspressedLabel1 = new JLabel("last pressed"); setPreferredSize(new Dimension(200,300)); setBackground(Color.blue); buttonPanel = new JPanel(); buttonPanel.setPreferredSize(new Dimension(80,290)); buttonPanel.setBackground(Color.white); buttonPanel.add(redButton); buttonPanel.add(amberButton); buttonPanel.add(greenButton); buttonPanel.add(laspressedLabel1); add(buttonPanel); add(LightPanel); } private class ButtonListener implements ActionListener{ public void actionPerformed (ActionEvent event) { if(event.getSource() == redButton){ laspressedLabel1.setText("Red"); buttonPanel.setBackground(Color.red); } else if(event.getSource() == amberButton){ laspressedLabel1.setText("Amber"); buttonPanel.setBackground(Color.orange); } else{ laspressedLabel1.setText("Green"); buttonPanel.setBackground(Color.green); } } } private class LightPanel extends JPanel{ public void paintComponent (Graphics page){ super.paintComponent(page); setBackground(Color.cyan); setPreferredSize(new Dimension(80,290)); page.setColor (Color.red); page.fillOval (40,30,40,40); } }

}

I want an output that similar to this:

image text in transcribed

Lab Work 1. Using listing 6.3 (L,D&C page 253) , or Lab 14 DinerApp (COMP160 Lab book page 71) as a guide, write an application class which makes a JFrame containing an instance of a class called TrafficLightPanel 2. Using sting 6.4 (L,D&C page 255) and the UML diagram on the previous page as a guide write a TrafficLightPanel class which extends JPanel imports the 3 packages necessary for GUIs, graphics and events .has 3 JButton data fields (which will show "Red" , "Amber" and "Green") has 1 JLabel data field (which wil initially show the text "last pressed") has 1 JPanel data field called buttonPanel .has a constructor which: instantiates any data fields that still don't exist sets the size of the TrafficLightPanel to 200 by 300 *sets the background colour of TrafficLightPanel to blue *sets buttonPanel's preferred size to 80 by 290 sets buttonPanel's background to white adds the buttons and label to buttonPanel *adds the buttonPanel to TrafficLightPanel Traffic Light Panel in the TrafficLightPanel constructor, any instruction to apply to the TrafficLightPanel itself does not need to use dot notation Red Amber Green last pressed 3. Compile and run. You should see something like the frame shown in Figure 18.1 Those buttons are no use unless they do something Figure 18.1 Time for action! 4. Still using listing 6.4 as a guide, write an inner private ButtonListener class which implements TrafficLightPanel constructor make an instance of this class. Register each button to this listener object 5. In the actionPerformed method, if the source of the event is redButton, set the text on ActionListener and has anactionPerformed method (it can be empty for now). In the lastPressedLabel to "red" and set the background colour of buttonPanel to red 5. In the actionPerformed method, if the source of the event is redButton, set the text on lastPressedLabel to "red" and set the background colour of buttonPanel to red. 6. Repeat this for the other buttons (use Color.orange for amber) 7. Compile and run. Your framewi look the same initially, but now should respond to the press of each button Graphics on the JPanel Now we want to draw a graphical representation of a traffic light and have the lights changing colour rather than the background. We could do it in the TrafficLightPanel, but it's not the ideal solution. Step 8 will illustrate why COMP160 85 Laboratory Workbook SS 2019 Laboratory 18 Graphical User Interfaces 8. Write a paintComponent method in the TrafficLightPanel class. Don't forget it needs a Graphics object as a parameter (refer back to L,D&C page 972 Listing F.2, SnowmanPanel). In this method, set the graphics colour to red and draw a filled circle at (0,30,40, 40). Compile and run. Now change the position of the circle to (40,30,40,40 If your dimensions are the same as those suggested above, the circle will disappear behind the button panel because it is being drawn on the TrafficLightPanel. A better solution is to make another panel to put the lights on (Step 9). This panel will sit neatly beside buttonPanel 9. Make another inner private class called LightPanel which extends JPanel. (This means it will have all the functionality of a JPanel as well as anything else we care to add on top, just like TrafficLightPanel.) Shift the paintComponent method into the LightPanel class. Make the first statement of the method a call to super.paintcomponent (page); (assuming page is the name of your Graphics parameter) This draws the parent: the JPanel which LightPanel extends. This will give you a background colour 10. 1. In a constructor for LightPanel, set the preferred dimension to 80 by 290 and set the background colour to 12. 13. 14. cyan In the TrafficLightPanel constructor, make an instance of LightPanel and add it. Compile and run. The red circle will now be drawn with reference to the LightPanel, so you may need to readjust the x position. Now draw the other 2 circles. Your frame should look something like that in Figure 18.2. Traffic Light Panel 8 Traffic Light Panel Red Amber Green last pressed Red Amber Green green Traffic Light Panel Red Amber Green green

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions