AirPlane Seating Program

Need help with Jlabels and ArrayLists

Page 1 of 1

14 Replies - 7061 Views - Last Post: 08 March 2010 - 07:00 PM Rate Topic: ***** 1 Votes

#1 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

AirPlane Seating Program

Posted 08 March 2010 - 10:26 AM

I'm working on this Airplane Seating program. Basically, how it works is that you can see the rows and columns of labels on your panel and you can select and assign a seat to a person by entering their name and then pressing a button.

Here's what I tried. I created a Seat class and gave it these instance variables:
    public boolean taken = false;
    public String person = "";
    JLabel thisseat;


Though, I'm not sure you can just use the JLabel up there like that. I put it there since every seat needs to have a label. In my component class, I was trying this:

public class SeatComponent extends JComponent {

      public void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;

      ArrayList<Seat> myseats = new ArrayList(10);
      myseats<0>.thisseat  // dunno how to make this work


Creating the ArrayList worked, but how do I make it so I can set the label's location/size etc for each one using the Arraylist (as in the last line above)

This is a demo the prof. gave us (what it will be similar to, but with less seats)
http://i296.photobuc...I_R/seating.jpg

Any help is appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: AirPlane Seating Program

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: AirPlane Seating Program

Posted 08 March 2010 - 12:25 PM

to get an Object stored in an ArrayList by it's use the get(int index) method.
as:
//get the fObject in index 0
Seat seat = myseats.get(0);

or
myseat.get(0).callSomeMethod();

Was This Post Helpful? 0
  • +
  • -

#3 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Re: AirPlane Seating Program

Posted 08 March 2010 - 01:59 PM

View Postjapanir, on 08 March 2010 - 11:25 AM, said:

to get an Object stored in an ArrayList by it's use the get(int index) method.
as:
//get the fObject in index 0
Seat seat = myseats.get(0);

or
myseat.get(0).callSomeMethod();


This is what the end program should look like:
http://i296.photobuc...I_R/seating.jpg

[Good news is that I finally got the labels to show up]

Here's how I'm approachin it:
--> Created Seat class with variables and constructor:

// all imports here

public class Seat {
 
    public boolean taken = false;
    public String person = "";
    JLabel label = new JLabel(" ");
 
public Seat (boolean istaken, String thetext) {
    taken = istaken;
    label.setText(thetext);
 
//more methods below for getting variable values etc...

}



---> Viewer Class:
Created the frame, 2 panels, text field, button
Array created for all seats (28 total):
      Seat[] myseats= new Seat[28];
      myseats[0] = new Seat(false,"2A");
      myseats[1]=new Seat(false,"1A");
      myseats[2]=new Seat(false,"1B");
      myseats[3]=new Seat(false,"1C");
      myseats[4]=new Seat(false,"1D");   /// goes up to6D 



> Added 4 labels to test:

      for(int i=0; i<3; i++){
      buttonpan.add(myseats[i].label);
      }



> Added panel + button/text field to frame

The labels were showing:
http://i296.photobuc..._I_R/labels.jpg

But I have no idea how to align them and change their background??? It didn't allow me to use:

myseats[i].label.setBackGround(Color.blue)




I also tried changing their size and location (didn't work):
myseats[i].label.setSize(20,20);




They're supposed to show up like this :( [I'm only doing the economy part]
http://i296.photobuc...I_R/seating.jpg

Any tips/help would be appreciated, thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: AirPlane Seating Program

Posted 08 March 2010 - 03:40 PM

// Assuming label has public access...
myseats[i].label.setBackground(Color.blue)

// Notice the lowercase 'G'


Was This Post Helpful? 0
  • +
  • -

#5 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Re: AirPlane Seating Program

Posted 08 March 2010 - 03:53 PM

View PostDogstopper, on 08 March 2010 - 02:40 PM, said:

// Assuming label has public access...
myseats[i].label.setBackground(Color.blue)

// Notice the lowercase 'G'



Nah lol that was a typo on my part, it doesn't work. Anyway, I sorted it all out and I can see all the labels in order. Here is a screenshot:

Left Spacing

Is there a way to space out the label on the left side as well? If you have a look at the image, you'll see its only spaced out on the right side. I still need to do the backgrounds to the labels, any alternate idea about that? since this didnt work:

myseats[i].label.setBackground(Color.blue);



So maybe I'll be forced to create rectangle objects to place with the labels??? Would the mousepress listeners still work after that, that's what I'm worried about.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: AirPlane Seating Program

Posted 08 March 2010 - 03:58 PM

Why don't you look into GridLayout. It allows you to post things like they're in a grid with padding.
Was This Post Helpful? 0
  • +
  • -

#7 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: AirPlane Seating Program

Posted 08 March 2010 - 04:06 PM

Yep grid layout would be the best option to get everything nice and even.

JPanel panelname = new JPanel(new GridLayout(4,7));


Was This Post Helpful? 0
  • +
  • -

#8 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Re: AirPlane Seating Program

Posted 08 March 2010 - 05:04 PM

View Postm-e-g-a-z, on 08 March 2010 - 03:06 PM, said:

Yep grid layout would be the best option to get everything nice and even.

JPanel panelname = new JPanel(new GridLayout(4,7));



I'm at this point:
http://i296.photobuc...loredlabels.jpg

Everything is runnin smooth, is there a way to center-align the text in the label?? I tried a few methods but they didn't work...
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: AirPlane Seating Program

Posted 08 March 2010 - 05:07 PM

Can you post the full code? I'm still not quite sure about the interactions of your variables... Nevermind...I get it.

To make a JLabel by default center itself, construct it like this:
JLabel label = new JLabel("text",null,JLabel.CENTER);



Where the first parameter is the text, the second is the Image, and the third is a layout constant.

This post has been edited by Dogstopper: 08 March 2010 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Re: AirPlane Seating Program

Posted 08 March 2010 - 05:12 PM

View PostDogstopper, on 08 March 2010 - 04:07 PM, said:

Can you post the full code? I'm still not quite sure about the interactions of your variables...


Email me here: ***********

And I'll send you the code :D

Edit: THANKS!! That worked, I modified it in the original Seat Class...now moving on to the mouse press listeners :D

This post has been edited by Dogstopper: 08 March 2010 - 05:15 PM
Reason for edit:: Email removed

Was This Post Helpful? -1
  • +
  • -

#11 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: AirPlane Seating Program

Posted 08 March 2010 - 05:12 PM

Er, no. Please post your code here using the code tags :code:
Was This Post Helpful? 1
  • +
  • -

#12 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Re: AirPlane Seating Program

Posted 08 March 2010 - 05:35 PM

View Posterik.price, on 08 March 2010 - 04:12 PM, said:

Er, no. Please post your code here using the code tags :code:


It's ok he edited his post later and the solution worked (I edited my post). The code is pretty big to post here (2 classes), anyway I found out I have to start all over since I didn't read instructions :oops: Are we not allowed to post emails here?
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

Re: AirPlane Seating Program

Posted 08 March 2010 - 05:39 PM

Generally, no, as members are encouraged to work through the forums not through PMs or emails. As when everyone works through the forums, the threads become references for DICs in the future. However, when you work via email or PM, then only you and the person(s) helping you benefit, leaving out the entire community from learning from your problem.
Was This Post Helpful? 1
  • +
  • -

#14 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: AirPlane Seating Program

Posted 08 March 2010 - 05:40 PM

If you post your code here, you allow other people to learn from your mistakes. It also allows for us to help you out more, since we all don't have to individually email you :)
Was This Post Helpful? 1
  • +
  • -

#15 programmer4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-February 10

Re: AirPlane Seating Program

Posted 08 March 2010 - 07:00 PM

View Posterik.price, on 08 March 2010 - 04:40 PM, said:

If you post your code here, you allow other people to learn from your mistakes. It also allows for us to help you out more, since we all don't have to individually email you :)


Thanks, I'll post code here from now on..

It seems I'm completely lost with my assignment. I didn't read the instructions before and started doing it using completely different methods and structures. I've started over. This is the assignment (my question is below it)
=====================================================
Create a class Seat. Class Seat has the following variables: a string holding the name of the row (e.g. "A"), a String holding the name of the aisle or column (e.g. "5"), a String holding the name of the passenger, a boolean variable indicating whether the seat is occupied, the x and y position of the top left hand corner of the square used to draw the seat, a rectangle width variable, and a color variable used to draw the seat, appropriate accessor, mutator, and constructor methods, and a draw(Graphics2D g2) method that draws a rectangle using the x, y position, width, color, row, and occupied variables. Class seat should also have a the following method:

* public boolean contains(int x, int y): This method takes an x,y mouse position and determines if this point in inside the square boundaries of the seat. The method can simply use the x,y and width variables of class Seat and check if the given x,y mouse position is within the correct x,y seat range or you may use the "contains" method of class Rectangle2D.Double. NOTE: do not make class Seat extend class Rectangle2D.Double. Class Seat should make use of a rectangle to draw itself but a Seat is not (just) a rectangle.

# Create a class Airplane. Class Airplane has an arraylist of seat objects. It also has two integer variables rows and columns (or aisles, if you prefer this term) indicating the seat configuration. Class Airplane should also have variables x position, y position , width, height etc so that it can draw itself.

Class Airplane should also have the following methods:



* A constructor method that, among other things, creates the arraylist of seat objects and initializes each seat object with a default row, aisle, passenger name, x,y position etc.

* public void draw(Graphics2D g2): this method should draw a border (see figure above) of the airplane and then loop through all the seat objects, calling the draw method of each seat object.

* private Seat getSeat(int x, int y): This method takes a x,y mouse position and loops through all the seat objects to determine if the x,y point is inside one of the seat object squares. If so, return a reference to this seat object. If not, return null.

* public String assignSeat(String passenger, int x, int y): This method uses the getSeat method to determine if the given x,y mouse position is inside one of the seat object squares. If no seat object is found, return a null string reference. If a seat object is found, if the seat is unoccupied then check to see if a passenger with this name is not on this flight. If true, mark the seat as occupied and store the passenger name with the seat object. Concatenate the seat row and aisle string and return a reference to this string object. If the seat is occupied, then check if the given passenger name is the same as the passenger name stored in the seat object. If true, mark the seat as unoccupied and set the passenger name variable to the empty string. Return a null string reference. You might want to perform other checks like if the passenger name is non null and not the empty string etc. If it is, do nothing and return a null string reference.



# Create a subclass of class Airplane. The subclass adds the idea of emergency exit seats to an airplane. These seats are slightly larger in size, are drawn using a different color, and the airplane rectangle is drawn indicating where the exits are. You will need to override the draw method in the subclass. Next, create a subclass of this subclass that adds the idea of a first class section and an economy section to the airplane. You will again need to override the draw method accordingly. You may need to add new variables and methods. You may want to change the configuration of seat rows and columns in the first class section (for example, only 2 rows).
=====================================================
Where it says "public boolean contains(int x, int y): This method takes an x,y mouse position and determines if this point in inside the square boundaries of the seat."

I don't get what the teacher means by "Square boundaries of the seat"? Where were they declared?? I'm really confused on all parts where he talks about the x,y mouse positions.
=====================================================

I have this code up till now:
public class Seat {

    private int row;
    private char letter;
    private boolean taken;
    private String name;
    private int xtop,ytop;
    private Color rectcolor;
    private int rectWidth;


public Seat(int row, char letter) {
//	super();
	this.row = row;
	this.letter = letter;
}



public void book (String name){
        this.name=name;
        taken=true;
}


public String getName() {
    return name;
}

public boolean isTaken() {
        return taken;
}

public String toString(){

        return "seat: " +row+ " " + letter;
}

public boolean contains(int x, int y) {

        Point2D.Double p = new Point2D.Double(x, y);
        int xmax = xtop + rectWidth;
        int ymax = ytop + rectWidth;
        boolean check = false;

        if (x >0 && y>0) {
            if (x <= xmax && y <= ymax) check= true;
        }
        else if (x > xmax || y>ymax) check = false;

return check;

}

public void draw(Graphics2D g2, int xtop, int ytop, int width, Color sentcolor,int row, boolean taken) {



}

}


import java.awt.Graphics2D;
import java.util.ArrayList;

public class Airplane {

          ArrayList<Seat> myseats= new ArrayList();
          int rows,columns;
          int    xPos, yPos;
          double xRadius, yRadius;

          private Seat getSeat(int x, int y) {
              // dunno what to put here either
          }

          public void draw(Graphics2D g2) {
          }

          public String assignSeat(String passenger, int x, int y) {
          }

}

This post has been edited by programmer4life: 08 March 2010 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1