0

I'm having a problem getting particular objects to appear in my frame/panels. In the code below I have objects of type "Drawing" such as light, red, yellow, and green. The program is supposed to create a traffic light but the other drawing don't appear and I'm not sure why. light is separate from the rest so that it won't be affected if the background draws over it, but this isn't the issue. The circles/lights don't draw and I don't see what I'm missing or what I'm doing wrong.

What the frame should look like

package lab8;

import oracle.jvm.hotspot.jfr.JFR;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

enum shape{
    circle,square;
}

public class TrafficLight2 extends JFrame {
JPanel panel = new JPanel();
JFrame frame = new JFrame();
TrafficLight2(){
    frame.setPreferredSize(new Dimension(500, 500));
    frame.setLayout(new FlowLayout());
    panel.setLayout(new FlowLayout());
    TrafficLight t = new TrafficLight();
    t.setPreferredSize(new Dimension(500,500));
    panel.setPreferredSize(new Dimension(500,500));
    panel.add(t,BorderLayout.CENTER);
    Drawing light = new Drawing();
    light.colour=Color.RED;
    light.s=shape.circle;
    repaint();
    panel.add(light);
    frame.add(light);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
public static void main(String[] args){
    new TrafficLight2();
}
class ButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
    ButtonListener(){

    }
}
class Drawing extends JPanel{
    int width=50;
    int height=50;
    shape s;
    Color colour;
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int verticalCenter = getParent().getHeight()/2;
        int horizontalCenter = getParent().getWidth()/2;
        int topLeftSquareCornerY = verticalCenter - (height/2);
        int topLeftSquareCornerX = horizontalCenter - (width/2);
        g.setColor(colour);
        if (s==shape.square) {
            g.fillRect(topLeftSquareCornerX, topLeftSquareCornerY, width, height);
        }
        else{
            g.fillOval(topLeftSquareCornerX,topLeftSquareCornerY,width,height);
        }
    }
}

class TrafficLight extends Drawing{
    Drawing red = new Drawing();
    Drawing yellow = new Drawing();
    Drawing green = new Drawing();
    Drawing background = new Drawing();
    TrafficLight(){
        this.red.s=shape.circle;
        this.red.colour=Color.RED;
        this.yellow.s=shape.circle;
        this.yellow.colour=Color.YELLOW;
        this.green.s=shape.circle;
        this.green.colour=Color.GREEN;
        this.background.s=shape.square;
        this.background.colour=Color.BLACK;
        this.background.width=100;
        this.background.height=300;
        this.s=shape.square;
        this.colour=Color.BLACK;
        this.width=100;
        this.height=300;
        background.add(red,BorderLayout.NORTH);
        background.add(yellow,BorderLayout.CENTER);
        background.add(green,BorderLayout.SOUTH);
        this.add(background)
        repaint();
    }
  }
}
Luke Ireland
  • 33
  • 1
  • 7

1 Answers1

0

You forgot to add background to TrafficLight.

TrafficLight(){
   .... other stuff
   this.add(background);
}

Also, I find it odd that TrafficLight2 extends JFrame, but is not actually used...

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • Originally it was used instead of Frame. I'll keep it using frame as per the code for now but I'll be changing it back once I get this fixed. – Luke Ireland Mar 02 '18 at 15:17
  • This makes the lights appear cropped off into small squares. They are being drawn as circles but the container doesn't fit them and I'm not sure which part is causing this. – Luke Ireland Mar 02 '18 at 15:21
  • Worth mentioning that background paints as a circle despite being set as a square, hence the need of this.s being set to square instead. – Luke Ireland Mar 02 '18 at 15:23
  • @LukeIreland Do you even need a background? It seems that TrafficLight2 is black and background is black as well. – kutschkem Mar 02 '18 at 15:29
  • No, as background doesn't actually do what I want and TrafficLight does despite the visual attributes being identical. TrafficLight works fine for the box that will contains the lights, but the lights don't appear as circles and they don't appear where I want. – Luke Ireland Mar 02 '18 at 16:06
  • change vertical center from using getParent().getHeight() to just getHeight() – kutschkem Mar 02 '18 at 16:15
  • This doesn't fix it unfortunately. The lights persist on being three squares at the top of the screen and not in the TrafficLight box. – Luke Ireland Mar 02 '18 at 16:54