The object called to create the GUI is below.
Expand|Select|Wrap|Line Numbers
- public city()
- {
- theWindow=new GUI();
- theWindow.setVisible(true);
- randomSet();
- draw();
- }
My GUI creation is below. The code is long so not everything is shown.
Expand|Select|Wrap|Line Numbers
- package city;
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.event.*;
- import java.util.ArrayList;
- import javax.swing.JCheckBoxMenuItem;
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- public class GUI extends JFrame implements MouseListener, ActionListener, KeyListener{
- Container c;
- int gX[];
- int gY[];
- int lowX, lowXcoord, lowY, lowYcoord;
- JMenuBar menubar;
- JCheckBoxMenuItem pr;
- city game;
- private String object="null";
- JMenu file,residential,business,commercial,manufacturing,administration,re,removeobject;
- JMenuItem bull, save, offices, n,s,e,w, lowresidential, highresidential, lowcommerce, busb, highcommerce, factory,fire,police,padmin,hospital,school,power,garbage,water,waste,reset;
- public GUI(boolean inb)
- {
- boolean prev=inb;
- }
- public GUI()
- {
- paintGrid();
- }
- public void paintGrid()
- {
- setResizable(false);
- setTitle("City");
- c = getContentPane();
- setSize(700, 700);
- c.setLayout(new BorderLayout());
- menubar = new JMenuBar();
- c.add("North",menubar);
- c.addMouseListener(this);
- file = new JMenu("FILE");
- save=new JMenuItem("Save");
- save.addActionListener(this);
- file.add(save);
- n=new JMenuItem("Go North");
- n.addActionListener(this);
- file.add(n);
- s=new JMenuItem("Go South");
- s.addActionListener(this);
- file.add(s);
- e=new JMenuItem("Go East");
- e.addActionListener(this);
- file.add(e);
- w=new JMenuItem("Go West");
- w.addActionListener(this);
- file.add(w);
- residential= new JMenu("Residential");
- lowresidential=new JMenuItem("Small Res");
- lowresidential.addActionListener(this);
- residential.add(lowresidential);
- highresidential=new JMenuItem("Large Res");
- highresidential.addActionListener(this);
- residential.add(highresidential);
- business = new JMenu("Small Business");
- busb=new JMenuItem("Business");
- busb.addActionListener(this);
- business.add(busb);
- commercial = new JMenu("Commmercial Offices");
- lowcommerce=new JMenuItem("Small Office");
- lowcommerce.addActionListener(this);
- commercial.add(lowcommerce);
- highcommerce= new JMenuItem("Large Office");
- highcommerce.addActionListener(this);
- commercial.add(highcommerce);
- manufacturing = new JMenu("Manufacturing");
- factory=new JMenuItem("Factory");
- factory.addActionListener(this);
- manufacturing.add(factory);
- administration = new JMenu("Public Services");
- power=new JMenuItem("Power");
- power.addActionListener(this);
- administration.add(power);
- school=new JMenuItem("School");
- school.addActionListener(this);
- administration.add(school);
- fire= new JMenuItem("Fire");
- fire.addActionListener(this);
- administration.add(fire);
- police= new JMenuItem("Police");
- police.addActionListener(this);
- administration.add(police);
- water=new JMenuItem("Water");
- water.addActionListener(this);
- administration.add(water);
- garbage=new JMenuItem("Garbage");
- garbage.addActionListener(this);
- administration.add(garbage);
- waste=new JMenuItem("Waste Treatment");
- waste.addActionListener(this);
- administration.add(waste);
- padmin=new JMenuItem("Government Services");
- padmin.addActionListener(this);
- administration.add(padmin);
- hospital= new JMenuItem("Hospital");
- hospital.addActionListener(this);
- administration.add(hospital);
- removeobject= new JMenu("Remove Building");
- bull=new JMenuItem("Remove Object");
- bull.addActionListener(this);
- removeobject.add(bull);
- re= new JMenu("Reset All");
- re.addActionListener(this);
- reset=new JMenuItem("Reset Game");
- reset.addActionListener(this);
- re.add(reset);
- menubar.add(file);
- menubar.add(residential);
- menubar.add(business);
- menubar.add(commercial);
- menubar.add(manufacturing);
- menubar.add(administration);
- menubar.add(bull);
- menubar.add(re);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- paint grid = new paint(0,0,"grid");
- c.add(grid);
- c.validate();
- }
- public void actionPerformed(ActionEvent ae)
- {
- String theText= ((JMenuItem) ae.getSource()).getText();
- if(theText.equals("Remove Object"))
- {
- object="remove";
- }
- else if(theText.equals("Reset Game"))
- {
- object="reset";
- c.repaint();
- c.removeAll();
- c.validate();
- paintGrid();
- randomizer res=new randomizer();
- }
Expand|Select|Wrap|Line Numbers
- public void addObject(int inx, int iny, String inobj)
- {
- calcSnapX(inx);
- calcSnapY(iny);
- paint sq= new paint(getSnapX(), getSnapY(), inobj);
- c.add(sq);
- c.validate();
- }
Expand|Select|Wrap|Line Numbers
- public city()
- {
- theWindow=new GUI();
- theWindow.setVisible(true);
- randomSet();
- draw();
- }
I used a paint class to draw the components. It is below.
Expand|Select|Wrap|Line Numbers
- package city;
- import java.awt.*;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- class paint extends JLabel{
- int x,y;
- int h,w;
- String object="null";
- public paint(int inx, int iny, String inobject)
- {
- if(inobject.equals("null"))
- {
- System.out.println("Select A Building and Building Type");
- }
- else
- {
- x=inx;
- y=iny;
- object=inobject;
- }
- if(inobject.equals("plant"))
- {
- setForeground(Color.GREEN);
- }
- else if(inobject.equals("lake"))
- {
- setForeground(Color.BLUE);
- }
- else if(inobject.equals("mountain"))
- {
- setForeground(Color.BLACK);
- }
- else if(inobject.equals("bus"))
- {
- setForeground(Color.MAGENTA);
- }
- else if(inobject.equals("lowres"))
- {
- setForeground(Color.CYAN);
- }
- else if(inobject.equals("highres"))
- {
- setForeground(Color.DARK_GRAY);
- }
- else if(inobject.equals("soffice"))
- {
- setForeground(Color.CYAN);
- }
- else if(inobject.equals("loffice"))
- {
- setForeground(Color.DARK_GRAY);
- }
- else if(inobject.equals("grid")) {
- setForeground(Color.BLACK);
- }
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if(object.equals("null"))
- {
- System.out.println("Select A Building and Building Type");
- }
- else
- {
- if(object.equals("grid"))
- {
- for(int i = 0;i<35;i++){
- g.drawLine(i*20, 0, i*20, 700);
- g.drawLine(0, i*20, 700, i*20);
- }
- }
- else if(object.equals("plant"))
- {
- g.fillOval(x, y, 5, 5);
- }
- else if(object.equals("lake"))
- {
- g.fillOval(x, y, 50, 50);
- }
- else if(object.equals("bus"))
- {
- g.fillOval(x-6, y-27, 15, 15);
- }
- else if(object.equals("lowres"))
- {
- g.fillRect(x-6, y-27, 15, 15);
- }
- else if(object.equals("highres"))
- {
- g.fillRect(x-6, y-27, 15, 15);
- }
- else if(object.equals("soffice"))
- {
- g.fillOval(x-6, y-27, 15, 15);
- }
- else if(object.equals("loffice"))
- {
- g.fillOval(x-6, y-27, 15, 15);
- }
- }
- }
- }
However, after redrawing the screen, I get the following error.
Expand|Select|Wrap|Line Numbers
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- at city.city.draw(city.java:183)
- at city.city.<init>(city.java:37)
- at city.randomizer.<init>(randomizer.java:7)
- at city.GUI.actionPerformed(GUI.java:160)
- at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
- at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
- at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
- at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
- at javax.swing.AbstractButton.doClick(Unknown Source)
- at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
- at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
- at java.awt.Component.processMouseEvent(Unknown Source)
- at javax.swing.JComponent.processMouseEvent(Unknown Source)
- at java.awt.Component.processEvent(Unknown Source)
- at java.awt.Container.processEvent(Unknown Source)
- at java.awt.Component.dispatchEventImpl(Unknown Source)
- at java.awt.Container.dispatchEventImpl(Unknown Source)
- at java.awt.Component.dispatchEvent(Unknown Source)
- at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
- at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
- at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
- at java.awt.Container.dispatchEventImpl(Unknown Source)
- at java.awt.Window.dispatchEventImpl(Unknown Source)
- at java.awt.Component.dispatchEvent(Unknown Source)
- at java.awt.EventQueue.dispatchEvent(Unknown Source)
- at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
- at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
- at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
- at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
- at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
- at java.awt.EventDispatchThread.run(Unknown Source)