0

I'm trying to write up a little "game" that consists of a U.F.O. that flies in all directions (based on buttons) and explodes when it hits the bottom of the screen. I have 4 classes, 2 of which I'm not allowed to edit. They are:

The display window

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

/**
 *  A class that puts a graphics window on your display
*/
public  class DisplayWindow extends JFrame{
 /**
   * Content pane that will hold the added Jpanel
   */
  private Container c;
  /**
   * DisplayWindow constructor - no parameters
   */
  public DisplayWindow(){
    super("Display");
    c = this.getContentPane();
  }
   /**
 * Adds panel to content pane
 * @parameter the panel to be added
 */
  public void addPanel(JPanel p){
    c.add(p);
  }
  /**
 * consolidates the frame, makes it visible,
 * causes program termination when window is closed manually
 */
  public void showFrame(){
    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

And the driver for the UFO.

import javax.swing.*;

public class SaucerDriver{

  public static void main(String[] args){
    DisplayWindow d = new DisplayWindow();
    JMenuBar menuBar = new JMenuBar();
    d.setJMenuBar(menuBar);
    SaucerPanel p = new SaucerPanel(menuBar);
    d.addPanel(p);
    d.showFrame();
  }
}

I'm trying to add a menu bar but it wont show up. What am I doing wrong?

Here is my class for drawing all the buildings and whatnot

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

public class SaucerPanel extends JPanel implements ActionListener{
  JButton quitButton;
  JButton upButton;
  JButton leftButton;
  JButton rightButton;
  JButton downButton;  
  int xLoc;
  int yLoc;
  boolean explode;
  
  public SaucerPanel(JMenuBar menuBar){
    xLoc = 20;
    yLoc = 200;
    
    explode = false;
    
    this.setPreferredSize(new Dimension(850,500));
    this.setBackground(Color.white);
    quitButton = new JButton("Quit");
    this.add(quitButton);
    quitButton.addActionListener(this);
    
    downButton = new JButton("Down");
    this.add(downButton);
    downButton.addActionListener(this);
    
    upButton = new JButton("Up");
    this.add(upButton);
    upButton.addActionListener(this);
    
    leftButton = new JButton("Left");
    this.add(leftButton);
    leftButton.addActionListener(this);
    
    rightButton = new JButton("Right");
    this.add(rightButton);
    rightButton.addActionListener(this);
  }
  
  public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.green);
    g.fillRect(0,440,850,60);
    
    g.setColor(Color.gray);
    g.fillRect(80,420,210,100);
      g.fillRect(100,410,170,100);
      g.fillRect(120,400,130,100);
      g.fillOval(130,350,110,150);
      int[] aPoints = {170,200,185};
      int[] bPoints = {370,370,320};
        g.fillPolygon(aPoints,bPoints, 3);
      g.setColor(Color.black);
      g.fillRect(135,410,20,100);
       g.fillRect(175,410,20,100);
       g.fillRect(215,410,20,100);
      
      g.setColor(Color.blue);
      g.fillRect(460,140,120,360);
      
      g.setColor(Color.red);
      g.fillRect(700,400,100,150);
      
      g.setColor(Color.yellow);
      int[] xPoints = {700,800,750};
      int[] yPoints = {400,400,350};
      g.fillPolygon(xPoints, yPoints, 3);
      
      g.setColor(Color.black);
      g.fillRect(730,450,40,50);
      
      g.setColor(Color.darkGray);
    if(explode == false){
      g.fillOval(xLoc,yLoc,80,40);
      g.drawString("hovering...",xLoc,yLoc - 10);
    }else{
      for(int i = 0; i < 2000; i++){
        int x = (int)(Math.random()*850);
        int y = (int)(Math.random()*850);
        g.setColor(Color.orange);
        g.fillOval(x,y,5,5);
        int a = (int)(Math.random()*850);
        int b = (int)(Math.random()*850);
        g.setColor(Color.red);
        g.fillOval(a,b,5,5);
        int c = (int)(Math.random()*850);
        int d = (int)(Math.random()*850);
        g.setColor(Color.yellow);
        g.fillOval(c,d,5,5);
      }
    }
  }
      
      public void actionPerformed(ActionEvent e){
        if(e.getSource() == quitButton){
          System.exit(0);
        }
        else if(e.getSource() == downButton){
          yLoc += 5;
          if(yLoc == 400){
            explode = true;
          }
        }
        else if(e.getSource() == upButton){
          yLoc -= 5;
        }
        else if(e.getSource() == leftButton){
          xLoc -= 5;
        }
        else if(e.getSource() == rightButton){
          xLoc += 5;
        }
        
        repaint();
      }
    }

And here is my menu bar class

import javax.swing.*;


public class menuBar extends JFrame{
  
  public menuBar(){
  
    JFrame frame = new JFrame("Menu");
    frame.setVisible(true);
    frame.setSize(850,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    
    JMenu file = new JMenu("File");
    menuBar.add(file);
    JMenuItem open = new JMenuItem("Open");
    file.add(open);
    JMenuItem neww = new JMenuItem("New");
    file.add(neww);
    
    JMenu edit = new JMenu("Edit");
    menuBar.add(edit);
    JMenuItem color = new JMenuItem("Change Color");
    edit.add(color);
    
    JMenu help = new JMenu("Help");
    menuBar.add(help);
    JMenuItem about = new JMenuItem("About");
    help.add(about);
    
    frame.setVisible(true);
  }
}

Edited by fengapapitt: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by IamAuser
0

Hi,

I made some quick changes and your menu shows up now. I removed JMenuBar from your constructor in your SuacerPanel class becuase it didnt seem to be doing anything. In addition to that I have include the classes which have been modified:

import javax.swing.*;

public class menuBar  {
	JMenuBar menu;

	public menuBar() {

		menu = new JMenuBar();

		JMenu file = new JMenu("File");
		menu.add(file);
		JMenuItem open = new JMenuItem("Open");
		file.add(open);
		JMenuItem neww = new JMenuItem("New");
		file.add(neww);

		JMenu edit = new JMenu("Edit");
		menu.add(edit);
		JMenuItem color = new JMenuItem("Change Color");
		edit.add(color);

		JMenu help = new JMenu("Help");
		menu.add(help);
		JMenuItem about = new JMenuItem("About");
		help.add(about);

		menu.add(file);
		menu.add(edit);
		menu.add(help);
		menu.add(about);

	}

	JMenuBar getMenuBar() {
		return menu;
	}


import javax.swing.*;

public class SaucerDriver {

	public static void main(String[] args) {

		DisplayWindow d = new DisplayWindow();       
		menuBar b = new menuBar();
		d.setJMenuBar(b.getMenuBar());
		SaucerPanel p = new SaucerPanel();
		d.addPanel(p);
		d.showFrame();

	}

}

Hope everything works out.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.