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. :)

Recommended Answers

All 7 Replies

Change your logic so you don't create a new JFrame in Drop's constructor.
Or in the mousePressed method don't create a new Drop object.

I am still learning. how would I go about doing that? If I don't have the new Drop object, then I don't know how else to communicate the boolean value of play back to the Drop class.

If the DropMenu class needs a reference to the Drop class, you could pass it to DropMenu's constructor and save it.

how do you mean? do you mean that when I call the menu method from the Drop class I pass along the constructor used in the main method of the Drop class and then just save it to a variable in the DropMenu class?

a reference to the Drop class, you could pass it to DropMenu's constructor

private DropMenu dm = new DropMenu(this); // 'this' refers to the Drop class

@sirlink99

if you switch JComponents in Top-Level Containser, then don't forget to call

revalidate();
repaint();

or maybe better ways is look for CardLayout

Thanks that worked. here are the new codes for future reference:

Main Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Drop {
	private JFrame f = new JFrame ("The Drop");
	private DropMenu dm = new DropMenu ();
	private DropGame dg = new DropGame ();
	
	public Drop (){
		
		
		f.add (dm);
		
		f.setSize (600,400);
		f.setVisible (true);
		dm.menu(this);
		
	}
	
	public void game (boolean play){
		if (play == true){
			System.out.println ("Game Method");
			f.add(dg);
			f.remove(dm);
			f.validate();
			dg.game ();
		}
		else {
			
		}
		
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Drop d = new Drop ();
		
	}

}

Menu Code:

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;
	
	Drop d;
	
	public void menu(Drop drop) {
		// TODO Auto-generated method stub
		d = drop;
		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){
			
			
			play = true;
			System.out.print ("Activated");
			
			d.game (play);
		}
		
		
		
	}
	
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	
	
}

Game Code:

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 the 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.