-2

I am writing this program that creates an H-Tree design wherever you click, and I am supposed to implement a JMenuBar so I can change the color of the recursive drawing. For some reason it is not showing up no matter what I do.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial") class Echo extends JFrame implements ActionListener, MouseListener, ChangeListener{

    private static final int SLIDER_MIN = 1;
    private static final int SLIDER_MAX = 11;
    private static final int SLIDER_INIT = 1;
    private int x;
    private int y;
    private int rec = SLIDER_INIT;

    private Echo() {
        super("H-Tree Drawing Pad");

        Container canvas = this.getContentPane();

        addMenus();
        canvas.add(createSlider(), BorderLayout.SOUTH);


        this.setSize(800,800);
        this.setVisible(true);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.addMouseListener(this);

    }

    private void addMenus() {
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        JMenu menuColor = new JMenu("Color");
        menuBar.add(menuColor);

        JMenuItem mitToBlack = new JMenuItem("Black");
        mitToBlack.addActionListener(new ColorListener());
        menuColor.add(mitToBlack);

        JMenuItem mitToBlue = new JMenuItem("Blue");
        mitToBlue.addActionListener(new ColorListener());
        menuColor.add(mitToBlue);

        JMenuItem mitToRed = new JMenuItem("Red");
        mitToRed.addActionListener(new ColorListener());
        menuColor.add(mitToRed);

        JMenuItem mitToGreen = new JMenuItem("Green");
        mitToGreen.addActionListener(new ColorListener());
        menuColor.add(mitToGreen);  
    }

    private JPanel createSlider() {
        JPanel slider = new JPanel(new BorderLayout());
        JSlider electricSlide = new JSlider(JSlider.HORIZONTAL,SLIDER_MIN, SLIDER_MAX, SLIDER_INIT);

        electricSlide.addChangeListener(this);

        electricSlide.setMajorTickSpacing(2);
        electricSlide.setMinorTickSpacing(1);
        electricSlide.setPaintTicks(true);
        electricSlide.setPaintLabels(true);
        electricSlide.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
        slider.add(electricSlide);
        return slider;
    }

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

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();

        repaint();

    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        drawOrder(this.getRec(),x,y,95,g);

    }

    private void drawHTree(int x, int y, int size, Graphics g) {
        g.drawLine(x, y, x, y + size);
        g.drawLine(x + size, y, x + size, y + size);
        g.drawLine(x, y + size / 2, x + size, y + size / 2);

    }

    private void drawOrder(int initiative, int x, int y, int size, Graphics g) {
        this.drawHTree(x, y, size, g);
        if(initiative > 1) {
            this.drawOrder(initiative - 1, x - size/4, y - size/4, size/2, g);
            this.drawOrder(initiative - 1, x+size - size/4, y-size/4, size/2, g);
            this.drawOrder(initiative -1, x-size/4, y+size-size/4, size/2, g);
            this.drawOrder(initiative - 1, x+size-size/4, y+size-size/4, size/2, g);
        }
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        JSlider origin = (JSlider)e.getSource();
        if(!origin.getValueIsAdjusting()) {
            setRec((int)origin.getValue());
        }

    }

    public int getRec() {

        return rec ;
    }

    public void setRec(int rec) {
        this.rec = rec;
    }

    private class ColorListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent arg0) {


        }

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }



}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
PhilD359
  • 1
  • 2

1 Answers1

2

You've overridden the paint method of your JFrame and did not call the super's method within it, breaking the painting chain.

e.g., you're missing this:

@Override
public void paint(Graphics g) {
    super.paint(g);  // ****** you're missing this ***
    g.setColor(Color.BLACK);
    drawOrder(this.getRec(), x, y, 95, g);
}

By not calling the super's painting method your code is now preventing the JFrame's child components, including its menu bar, from painting correctly .

Having said this, in fact you should never paint directly within a JFrame but rather in a JPanel's paintComponent and even there you should call the super's painting method, here super.paintComponent(g);.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373