Hello. I am trying to make a program that has a menu. When you click play , it should (for now) output the text "In Game". It does that. However, it prints the text on a new JFrame. I would like it to print it on the same frame. How would I go about fixing this?
This is the main code that contains the frame and launched the menu and the game.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Drop {
private JFrame f;
private DropMenu dm = new DropMenu ();
private DropGame dg = new DropGame ();
public Drop (){
f = new JFrame ("The Drop");
f.add (dm);
f.setSize (600,400);
f.setVisible (true);
dm.menu();
}
public void game (boolean play){
if (play == true){
f.remove(dm);
f.add(dg);
dg.game ();
}
else {
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Drop d = new Drop ();
}
}
This code is for the menu
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class DropMenu extends JPanel implements MouseListener, MouseMotionListener{
boolean play = false;
public void menu() {
// TODO Auto-generated method stub
addMouseListener (this);
addMouseMotionListener (this);
repaint ();
}
Color bc = Color.red;
public void paintComponent (Graphics g){
super.paintComponent(g);
// Play Button
g.setColor (bc);
g.fillRect (50,50,150,50);
// button text
g.setColor (Color.black);
g.setFont (new Font ("Arial", 30,30));
g.drawString("Play", 95, 85);
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
int mx, my;
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
mx = e.getX();
my = e.getY();
if (mx < 200 && mx > 50 && my < 100 && my > 50){
bc = Color.yellow;
}
else {
bc = Color.red;
}
//System.out.println ("Mouse Coordinates: (" + mx + ", " + my + ")");
repaint ();
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if (bc == Color.yellow){
Drop d = new Drop ();
play = true;
System.out.print ("Activated");
d.game(play);
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
and this is the code for the game
import java.awt.Graphics;
import javax.swing.*;
public class DropGame extends JPanel{
public void game() {
// TODO Auto-generated method stub
repaint ();
}
public void paintComponent (Graphics g){
super.paintComponent(g);
g.drawString("In Game", 250, 150);
}
}
Thanks for any help. :)