Jump to content

Java Elevator Program (Problem)

PampaZiya
Go to solution Solved by PampaZiya,

Oh man , I really don't like Java.

OT : Why would a castle have elevators?

 

Anyway , looking over the code , let's see if I can spot anything.

 

It's solved mate :) I figured it out. I made the elevator class implement Runnable and make the buttons changes the x and y in elevator making it "run" everytime they changed. Works beautifully! Now I got 2 elevators, one on each side with 4 floors.

 

Actually It's just a java project and why not ? :P Drawing the castle was fun ! I'm gonna write "Super Mario Elevator" in the middle :P

So my java project is to draw a building and make an elevator for it.

 

First of all this is how the program looks:

 

2efncph.png

 

And after I click on "L2" this is how it looks:

 

 

34nijd2.png

 

My problem is the elevator. Whenever I click "L2" or "L1" it will just disappear.... And for some reason there's the name of the floor i chosen and the window just disappears too.. 

 

First of all there's the landscape... then there's the windows ... then the elevator.

 

I don't understand why it's just disappearing and not appearing anywhere again and why did it crop where it was? Why did the whole picture kinda move?

 

I tried to delay the thread (puttng it to 100) but that didn't fix it, it'll just take more time and it'll disappear. 

 

Any thoughts?

 

 

CODE:

 

Design.java

package learnG;import java.awt.Color;import java.awt.Component;import java.awt.Graphics;import java.awt.Rectangle;import javax.swing.JFrame;import javax.swing.JPanel;public class Design extends JFrame {		Canvas canvas = new Canvas();	static public Elevator elevator = new Elevator(250, 280);	public Design () {		this.setLayout(null);		this.add(elevator);		int k = 3;		for(int i=0; i<=300; i+=150){			this.add(new Window(250, 280+i,"L", k));			k--;		}		k=3;		for(int i=0; i<=300; i+=150){			this.add(new Window(1250, 280+i,"R", k));			k--;		}		this.add(canvas); 		this.setVisible(true);		this.setSize(1600,1000);		this.setLocationRelativeTo(null);		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		this.setBackground(new Color(168,186,186));		this.setResizable(false);	}		class Canvas extends JPanel implements Runnable{		int width;		int height;				public Canvas(){			width = getSize().width;			height = getSize().height;			setBounds(0,0,1600,1000);		}				public void paintComponent(Graphics g){			//clouds			g.setColor(Color.WHITE);			g.fillOval(500, 100, 200, 100);			g.fillOval(640, 100, 170, 70);			g.fillOval(400, 100, 170, 70);			g.fillOval(1000, 300, 200, 100);			g.fillOval(1140, 300, 170, 70);			g.fillOval(900, 300, 170, 70);						g.fillOval(1500, 100, 200, 100);			g.fillOval(1640, 100, 170, 70);			g.fillOval(1400, 100, 170, 70);						//birds			g.setColor(Color.BLACK);			g.drawArc(1000, 100, 50, 20, 0, 180);			g.drawArc(1050, 100, 50, 20, 0, 180);						g.drawArc(1100, 50, 50, 20, 0, 180);			g.drawArc(1150, 50, 50, 20, 0, 180);						//foundation			int x[] = {200, 200, 1400, 1400, 					1450, 1450, 1150, 1150, 1200, 1200, 					1150, 1150, 1100, 1100, 1050, 1050, 1000,					1000, 950, 950, 650, 650, 600, 600, 550, 550,					500, 500, 450, 450, 400, 400, 450, 450,					150, 150, 200};			int y[] = {400, 900, 900, 400, 					350, 250, 250, 350, 400, 500, 					500, 450, 450, 500, 500, 450, 450,					500, 500, 350, 350, 500, 500, 450, 450, 500,					500, 450, 450, 500, 500, 400, 350, 250,					250, 350, 400};			g.setColor(new Color(233,234,226));			g.fillPolygon(x, y, x.length); 			g.setColor(Color.GRAY);			g.drawPolygon(x, y, x.length);			g.setColor(new Color(68,11,2));						int c1x[] = {150, 300, 450};			int c1y[] = {250, 100, 250};			g.fillPolygon(c1x, c1y, c1x.length); 						int c2x[] = {1450, 1300, 1150};			int c2y[] = {250, 100, 250};			g.fillPolygon(c2x, c2y, c2x.length);						int c3x[] = {950, 800, 650};			int c3y[] = {350, 200, 350};			g.fillPolygon(c3x, c3y, c3x.length);						//sun			g.setColor(Color.YELLOW);			g.fillOval(1450,-100,200,200);						//ground			g.setColor(Color.GREEN);			g.fillRect(0,900,1600,100);						//pathway			g.setColor(new Color(255, 170, 130));			int z[] = {700, 900, 1000, 600};			int t[] = {900, 900, 1000, 1000};			g.drawPolygon(z, t, z.length);			g.fillPolygon(z, t, z.length); 						//door					g.setColor(new Color(68,11,2));			g.fillArc(700, 750, 200, 100, 0, 180);			g.fillRect(700, 800, 200, 100);					}		@[member=OverRide]		public void run() {			// TODO Auto-generated method stub			this.repaint();		}				}}

Window.java

package learnG;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JPanel;public class Window extends JPanel{		public int floor;		public JButton button;		public int x, y;	public Window (int x, int y, String no, int floor){		this.x = x;		this.y = y;		setBounds(x,y,200,100);		button = new JButton(no+floor);		setLayout(null);		add(button);		button.setBounds(100, 50, 30,30);		button.setBorder(null);		button.setBackground(Color.YELLOW);		button.addMouseListener(new MouseHandler());		this.floor = floor;	}		public void paintComponent(Graphics g){		g.setColor(new Color(68,11,2));		g.fillRect(0, 0, 100, 100);			}			private class MouseHandler implements MouseListener{		public void mouseClicked(MouseEvent arg0) {			button.setBackground(Color.ORANGE);				}				public void mouseExited(MouseEvent e){			button.setBackground(Color.YELLOW);		}		public void mousePressed(MouseEvent e){			button.setBackground(Color.ORANGE);			Design.elevator.x = x;			Design.elevator.y = y;			Design.elevator.run();		}		public void mouseReleased(MouseEvent e){			button.setBackground(Color.YELLOW);		}		public void mouseEntered(MouseEvent arg0) {			button.setBackground(Color.ORANGE);		}	}} 

Elevator.java

package learnG;import java.awt.Color;import java.awt.Graphics;import javax.swing.JPanel;public class Elevator extends JPanel implements Runnable{	public int currx, curry, x, y, floor;	public String no;		public Elevator(int x, int y){		setBounds(x, y, 680, 550);		currx = 0;		curry = 0;	}		public void paintComponent(Graphics g){			g.setColor(new Color(68,11,2));		g.fillRect(currx+0, curry+0, 100, 100);		g.setColor(Color.orange);		for(int i=0;i<5;i++){			g.drawRect(currx+i, curry+i, 100-2*i, 100-2*i);		}		g.drawLine(currx+50,curry,currx+50,curry+100);	}	@[member=OverRide]	public void run() {		// TODO Auto-generated method stub		try{			if(y>curry+280){				curry++;				while(y>curry+280){				curry++;				Thread.sleep(5);				this.repaint();				}			}		} catch(InterruptedException e){			System.out.println(e.getMessage());		}	}} 

EDIT:

 

Please reread the post I fixed some stuff

Link to comment
Share on other sites

Link to post
Share on other sites

Oh man , I really don't like Java.

OT : Why would a castle have elevators?

 

Anyway , looking over the code , let's see if I can spot anything.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Oh man , I really don't like Java.

OT : Why would a castle have elevators?

 

Anyway , looking over the code , let's see if I can spot anything.

 

It's solved mate :) I figured it out. I made the elevator class implement Runnable and make the buttons changes the x and y in elevator making it "run" everytime they changed. Works beautifully! Now I got 2 elevators, one on each side with 4 floors.

 

Actually It's just a java project and why not ? :P Drawing the castle was fun ! I'm gonna write "Super Mario Elevator" in the middle :P

Link to comment
Share on other sites

Link to post
Share on other sites

It's solved mate :) I figured it out. I made the elevator class implement Runnable and make the buttons changes the x and y in elevator making it "run" everytime they changed. Works beautifully! Now I got 2 elevators, one on each side with 4 floors.

 

Actually It's just a java project and why not ? :P Drawing the castle was fun ! I'm gonna write "Super Mario Elevator" in the middle :P

Well, whenever you've solved it, please make a post saying so and click "Mark Solved" on that post before waiting for someone to reply :P That way the rest of us won't spend time on something you don't need us to solve anymore. I'm glad it worked out for you.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×