Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Question Closing a JFrame. setVisible etc, isn't working.

    Hi.
    I've made so that when you finish the game i made, it comes up a window saying play again, kinda. And i want so that when you press that button either a new window pops up or every int, timer etc resets(I'll work on the reseting way myself). What i want help with is that my main frame, the game frame, is made like this: The problem is: how do i make the "setVisible(false)" on the "frame" JFrame. Cause frame.setVisible(false) only works from within the public method. On other frames you can simply do setVisible(false) outside the method but it ain't working here.

    EDIT: And also, how do i make so that my character ain't going offscreen? So that he simply stops at the border..atm i have something like, if x > something make x something - 1 or so.. how do i make so that the char simply stops? i could make his velocity to 0, but it dosen't really work, either he's stuck at the same place or he can go outside the frame for 2 secs then get placed back inside, dosen't look that good.

    PHP Code:
    public class Mainclass {
    	
    	int x, y;
    	
    	public Mainclass(int startX, int startY){
    		x = startX;
    		y = startY;
    	}
    	
    	public static void main(String[] args){
    		new Menu();
    	}
    	
    	public Mainclass(){
    		JFrame frame = new JFrame("Walk anim");
    		frame.setVisible(true);
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 200);
    		Char cha = new Char();
    		frame.addKeyListener(cha);
    		frame.add(cha);
    		
    	}
    	
    
    	public Rectangle bounds(){
    		return (new Rectangle(x,y, 30, 30));
    	}
    	public Rectangle bounds2(){
    		return (new Rectangle(x,y, 20, 20));
    	}
    	public Rectangle bounds3(){
    		return (new Rectangle(x,y, 20, 20));
    	}
    
    	
    }
    Last edited by AndroidAppNewbie; 03-03-2011 at 05:15 PM.

  2. #2
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Bump, really want this to work :/

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    Quote Originally Posted by AndroidAppNewbie View Post
    Bump, really want this to work :/
    I was absolutely going to help you, but sorry, "bumping" really annoys me. Your post is only an hour old, and you're still a few hours from the forum's busy hours, so you need to show some patience. Doing things like this actually really decreases your chances of getting help.

    PS- Ain't ain't a word.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Actually, ain't is a word
    :)

    To the op, give dispose() a shot

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    Quote Originally Posted by sunde887 View Post
    Actually, ain't is a word
    I was waiting for somebody to do that. It might be a word in that it's in the dictionary, but like "y'all" and SMS-type speak, it's extremely unprofessional. It comes off as lazy, which even further decreases the OP's chances of getting help.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Lol, I see where you are coming from, however; I couldn't help myself.

  7. #7
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Okey, i've been trying some with both dispose and setVisible. The way of reseting all the values wasen't working aswell..so, how should i do make the JFrame "frame" close?

    And also that about how do i make the character stop at border(see first post).

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    Quote Originally Posted by AndroidAppNewbie View Post
    Okey, i've been trying some with both dispose and setVisible. The way of reseting all the values wasen't working aswell..so, how should i do make the JFrame "frame" close?
    You haven't posted any code that calls either of those methods, so we can't tell you what you're doing wrong. The code you posted uses classes to which we do not have access.

    When you post code, it should be in the form of an SSCCE. That's one class, as few lines as possible, that only deals with a single problem.

    Quote Originally Posted by AndroidAppNewbie View Post
    And also that about how do i make the character stop at border(see first post).
    You worry about that later. Take your problems one at a time. But again, you haven't posted any code that deals with that, so we can't really comment on it. Save that for its own thread, after you finish your first problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Okey, here's the code, and it's currently in my testing stage, moved the frame stuff from the public method to a public void one.. you'll see:
    and i'll try to do it as short as possible..


    The class where the main game frame gets created, i moved the main method to another class.
    PHP Code:
    public class Mainclass extends JFrame{
    	
    	int x, y;
    
    
    	public Mainclass(int startX, int startY){
    		x = startX;
    		y = startY;
    	}
    	public Mainclass(){
    		
    	}
    
    	
    	public void zain(){
    		JFrame frame = new JFrame("Walk anim");
    		frame.setVisible(true);
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 200);
    		Char cha = new Char();
    		frame.addKeyListener(cha);
    		frame.add(cha);
    		frame.setLocation(600, 250);
    	}
    	public void killer(){
    		dispose();
    		setVisible(false);
    	}
    	
    	public Rectangle bounds(){
    		return (new Rectangle(x,y, 30, 30));
    	}
    	public Rectangle boundy(){
    		return (new Rectangle(x,y, 10, 10));
    	}
    	public Rectangle bounds2(){
    		return (new Rectangle(x,y, 20, 20));
    	}
    	public Rectangle bounds3(){
    		return (new Rectangle(x,y, 20, 20));
    	}
    
    	
    }

    The game method, where all the game stuff occurs
    PHP Code:
    public class Char extends JPanel implements KeyListener, ActionListener{
    
            Timer t;
    	Timer levelstart;
    	Timer levelstart2;
    	Timer lifeswitch;
    	Timer shooter;
    	Timer restart;
    	
    	Mainclass you;
    	Mainclass bullet;
    	Mainclass enemy;
    	Mainclass enemy2;
    	Mainclass enemy4;
    	Mainclass enemy5;
    
    public Char(){
    		you = new Mainclass(x, y);
    		bullet = new Mainclass(bulletx, bullety);
         	enemy = new Mainclass(xe, ye);
         	setLocation(600, 250);
         	enemy2 = new Mainclass(xe2, ye2);
         	enemy4 = new Mainclass(xe4, ye4);
         	enemy5 = new Mainclass(xe5, ye5);
         	tic = new JLabel("Level 2 in: " + nextlevel);
         	life = new JLabel("Health left: ");
         	life.setBounds(330, 3, 100, 24);
         	life.setFont(new Font("Arial", Font.PLAIN, 14));
         	life.setForeground(Color.WHITE);
         	tic.setBounds(360, 120, 200, 40);
         	tic.setForeground(Color.RED);
         	tic.setFont(new Font("Arial", Font.PLAIN, 14));
         	setLayout(null);
    		setBackground(Color.BLACK);
    		addKeyListener(this);
    		setFocusable(true);
    		t = new Timer(5, this);
    		levelstart = new Timer(1000, this);
    		levelstart2 = new Timer(1000, this);
    		shooter = new Timer(1150, this);
    		restart = new Timer(500, this);
    		lifeswitch = new Timer(245, this);
    		setFocusTraversalKeysEnabled(false);
    		t.start();
    		add(tic);
    		add(life);
    		levelstart.start();
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    
    			
    		Graphics2D g2 = (Graphics2D) g;
    		
    		ship = new ImageIcon("imgs/SPACESHIP.png");
    		ship.paintIcon(this, g, you.x, you.y);
    		if(shoot){
    			g2.setColor(Color.CYAN);
    			g2.fillRect(bullet.x, bullet.y, 10, 10);
    			bullet.y = bullet.y;
    			bulletspeed = 2;
    
    
    // GAME OVER, the code isn't "that" good, so i have the end of game stuff in the paint method, cause the health int is connected to graphics etc..
    		if(health == 0){
    			t.stop();
    			vely = 0;
    			velx = 0;
    			vely2 = 0;
    			velx2 = 0;
    			velx3 = 0;
    			vely3 = 0;
    			vely4 = 0;
    			velx4 = 0;
    			levelstart.stop();
    			levelstart2.stop();
    			lifeswitch.stop();
    			restart.start();   // The delay before menu pops up.
    			
    		}
    			
    		}
    
    //Restart is a timer, which gets started when your health reaches 0, i made //a 1 sec delay from loosing to the menu gets showed cause it didn't work in //any other way.
    
    boolean frix = false;
    		public void actionPerformed(ActionEvent e) {	
    		
    		if(e.getSource() == restart){
    			if(!frix){
    				Mainclass man = new Mainclass();
    				man.killer();
    			
    			new winner();  // Creates a frame, saying Game Over, showing your score etc, from the winner frame you can back to menu or press play again, only the menu button works..
    			}
    			
    			frix = true;
    			restart.stop();
    		}
    //Timer t, tics every 5th millisecond, the timer that makes the game run smooth/run at all.
    if(e.getSource() == t){
    		repaint();
    		score++;
    		collision();
    		bullet.x += bulletspeed;
    		you.x += velx;
    		you.y += vely;
    		enemy.x += velx2;
    		enemy2.x += velx3;
    		enemy4.x += velx4;
    		enemy5.x += velx5;
    		velx2 = -2;
    		velx3 = -1;
    	}
    PHP Code:
    public class Menu extends JFrame implements ActionListener{
    	public static void main(String[] args){
    		new Menu();
    	}
    	protected JButton start;
    	private JButton howto;
    	protected JButton again;
    	
    	Timer swiz;
    	
    	private JButton exit;
    	protected JLabel finalscore;
    	private JLabel title;
    	
    	public Menu(){
    		super("Main Menu");
    		setLayout(null);
         	finalscore = new JLabel("");
         	finalscore.setBounds(30, 185, 195, 30);
         	finalscore.setForeground(Color.WHITE);
         	finalscore.setFont(new Font("Arial", Font.PLAIN, 15));
         	title = new JLabel("On the Run");
         	title.setBounds(30, 0, 250, 50);
         	title.setForeground(Color.GREEN);
         	title.setFont(new Font("Arial", Font.BOLD, 25));
    		start = new JButton("Start");
    		howto = new JButton("How to Play");
    		again = new JButton("Play Again");
    		exit = new JButton("Exit");
    		again.setEnabled(false);
    		start.setBounds(0, 60, 195, 30);
    		start.setEnabled(true);
    		howto.setBounds(0, 90, 195, 30);
    		again.setBounds(0, 120, 195, 30);
    		exit.setBounds(0, 150, 195, 30);
    		setVisible(true);
    		setResizable(false);
    		setSize(200, 250);
    		setLocation(550, 250);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		start.addActionListener(this);
    		howto.addActionListener(this);
    		more mo = new more();
    		again.addActionListener(mo);
    		exit.addActionListener(this);
    		
    		add(start);
    		add(again);
    		add(title);
    		add(exit);
    		add(finalscore);
    		add(howto);
    		Container con = getContentPane();
    		con.setBackground(Color.BLACK); 
    	
    
    	}
    Maybe it wasen't short, but i'm quite sure that's all the code involved. And my code isn't perfect so don't judge me too hard, unless there's a huge misstake somewhere.

    At ActionPerformed in the Char/main game class, i want the timer that shows up the Game over menu, also makes the game frame close. If i write setVisible(false) in the char/main game class, there's a blank frame left to investigate, which is the frame shown in the Mainclass class(top 1).
    Last edited by AndroidAppNewbie; 03-03-2011 at 05:52 PM.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    Quote Originally Posted by AndroidAppNewbie View Post
    Maybe it wasen't short, but i'm quite sure that's all the code involved.
    That's exactly what I told you NOT to do. I for one do not have time to wade through all that extra code. If you have a question about closing one JFrame from another, then that's the only code you should have: two JFrames, code that you think should close one from the other, one file. All that extra stuff makes it impossible for people to help you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Closing a JFrame
    By SBL in forum AWT / Swing
    Replies: 5
    Last Post: 11-23-2009, 02:54 PM
  2. Closing Popup JFrame in Applet
    By Arsenic in forum Java Applets
    Replies: 3
    Last Post: 04-10-2009, 08:37 PM
  3. JFrame (closing under a condition)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:17 AM
  4. not workin
    By varunthecool in forum NetBeans
    Replies: 3
    Last Post: 10-03-2007, 05:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •