my problem in here is that i want to input chairs for the barber shop, there should be input chairs button and i will input how many chairs i want, and if for example i will input 4 and press ok, the barber chairs will become 4 also , and in my barbers also i want to input also how many barbers i want to input ... need help please

this is my code... Thanks for helping...

This is program , i uploaded it in here, because there are images in here that are needed

http://www.mediafire.com/?m3fe3rk2kq3j9w4

Recommended Answers

All 10 Replies

Could you post the code here or at least the important parts concerning the problem and also the error messages

most commonly used way:

keep a number of Chair elements, or an int counting the number of chairs, depending on the structure of your code,
as an instance variable and use either the constructor or a setter.

This is my code...
my problem is that my barbersBack image is static, because its only jpg format...
i would like that i will input some number of barber chairs and also i will input how many barbers that i want, so that when customers come in, for example they will occupy the barbers sit, please help me...

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class SleepingBarbers extends JPanel implements ActionListener{
    private ImageIcon asleep=new ImageIcon("barberAsleep.gif"),awake=new ImageIcon("barberAwake.gif");
    private static BufferedImage[][] images=new BufferedImage[1][8];
    private JButton addCustomer;
    private BufferedImage backdrop;
    private javax.swing.Timer timer=new javax.swing.Timer(30, this);
    private LinkedList<Customer> customers=new LinkedList<Customer>(), seatedAtQueue=new LinkedList<Customer>();
    private boolean queued[]=new boolean[3], seated[]=new boolean[3];
    private int customerCount=1, left=0, served=0;
    private Barber barbers[]=new Barber[3];
    private JLabel leftLabel=new JLabel("0"), servedLabel=new JLabel("0");

    public SleepingBarbers() throws IOException{
        super();
        setLayout(null);
        for(int ctr=0; ctr<3; ctr++){
            try{
                barbers[ctr]=new Barber();
                add(barbers[ctr]);
            }catch(IOException e){}
        }
        for(int ctr=0; ctr<8; ctr++){
            try{
                images[0][ctr]=ImageIO.read(new File("char"+0+"_"+ctr+".gif"));
            }catch(Exception e){}
        }
        backdrop=ImageIO.read(new File("barbersBack.jpg"));
        leftLabel.setForeground(Color.WHITE);
        add(leftLabel);
        leftLabel.setBounds(615,483,100,20);
        servedLabel.setForeground(Color.WHITE);
        add(servedLabel);
        servedLabel.setBounds(615,470,100,20);

        addCustomer=new JButton(new ImageIcon("addCust.jpg"));
        add(addCustomer);
        addCustomer.addActionListener(this);
        addCustomer.setBounds(487,530,263,29);
        addCustomer.setRolloverIcon(new ImageIcon("addCustOver.jpg"));
        addCustomer.setPressedIcon(new ImageIcon("addCustPressed.jpg"));
        addCustomer.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        barbers[0].setBounds(92,112,30,130);
        barbers[1].setBounds(71,235,30,130);
        barbers[2].setBounds(49,354,30,130);

        timer.start();
    }

    public void actionPerformed(ActionEvent e){
        Object src=e.getSource();
        timer.stop();
        if(src==addCustomer){
            Customer currentCust=new Customer((int)(Math.random()*1));
            add(currentCust);
            customers.add(currentCust);
            currentCust.setBounds(500,80,40,130);

        }
        if(src==timer&&!customers.isEmpty()){
            Collections.sort(customers);
            for(int ctr=0; ctr<customers.size(); ctr++){
                remove(customers.get(ctr));
            }
            for(int ctr=0; ctr<customers.size(); ctr++){
                add(customers.get(ctr));
            }
            for(int ctr=0; ctr<customers.size(); ctr++){
                for(int ctr1=0; ctr1<seatedAtQueue.size(); ctr1++){
                    (seatedAtQueue.get(ctr1)).findBarber();
                }
                (customers.get(ctr)).animate();
                if((customers.get(ctr)).exit){
                    remove(customers.get(ctr));
                    customers.remove(ctr);
                    //System.out.println("cutomer size "+customers.size());
                }
            }
        }
        repaint();
        validate();
        timer.restart();
    }

    private class Customer extends JPanel implements Comparable{
        private int  character, atQueue, move, time, barberNo;
        private boolean beenQueued, exit, atBarber, sit, done;
        private BufferedImage image;
        private LinkedList actions=new LinkedList<String>(), motions=new LinkedList<Integer>();
        //private Integer moveX, moveY;

        private Customer(int c){
            super(new BorderLayout());
            character=c;
            setOpaque(false);
            motions.add(2);
            actions.add("down");
            actions.add("enter");
            image=images[c][0];
        }

        public void animate(){
            if(!actions.isEmpty()){
                if(!motions.isEmpty()) move=(Integer)motions.getFirst();
                //System.out.println(move+" moving ");
                if(actions.getFirst().equals("down")){
                    setLocation(getX(), getY()+4);
                    if(!done)image=images[character][0];
                    else image=images[character][6];
                    move--;
                }else if(actions.getFirst().equals("up")){
                    setLocation(getX(), getY()-4);
                    image=images[character][1];
                    move--;
                }else if(actions.getFirst().equals("left")){
                    image=images[character][3];
                    setLocation(getX()-4, getY());
                    move--;
                }else if(actions.getFirst().equals("right")){
                    if(!done)image=images[character][2];
                    else image=images[character][7];
                    setLocation(getX()+4, getY());
                    move--;
                }else if(actions.getFirst().equals("exit")){
                    exit=true;
                    if(!atBarber){
                        left++;
                        leftLabel.setText(""+left);
                    }
                    return;
                }else if(actions.getFirst().equals("sit")){
                    if(!sit){
                        sit=true;
                        setLocation(getX(), getY()-1);
                        image=images[character][4];
                    }
                }
                if(actions.getFirst().equals("cut")){
                    image=images[character][4];
                    setOpaque(false);
                    if(time==5100){
                        done=true;
                        served++;
                        servedLabel.setText(""+served);
                        actions.removeFirst();
                        //System.out.println("waiting "+time);
                        seated[barberNo]=false;
                        barbers[barberNo].sleep();
                        motions.add(20);
                        actions.add("right");
                        if(barberNo==0){
                            motions.add(64);
                            actions.add("down");
                        }else if(barberNo==1){
                            motions.add(33);
                            actions.add("down");
                        }else if(barberNo==2){
                            motions.add(3);
                            actions.add("down");
                        }
                        motions.add(50);
                        actions.add("right");
                        motions.add(10);
                        actions.add("down");
                        actions.add("exit");
                    }else{
                        barbers[barberNo].wakeUp();
                        time+=30;
                    }
                    return;
                }
                if(actions.getFirst().equals("enter")){
                    findBarber();
                    if(atBarber) return;
                    //System.out.println(actions.getFirst());
                    actions.removeFirst();
                    for(int ctr=0; ctr<3; ctr++){
                        if(!queued[ctr]){
                            seatedAtQueue.add(this);
                            queued[ctr]=true;
                            beenQueued=true;
                            atQueue=ctr;
                            if(ctr==0){
                                motions.add(33);
                                actions.add("left");
                                actions.add("sit");

                            }else if(ctr==1){
                                motions.add(24);
                                actions.add("right");
                                actions.add("sit");

                            }else if(ctr==2){
                                motions.add(39);
                                actions.add("right");
                                actions.add("sit");
                            }
                            //System.out.println("queue at "+ctr);
                            break;
                        }
                    }
                    //System.out.println(actions.getFirst());
                    if(!beenQueued){
                        actions.add("wander");
                    }
                    return;
                }
                if(actions.getFirst().equals("wander")){
                    actions.removeFirst();
                    beenQueued=true;
                    actions.add("left");
                    motions.add(10);
                    actions.add("right");
                    motions.add(20);
                    actions.add("left");
                    motions.add(10);
                    actions.add("up");
                    motions.add(15);
                    actions.add("exit");
                    return;
                }

                if(!motions.isEmpty()) motions.set(0, move);
                if(move==0&&!motions.isEmpty()){
                    actions.removeFirst();
                    motions.removeFirst();
                }
            }
        }

        public void findBarber(){
            //System.out.println("hello");
            if(!motions.isEmpty()) return;
            for(int ctr=0; ctr<3; ctr++){
                if(!seated[ctr]){
                    seatedAtQueue.remove(this);
                    seated[ctr]=true;
                    atBarber=true;
                    barberNo=ctr;
                    actions.clear();
                    //System.out.println("going to "+ctr);
                    if(beenQueued){
                        setLocation(getX(), getY()+1);
                        queued[atQueue]=false;
                        if(atQueue==0){
                            //System.out.println("yep");
                            motions.add(43);
                            actions.add("left");
                        }else if(atQueue==1){
                            motions.add(101);
                            actions.add("left");
                        }else if(atQueue==2){
                            motions.add(115);
                            actions.add("left");
                        }

                    }else{
                        motions.add(77);
                        actions.add("left");
                    }
                    if(ctr==0){
                        motions.add(15);
                        actions.add("down");
                        motions.add(19);
                        actions.add("left");
                    }else if(ctr==1){
                        motions.add(46);
                        actions.add("down");
                        motions.add(24);
                        actions.add("left");
                    }else if(ctr==2){
                        motions.add(76);
                        actions.add("down");
                        motions.add(30);
                        actions.add("left");
                    }

                    actions.add("cut");
                    //System.out.println(actions.getFirst());
                    break;
                }
            }
        }

        public int compareTo(Object other){
            if(((Customer)other).getY()>getY()){
                return 1;
            }else{
                return -1;
            }
        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            if(actions.getFirst().equals("cut")) ((Graphics2D)g).drawImage(new ImageIcon("char0_5.gif").getImage(),0,0,this);
                else ((Graphics2D)g).drawImage(image, null,0,0);
        }
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        ((Graphics2D)g).drawImage(backdrop, null, 0, 0);
    }

    private class Barber extends JPanel{
        private ImageIcon barberImage;
        private Barber() throws IOException{
            super();
            setOpaque(false);
            sleep();
        }

        private void sleep(){
            barberImage=asleep;
        }

        private void wakeUp(){
            barberImage=awake;
        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            ((Graphics2D)g).drawImage(barberImage.getImage(),0,0,this);
        }
    }
    public static void createAndShowGUI(){
        try{
            JFrame x=new JFrame("Sleeping Barber's Problem");
            x.setSize(799,730);
            x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            x.add(new SleepingBarbers());
            x.setResizable(false);
            x.setVisible(true);
        }catch(IOException e){}

    }
    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                createAndShowGUI();
            }
        });
    }
}

you made your image variable static because it's a .jpg?
do you know what static in java means?

no i mean that my barbersBack jpg format image is not changable ... because its jpg, i want that whenever i run my program, it shouldnt have barbers chair at all, so i will input how many chairs i like and also how many barbers and appear it in my gui, how to do these??... pls help

not changable, then you should use "final" instead of static. static can still be changed.

keep a list of chairs, and instantiate that list in your constructor.
add an addChairmethod in which you add a chair each time it's called.

at the end of the adding, you'll need to repaint a part of your gui, or reset the value of a textArea or JComboBox (or what else you use, didn't read the entire code above)

hmf... i dont get it.. sir can you give me sample code to these... and i will understand it...

since I'm not totally clear on what exactly you're trying to do, not really. but:
when you instantiate your barber (if you have a Chair class)

private ArrayList<Chair> myChairs;
public Barber(){
// your code
myChairs = new ArrayList<Chair>();
}
....
public void addChair(){
  addChair(new Chair());
}
public void addChair(Chair newChair){
  myChairs.add(newChair);
}

ahm... about the image of the chair that i want to put , where will i put it, in that code sir?

please help...

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.