Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Opening new class (JFrame) from old one  RSS feed

 
Ben Watt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to be able to:
1. click a button
2. close existing JFrame
3. run new class (which would result in opening a new JFrame)
4. bring the new class to the front.

I've tried all kind of methods, I'm kind of up against a wall right now.

I'm doing this for an independent study project so I might be on here relatively often from now till summer.

Any help is appreciated!!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post your attempt, and we'll try to point you in the right direction
 
Ben Watt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public FrontScreen(){
super("Start Game");
getContentPane().setBackground(Color.BLACK);
setExtendedState(MAXIMIZED_BOTH);
setUndecorated(true);
addMouseListener(this);
}

public static void main(String[] args){
FrontScreen window = new FrontScreen();
window.setVisible(true);
window.repaint();
}

public void paint(Graphics g){
super.paint(g);

Image title = new ImageIcon("Portal.png").getImage();
g.drawImage(title, 422, 200, null);

/* Get Images for menu buttons using the following method
*/
Image start = new ImageIcon("SGY.png").getImage();
g.drawImage(start, 500, 400, null);

Image option = new ImageIcon("O.png").getImage();
g.drawImage(option, 500, 500, null);

Image exit = new ImageIcon("EXIT.png").getImage();
g.drawImage(exit, 500, 600, null);

}

public void mouseClicked(MouseEvent e) {

xpos = e.getX();
ypos = e.getY();

if(xpos > 500 && xpos < 500 + 216 && ypos > 400 && ypos < 400 + 72){}
else if(xpos > 500 && xpos < 500 + 216 && ypos > 500 && ypos < 500 + 72){
System.exit(0);
OptionScreen option = new OptionScreen();
option.setExtendedState(MAXIMIZED_BOTH);
option.setVisible(true);
}
else if(xpos > 500 && xpos < 500 + 216 && ypos > 600 && ypos < 600 + 72){System.exit(0);}
}
}


(NEXT CLASS)


public class OptionScreen extends JFrame implements MouseListener{

public OptionScreen(){
super("Options");
getContentPane().setBackground(Color.BLACK);
setExtendedState(MAXIMIZED_BOTH);
setUndecorated(true);
addMouseListener(this);
}

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

}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assuming FrontScreen() extends JFrame:

1) treat the JFrame as a container only, do all of your image stuff on a JPanel, then add that to the JFrame
2) override the panel's paintComponent (not paint())
3) do not do this in paintComponent (or paint)
Image title = new ImageIcon("Portal.png").getImage();
get the image in the constructor, or other method, where it is retrieved once only. paint/paintComponent is called numerous times
4) don't use mouseClicked - it fires only if the x,y of mousePressed and the x,y of mouseReleased are identical,
so a slight movement between press/release and mouseClicked won't fire (generally better to use mousePressed)
5)
else if(...){
System.exit(0);//<--------------------------------you might want dispose() here
OptionScreen option = new OptionScreen();
option.setExtendedState(MAXIMIZED_BOTH);
option.setVisible(true);
}

that should be enough changes for the moment, see how you go
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!