473,467 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How Do I Implement Java Swing and JLabel?

9 New Member
I have been writing a program that allows the user to select and then post items to a screen. It will also redraw the screen. It sets up fine the first time the classes are called. However, after redrawing the container, I cannot make a call to draw to the container from the object used to set it up. I have tried using JFrames, JPanels; a separate class. Is there a way to do this?

The object called to create the GUI is below.
Expand|Select|Wrap|Line Numbers
  1. public city()
  2.  theWindow=new GUI();
  3.  theWindow.setVisible(true);
  4.  randomSet();
  5.  draw();
  6. }
  7.  
The important GUI code is below.

My GUI creation is below. The code is long so not everything is shown.
Expand|Select|Wrap|Line Numbers
  1. package city;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Container;
  5. import java.awt.event.*;
  6. import java.util.ArrayList;
  7.  
  8. import javax.swing.JCheckBoxMenuItem;
  9. import javax.swing.JFrame;
  10. import javax.swing.JMenu;
  11. import javax.swing.JMenuBar;
  12. import javax.swing.JMenuItem;
  13.  
  14. public class GUI extends JFrame implements MouseListener, ActionListener, KeyListener{
  15.  
  16.      Container c;
  17.     int gX[];
  18.     int gY[];
  19.     int lowX, lowXcoord, lowY, lowYcoord;
  20.      JMenuBar menubar;
  21.      JCheckBoxMenuItem pr;
  22.      city game;
  23.      private String object="null";
  24.      JMenu file,residential,business,commercial,manufacturing,administration,re,removeobject;
  25.      JMenuItem bull, save, offices, n,s,e,w, lowresidential, highresidential, lowcommerce, busb, highcommerce, factory,fire,police,padmin,hospital,school,power,garbage,water,waste,reset;
  26.  
  27.    public GUI(boolean inb)
  28.    {
  29.        boolean prev=inb;
  30.    }
  31.     public GUI()
  32.     {
  33.        paintGrid();
  34.     }
  35.  
  36.      public void paintGrid()
  37.      {
  38.          setResizable(false);
  39.             setTitle("City");
  40.             c = getContentPane();
  41.             setSize(700, 700);
  42.             c.setLayout(new BorderLayout());
  43.             menubar = new JMenuBar();
  44.             c.add("North",menubar);
  45.             c.addMouseListener(this);
  46.  
  47.             file = new JMenu("FILE");
  48.             save=new JMenuItem("Save");
  49.             save.addActionListener(this);
  50.             file.add(save);
  51.             n=new JMenuItem("Go North");
  52.             n.addActionListener(this);
  53.             file.add(n);
  54.             s=new JMenuItem("Go South");
  55.             s.addActionListener(this);
  56.             file.add(s);
  57.             e=new JMenuItem("Go East");
  58.             e.addActionListener(this);
  59.             file.add(e);
  60.             w=new JMenuItem("Go West");
  61.             w.addActionListener(this);
  62.             file.add(w);
  63.  
  64.             residential= new JMenu("Residential");
  65.             lowresidential=new JMenuItem("Small Res");
  66.             lowresidential.addActionListener(this);
  67.             residential.add(lowresidential);
  68.             highresidential=new JMenuItem("Large Res");
  69.             highresidential.addActionListener(this);
  70.             residential.add(highresidential);
  71.  
  72.             business = new JMenu("Small Business");
  73.             busb=new JMenuItem("Business");
  74.             busb.addActionListener(this);
  75.             business.add(busb);
  76.  
  77.             commercial = new JMenu("Commmercial Offices");
  78.             lowcommerce=new JMenuItem("Small Office");
  79.             lowcommerce.addActionListener(this);
  80.             commercial.add(lowcommerce);
  81.             highcommerce= new JMenuItem("Large Office");
  82.             highcommerce.addActionListener(this);
  83.             commercial.add(highcommerce);
  84.  
  85.             manufacturing = new JMenu("Manufacturing");
  86.             factory=new JMenuItem("Factory");
  87.             factory.addActionListener(this);
  88.             manufacturing.add(factory);
  89.  
  90.             administration = new JMenu("Public Services");
  91.             power=new JMenuItem("Power");
  92.             power.addActionListener(this);
  93.             administration.add(power);
  94.             school=new JMenuItem("School");
  95.             school.addActionListener(this);
  96.             administration.add(school);
  97.             fire= new JMenuItem("Fire");
  98.             fire.addActionListener(this);
  99.             administration.add(fire);
  100.             police= new JMenuItem("Police");
  101.             police.addActionListener(this);
  102.             administration.add(police);
  103.             water=new JMenuItem("Water");
  104.             water.addActionListener(this);
  105.             administration.add(water);
  106.             garbage=new JMenuItem("Garbage");
  107.             garbage.addActionListener(this);
  108.             administration.add(garbage);
  109.             waste=new JMenuItem("Waste Treatment");
  110.             waste.addActionListener(this);
  111.             administration.add(waste);
  112.             padmin=new JMenuItem("Government Services");
  113.             padmin.addActionListener(this);
  114.             administration.add(padmin);
  115.             hospital= new JMenuItem("Hospital");
  116.             hospital.addActionListener(this);
  117.             administration.add(hospital);
  118.  
  119.             removeobject= new JMenu("Remove Building");
  120.             bull=new JMenuItem("Remove Object");
  121.             bull.addActionListener(this);
  122.             removeobject.add(bull);
  123.  
  124.             re= new JMenu("Reset All");
  125.             re.addActionListener(this);
  126.             reset=new JMenuItem("Reset Game");
  127.             reset.addActionListener(this);
  128.             re.add(reset);
  129.  
  130.             menubar.add(file);
  131.             menubar.add(residential);
  132.             menubar.add(business);
  133.             menubar.add(commercial);
  134.             menubar.add(manufacturing);
  135.             menubar.add(administration);
  136.             menubar.add(bull);
  137.             menubar.add(re);
  138.  
  139.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140.             paint grid = new paint(0,0,"grid");
  141.             c.add(grid);
  142.             c.validate();
  143.      }
  144.  
  145.      public void actionPerformed(ActionEvent ae)
  146.      {
  147.              String theText= ((JMenuItem) ae.getSource()).getText();
  148.  
  149.              if(theText.equals("Remove Object"))
  150.              {
  151.                  object="remove";
  152.              }
  153.              else if(theText.equals("Reset Game"))
  154.              {
  155.                  object="reset";
  156.                  c.repaint();
  157.                  c.removeAll();
  158.                  c.validate();
  159.                  paintGrid();
  160.                  randomizer res=new randomizer();
  161.              }
  162.  
Objects are added using the method below in the GUI class.

Expand|Select|Wrap|Line Numbers
  1. public void addObject(int inx, int iny, String inobj)
  2.         {
  3.            calcSnapX(inx);
  4.            calcSnapY(iny);
  5.            paint sq= new paint(getSnapX(), getSnapY(), inobj);
  6.            c.add(sq);
  7.            c.validate();
  8.         }
  9.  
This code is called using the following.

Expand|Select|Wrap|Line Numbers
  1. public city()
  2.  theWindow=new GUI();
  3.  theWindow.setVisible(true);
  4.  randomSet();
  5.  draw();
  6. }
  7.  

I used a paint class to draw the components. It is below.
Expand|Select|Wrap|Line Numbers
  1. package city;
  2.  
  3. import java.awt.*;
  4.  
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7.  
  8. class paint extends JLabel{
  9.  
  10.     int x,y;
  11.     int h,w;
  12.     String object="null";
  13.  
  14.     public paint(int inx, int iny, String inobject)
  15.     {
  16.         if(inobject.equals("null"))
  17.         {
  18.         System.out.println("Select A Building and Building Type");
  19.         }
  20.         else 
  21.         {
  22.             x=inx;
  23.             y=iny;
  24.             object=inobject;
  25.         }
  26.         if(inobject.equals("plant"))
  27.         {
  28.             setForeground(Color.GREEN);
  29.         }
  30.         else if(inobject.equals("lake"))
  31.         {
  32.             setForeground(Color.BLUE);
  33.         }
  34.         else if(inobject.equals("mountain"))
  35.         {
  36.             setForeground(Color.BLACK);
  37.         }
  38.         else if(inobject.equals("bus"))
  39.         {
  40.         setForeground(Color.MAGENTA);
  41.         }
  42.         else if(inobject.equals("lowres"))
  43.         {
  44.          setForeground(Color.CYAN);    
  45.         }
  46.         else if(inobject.equals("highres"))
  47.         {
  48.          setForeground(Color.DARK_GRAY);    
  49.         }
  50.         else if(inobject.equals("soffice"))
  51.         {
  52.          setForeground(Color.CYAN);
  53.         }
  54.         else if(inobject.equals("loffice"))
  55.         {
  56.             setForeground(Color.DARK_GRAY);
  57.         }
  58.         else if(inobject.equals("grid")) {
  59.             setForeground(Color.BLACK);
  60.         }
  61.     }
  62.  
  63.      protected void paintComponent(Graphics g) {
  64.             super.paintComponent(g);
  65.             if(object.equals("null"))
  66.             {
  67.             System.out.println("Select A Building and Building Type");
  68.             }
  69.             else
  70.             {
  71.             if(object.equals("grid")) 
  72.             {
  73.                for(int i = 0;i<35;i++){
  74.                 g.drawLine(i*20, 0, i*20, 700);
  75.                  g.drawLine(0, i*20, 700, i*20);
  76.                 }
  77.              }
  78.             else if(object.equals("plant"))
  79.             {
  80.                 g.fillOval(x, y, 5, 5);
  81.             }
  82.             else if(object.equals("lake"))
  83.             {
  84.                 g.fillOval(x, y, 50, 50);
  85.             }
  86.             else if(object.equals("bus"))
  87.             {
  88.                g.fillOval(x-6, y-27, 15, 15);
  89.             }
  90.             else if(object.equals("lowres"))
  91.             {
  92.                 g.fillRect(x-6, y-27, 15, 15);
  93.             }
  94.             else if(object.equals("highres"))
  95.             {
  96.                 g.fillRect(x-6, y-27, 15, 15);
  97.             }
  98.             else if(object.equals("soffice"))
  99.             {
  100.                  g.fillOval(x-6, y-27, 15, 15);
  101.             }
  102.             else if(object.equals("loffice"))
  103.             {
  104.                  g.fillOval(x-6, y-27, 15, 15);
  105.             }
  106.             }
  107.      }
  108. }
  109.  
  110.  


However, after redrawing the screen, I get the following error.
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2.     at city.city.draw(city.java:183)
  3.     at city.city.<init>(city.java:37)
  4.     at city.randomizer.<init>(randomizer.java:7)
  5.     at city.GUI.actionPerformed(GUI.java:160)
  6.     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  7.     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  8.     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  9.     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  10.     at javax.swing.AbstractButton.doClick(Unknown Source)
  11.     at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
  12.     at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
  13.     at java.awt.Component.processMouseEvent(Unknown Source)
  14.     at javax.swing.JComponent.processMouseEvent(Unknown Source)
  15.     at java.awt.Component.processEvent(Unknown Source)
  16.     at java.awt.Container.processEvent(Unknown Source)
  17.     at java.awt.Component.dispatchEventImpl(Unknown Source)
  18.     at java.awt.Container.dispatchEventImpl(Unknown Source)
  19.     at java.awt.Component.dispatchEvent(Unknown Source)
  20.     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  21.     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  22.     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  23.     at java.awt.Container.dispatchEventImpl(Unknown Source)
  24.     at java.awt.Window.dispatchEventImpl(Unknown Source)
  25.     at java.awt.Component.dispatchEvent(Unknown Source)
  26.     at java.awt.EventQueue.dispatchEvent(Unknown Source)
  27.     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  28.     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  29.     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  30.     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  31.     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  32.     at java.awt.EventDispatchThread.run(Unknown Source)
  33.  
May 22 '11 #1
0 1212

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Scott Khan | last post by:
We have a Java Swing position. Need consultants ASAP. Position : Java Swing XML / SOAP - FX / Brokerage Requirements : Swing - JTrees / Jtables. other swing classes. etc. Java/ J2EE. JDK...
5
by: haran | last post by:
Hi, I have developed a project by using java Swings. Now i want to deploy that project into web. If its applet means we can deploy that by using browser. so plz help me to deploy the java swing...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: bytespaulraj | last post by:
We have RHEL5 server. We could able to run Java programs through telnet But When we run Java Swing (GUI) we are getting GTK error Please help us to eliminate this error.
10
by: yeshello54 | last post by:
Hey guys i am pretty new to java swing and need some help. I am developing a simple color chooser program in swing. I have a color panel that is connected to three sliders. red green and blue. but...
4
by: Ha Nguyen | last post by:
Hi all, I showed the popup when login fail but after that it was displayed again if i refresh that login page. Pls help me to not show popup when refreshing. Incidentally, is there any idea about...
2
by: Selva123 | last post by:
Hi All, Greetings. I want to test JAVA SWING application with PERL, do we have any module to do so (like win32::GUI for windows)?. or some third party free tools integrated with PERL? I am...
3
by: Akino877 | last post by:
Hello, I have a Java Swing application that I would like to be able to forward to or to run it - sorry, I am not sure if I am using the right term - from a JSP page. And I would like for my Java...
1
by: JanineXD | last post by:
Hey, I seem to have a problem on a Java Swing Program with ActionListener. I've tried to use getSource in our class but seem don't have an idea why it won't work when I tried to program at...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.