2

This is a assignment that have a Menubar including the types of rides which user can pick from, after a ride is picked, there would be a messege appearing on the second panel regarding the safety and and also a slider for rider's height. I tried put everything together for panel1 and 2, as the JMenuBar, drawString and actionperformed, but nothing shows up other than the slider.

Heading

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


public class Ride extends JFrame
{
        private JMenu type;
        private JSlider slider;
        private GridLayout grid;
        private JMenuBar menu;
        private JMenuItem Roller;
        private JMenuItem Horror;
        private int sliderVal;
        private int allow;
        private String msg;
        private String yesno;
        private BorderLayout b;



        public static void main(String[]args)
        {
                Ride ride = new Ride();
        }

        public Ride()
        {
                super("Ride");
                setSize(1200,1000);
                Panel1 p1 = new Panel1();
                Panel2 p2 = new Panel2();
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setLayout(new GridLayout(1,2,0,0));
                add(p1);
                add(p2);
                setVisible(true);
        }

        class Panel1 extends JPanel implements ActionListener
        {
                public Panel1()
                {
                    setSize(600,1000);
                        setBackground(Color.BLACK);
                        //Direction direction = new Direction();
                        type = new JMenu("type of rides");
                        Roller = new JMenuItem("Roller Coaster");
                        Roller.addActionListener(this);
                        type.add(Roller);
                        Horror = new JMenuItem("Scary Ride");
                        Horror.addActionListener(this);
                        type.add(Horror);
                        menu = new JMenuBar();
                        menu.add(type);
                       // setLayout(new GridLayout(2,1,0,20));
                        add(menu);
                        //add(direction);
                        setVisible(true);
                }

                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==Roller)
                        {
                                msg = new String("Rough Ride, height must be >42");
                                allow = 42;
                                repaint();
                        }
                        else if(e.getSource()==Horror)
                        {
                                msg = new String("Scary Ride, height must be >32");
                                allow = 32;
                                repaint();
                        }
                }

                public void paintComponent(Graphics g)
                {
                        super.paintComponent(g);
                        g.drawString("Pick Ride on the menu above,",10,600);
                        g.drawString("select height on the slidr.",10,700);
                }
        }

        class Panel2 extends JPanel implements ChangeListener
        {
            public Panel2()
            {
                setSize(600,1000);
                    setLayout(new FlowLayout());
                    slider = new JSlider(JSlider.VERTICAL,30,45,30);
                    slider.addChangeListener(this);
                    add(slider);
                    setVisible(true);

            }


            public void stateChanged(ChangeEvent e)
            {
                            sliderVal = slider.getValue();//get the value of the slider
                            repaint();//reset
                            if(sliderVal > allow)
                                    yesno = new String("allowed");
                            else if (sliderVal < allow)
                                    yesno = new String("not allowed");
            }


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

                    g.drawString(msg,20,600);
                    g.drawString(sliderVal+" :"+yesno,20,800);
            }

        }
}## Heading ##
leppie
  • 115,091
  • 17
  • 196
  • 297
Hallllis
  • 31
  • 2

1 Answers1

0

I think you have to initialize msg at first with a default value (e.g. "" for empty string). Otherwise java throws a NullPointerException (Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: String is null). If the variable is initialized the black panel is shown.

java_olli
  • 51
  • 2
  • 9
  • THANK YOU SO MUCH IT DID WORK. i assigned two strings to empty value and the program runs well. Really appreciate your help.<333 – Hallllis May 08 '15 at 03:58