Ive been trying to make a program that should keep
the status of a hotel room layout.

main method should return this:
http://img202.imageshack.us/img202/969/layoutview.jpg

the cells should also be painted green if they are
available for renting
Yellow if they are booked
or red if the are not available

one class that stores the status of all these rooms
if they are available/booked or not available.

and a sub class of JPanel with 3 buttons that if you mark one
room on the grid and hit "booking" button it should be painted yellow
and if you mark another room and hit "pay" it should
be painted red

I have made an effort of making the button that should be printed
in the view area

public class GridButton extends JButton implements ActionListener {
    private boolean selected;

    public GridButton(int number) {
        super("" + number);
        setBackground(Color.GREEN);
        selected = false;
        setBorderPainted(false);
        addActionListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        if (selected) {
            g2.setStroke(new BasicStroke(10));
        }
        g2.drawRect(0, 0, getWidth(), getHeight());
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public void actionPerformed(ActionEvent e) {
        selected = !selected;
    }
}

I wonder how I can implement these as
in the view I want
or if there is an easier way of
doing this right in the view class itself?

Recommended Answers

All 5 Replies

so what exactly is your problem? the code that you gave does not seem to explain your problem at all. I understand the end goal, but our goal here is not to write your code for you but to help you overcome problems you might have.

my problem is that I need to make this grid
of squares but not sure how to do it
so I thought if I want to make a grid consisting of
Squares instead of JButtons as buttons
but im not sure on how to do it
I want to create a
for loop that prints out these but
if you create a square say

x = 10
y = 10
for the size of it

how do you make a for loop that prints out
in a similiar way as button

with a code like this

public class Gui extends JFrame(){
  public Gui(){
    super("frameTitle");

    JPanel gridPanel = new JPanel( new GridLayout(10,12) );
    for(int i=0 ; i<10 ; i++){
      for(int j=0 ; j<12 ; j++){
        gridPanel.add(new JButton("buttonText") );
      }
    }
    add(gridPanel);

    pack(); //let java handle the size
  }

  public static void main(String[] args){
    Gui gui = new Gui();
    gui.setVisible(true);
  }
}

if I understand your question correctly, you would like to instead of having a grid of buttons have a grid of squares or Jpanels, just replace new JButton with new JPanel.

public class Gui extends JFrame(){
  public Gui(){
    super("frameTitle");

    JPanel gridPanel = new JPanel( new GridLayout(10,12) );
    for(int i=0 ; i<10 ; i++){
      for(int j=0 ; j<12 ; j++){
        gridPanel.add(new JPanel());
      }
    }
    add(gridPanel);

    pack(); //let java handle the size
  }

  public static void main(String[] args){
    Gui gui = new Gui();
    gui.setVisible(true);
  }
}

I have a feeling that I completely missed your question. :)

I want to have squares numbered from 1-10
in each row so you have something like this

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
etc

and I need to implement a Mouse Listener
that should check if a special room is marked

and then with a new class

public class Controller extends JFrame implements MouseListener {
private JLabel room;
private JButton quit;
private JButton book;
private JButton sell;
private JComboBox date;

public void Controller()
{
setLayout(null);
setSize(100,200);
room = new JLabel("Room booking system");
quit = new JButton("Quit");
book = new JButton("Book");
sell = new JButton("Buy");

film.setBounds(60,30,120,30);
quit.setBounds(------);
quit.addMouseListener(this);
//should quit the whole program

book.setBounds(----);
book.addMouseListener(
// if you have marked a room and hit this button it shall be painted yellow and stored in the status class

sell.setBounds();
sell.addMouseListener() 
// if you marked a room and hit this button it shall be painted red and stored in the status class

add(quit);
add(book);
add(sell);
add(room);

so far if a room is marked and you press book
it should change color to yellow instead of green and this status will be
stored in a another class Status which Im still working on but i have problem
linking these two with eachother and the mouse listener

I just remembered that I had done something similar to this. this is how I did it.

view(){
         
      	
         for(int i = 0; i < 20;i++){
            for(int j = 0; j <20; j++){
               litPanConArray[i][j] = new littlePanelController(i,j,blankimg);
               pan.add(litPanConArray[i][j].getPanel());
               litPanConArray[i][j].setPreferredSize(new Dimension(17,17));
               litPanConArray[i][j].addMouseListener(new LitPanListener());
            }
         }
         ovcon = new overallController(this,litPanConArray);
         frame.add(pan);
         frame.setVisible(true);
         frame.pack();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
private class LitPanListener implements MouseListener{
          public void mouseExited(MouseEvent e){
         }
          public void mouseEntered(MouseEvent e){
         }
          public void mouseReleased(MouseEvent e){
         }
          public void mousePressed(MouseEvent e){
         }
          public void mouseClicked(MouseEvent e){
            littlePanel litp = (littlePanel)e.getComponent();
            littlePanelController litpcon = litp.getCon();
            clearAll();
            ship s = litpcon.getCurrentShip();
            if(!(s == null)){
               s.setSelected(true);
            }
            else {
               ovcon.createShip(litp.getRelX(),litp.getRelY());
            }
            ovcon.selected(litp.getRelX(),litp.getRelY());
            pan.repaint();
         }
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.