StephNicolaou 32 Posting Whiz in Training

There isn't exactly anything wrong with my code, it's fine although the order isn't correct, I think, which makes my shapes appear behind the center panel. I'm not sure how to correct this as I've drawn my shapes under a paint method.

I've tried moving the paint method before and after the initialising method and repainted in either methods but it will then present only one or the other; the combos/txts, etc or the shapes.

So at the moment I've positioned the shape method after the initialising method and have not repainted in neither methods. The shapes will only appear if I resize the applet, why is that?

-Sorry about all of the comments and thanks to anyone that helps

import java.awt.*;
import java.awt.event.*;
import java.awt.Shape;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;

public class cinemabooking extends JApplet implements ActionListener, MouseListener
{
    //Variables


    JComboBox cboSeats;
    JComboBox cboTimes;
    JComboBox cboAge;

    JLabel lblTitle;
    JLabel lblSeats;
    JLabel lblTimes;
    JLabel lblAge;
    JLabel lblNoPeople;
    JLabel lblTotalCost;

    JLabel lblCenter;
    JLabel lblEast;
    JLabel lblWest;

    JTextField txtNoPeople;
    JTextField txtTotalCost;

    //Rectangle properties
    int rx = 120; //x position
    int ry = 100; //y position
    int rh = 30; //height
    int rw = 50; //width

    int x;
    int y;

    JPanel mainNorthPanel;
    JPanel eastPanel;
    JPanel southPanel;
    JPanel westPanel;
    JPanel centerPanel;
    JPanel row1;
    JPanel row2;

    public void init()
    {

        setSize(900,600);

        //Initialise Panels
               this.setLayout (new BorderLayout());

        //Initialise Combo Boxes

        //Seats Combo Boxes
        cboSeats = new JComboBox();
        String[] seatStrings = {"Left Block", "Middle Block", "Right Block"};

        for (int index = 0; index < seatStrings.length; index++)
        cboSeats.addItem(seatStrings[index]);

        //Times Combo Box
        cboTimes = new JComboBox();
        String[] timeStrings = {"1:00", "15:00", "17:00", "19:00", "21:00"};

        for (int index = 0; index < timeStrings.length; index++)
        cboTimes.addItem(timeStrings[index]);

        //Age Combo Box
        cboAge = new JComboBox();
        String[] ageStrings = {"Adult", "Child", "OAP"};

        for (int index = 0; index < ageStrings.length; index++)
        cboAge.addItem(ageStrings[index]);

        //Initialise labels
        lblSeats = new JLabel("  Seat Block:");
        lblTimes = new JLabel("  Times Available:");
        lblAge = new JLabel("  Age:");
        lblTitle = new JLabel("Cinema Booking");

        lblNoPeople = new JLabel("  No of People:");
        lblTotalCost = new JLabel("  Total Cost:");

        lblEast= new JLabel("e:");
        lblWest = new JLabel("w:");
        lblCenter = new JLabel("cdgfasdgas:");

       //Initialise Text boxes
       txtNoPeople = new JTextField(15);
       txtTotalCost = new JTextField(15);

//NORTH PANEL
               mainNorthPanel = new JPanel();
               mainNorthPanel.setLayout (new BorderLayout());

                add(lblTitle, BorderLayout.NORTH);
                add(lblAge, BorderLayout.NORTH);
                add(lblSeats, BorderLayout.NORTH);
                add(lblTimes, BorderLayout.NORTH); //Add labels

                add(cboSeats, BorderLayout.NORTH);
                add(cboAge, BorderLayout.NORTH);
                add(cboTimes, BorderLayout.NORTH); //Add combo boxes

                row1 = new JPanel();
                row2 = new JPanel();

                row1.add(lblTitle);
                row2.add(lblSeats);
                row2.add(cboSeats);
                row2.add(lblTimes);
                row2.add(cboTimes);
                row2.add(lblAge);
                row2.add(cboAge);

                mainNorthPanel.add(row1, BorderLayout.NORTH);
                mainNorthPanel.add(row2, BorderLayout.SOUTH);

                add(mainNorthPanel, BorderLayout.NORTH);

                //EAST PANEL
                eastPanel = new JPanel();
                eastPanel.setLayout (new BorderLayout());

                add(lblEast, BorderLayout.EAST);
                add(eastPanel, BorderLayout.EAST);

                eastPanel.add(lblEast);

               //WEST PANEL
               westPanel = new JPanel();
               westPanel.setLayout (new BorderLayout());

                add(lblWest,BorderLayout.WEST);

                westPanel.add(lblWest);

                add(westPanel, BorderLayout.WEST);

                //SOUTH PANEL
                southPanel = new JPanel();
                southPanel.setLayout (new GridLayout(1,4));

                add(lblNoPeople,BorderLayout.SOUTH);
                add(lblTotalCost, BorderLayout.SOUTH);
                add(txtNoPeople, BorderLayout.SOUTH);
                add(txtTotalCost, BorderLayout.SOUTH);

                southPanel.add(lblTotalCost);
                southPanel.add(txtTotalCost);
                southPanel.add(lblNoPeople);
                southPanel.add(txtNoPeople);

                add(southPanel, BorderLayout.SOUTH);

                //CENTER PANEL

                centerPanel = new JPanel();
                centerPanel.setLayout (new BorderLayout());

                add(lblCenter,BorderLayout.CENTER);

                centerPanel.add(lblCenter);
                add(centerPanel, BorderLayout.CENTER);
                this.addMouseListener(this);

               validate();

        //repaint();


}

       public void paint (Graphics graf)
       {

              Shape[][] rect = new Shape[4][6];


              for(int i = 0; i < rect.length; i++) // i = row
              {
                  for(int j = 0; j < rect[0].length; j++) // j = column
                  {

                      //if (centerPanel!= null)
                      //{
                          //System.out.println (centerPanel.getX());
                          //get center of centerpanel + the width divided by 2 of center panel (making grid start position from center)
                          //so minus half of the grid to make in center of panel and get the position of grid
                          graf.drawRect(rx, ry, rw * i, rh * j);
                          //((centerPanel.getX()+((centerPanel.getWidth()/2)-((rw/2) * rect.length)) + rx), ((centerPanel.getY()+((centerPanel.getHeight()/2)-((rh/2) * rect[0].length)) + ry)), (rw * i), (rh * j));

                      //}
                  }
              }

             // if(  (x <= rw*(i+1)) && (x > rw*(i)) )
              // {
              //   if(  (y <= rh*(j+1)) && (y > rh*(j)) )
              //    {
              //        g.fillRect(i*rw, j*rh, rw, rh);
              //    }
              // }
//repaint();


                      Shape[][] rect2 = new Shape[6][5];


                      for(int i = 0; i < rect2.length; i++) // i = row
                      {
                          for(int j = 0; j < rect2[0].length; j++) // j = column
                          {

                              //if (centerPanel!= null)
                              //{
                                  //System.out.println (centerPanel.getX());
                                  //get center of centerpanel + the width divided by 2 of center panel (making grid start position from center)
                                  //so minus half of the grid to make in center of panel and get the position of grid
                                  graf.drawRect(rx+200, ry, rw * i, rh * j);
                                  //((centerPanel.getX()+((centerPanel.getWidth()/2)-((rw/2) * rect2.length)) + rx), ((centerPanel.getY()+((centerPanel.getHeight()/2)-((rh/2) * rect2[0].length)) + ry)), (rw * i), (rh * j));

                              //}
                          }
                      }

                     // if(  (x <= rw*(i+1)) && (x > rw*(i)) )
                      // {
                      //   if(  (y <= rh*(j+1)) && (y > rh*(j)) )
                      //    {
                      //        g.fillRect(i*rw, j*rh, rw, rh);
                      //    }
                      // }
        //repaint();

                      Shape[][] rect3 = new Shape[4][6];


                      for(int i = 0; i < rect3.length; i++) // i = row
                      {
                          for(int j = 0; j < rect3[0].length; j++) // j = column
                          {

                              //if (centerPanel!= null)
                              //{
                                  //System.out.println (centerPanel.getX());
                                  //get center of centerpanel + the width divided by 2 of center panel (making grid start position from center)
                                  //so minus half of the grid to make in center of panel and get the position of grid
                                  graf.drawRect(rx+500, ry, rw * i, rh * j);
                                  //((centerPanel.getX()+((centerPanel.getWidth()/2)-((rw/2) * rect2.length)) + rx), ((centerPanel.getY()+((centerPanel.getHeight()/2)-((rh/2) * rect2[0].length)) + ry)), (rw * i), (rh * j));

                              //}
                          }
                      }

                     // if(  (x <= rw*(i+1)) && (x > rw*(i)) )
                      // {
                      //   if(  (y <= rh*(j+1)) && (y > rh*(j)) )
                      //    {
                      //        g.fillRect(i*rw, j*rh, rw, rh);
                      //    }
                      // }
        //repaint();

        }


    public void mouseClicked(MouseEvent e)
    {
        x = e.getX();
        y = e.getY();
    }

    public void mouseExited(MouseEvent e)
    {

        }

    public void mouseEntered(MouseEvent e)
    {

        }

    public void mouseDragged(MouseEvent e)
    {

        }

    public void actionPerformed (ActionEvent ev)
    {

        }

    public void mouseReleased(MouseEvent e)
    {

        }

    public void mousePressed(MouseEvent e)
    {

    }

}

Cleo

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.