how to add jbuttons on canvas in java?

500 pts.
Tags:
Java applications
Swing
I am developing a computer game using java swing and adding graphics to it. now i want to add a menu list on the canvas like most of the other games. I don't want to add menu to the menubar . This is my code so far which has a blank screen: import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; /* * This is an example of a simple windowed render loop */ public class AddingMenu { static JFrame app; static JButton createMenuBtn; static Canvas canvas; // static boolean play=true; static BufferStrategy buffer; static BufferedImage bi; static Graphics graphics; static Graphics2D g2d; static Color background; static Random rand; static int x=10,y=10; public static void main( String[] args ) { // Create game window... app = new JFrame(); // app.setIgnoreRepaint( true ); app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Create canvas for painting... canvas = new Canvas(); //canvas.setIgnoreRepaint( true ); canvas.setSize( 640, 480 ); canvas.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) {} @Override public void mousePressed(MouseEvent e) { play=false; } @Override public void mouseExited(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseClicked(MouseEvent e) {} }); // Add canvas to game window... app.add(canvas); app.pack(); app.setVisible( true ); // Create BackBuffer... canvas.createBufferStrategy( 2 ); buffer = canvas.getBufferStrategy(); // Create off-screen drawing surface bi=new BufferedImage(640,480,1); System.out.println(bi.getHeight()+" "+bi.getWidth()); // Objects needed for rendering... graphics = null; g2d = null; background = Color.BLACK; rand = new Random(); run(); } protected static void run(){ while( play ) { /* String playerNames[]={"Sehwag","Gambhir","Kohli","Yuvraj","Raina","Rohit","Dhoni" ,"Pathan","Ashwin","Zaheer","Umesh"}; */ try { // clear back buffer... g2d = bi.createGraphics(); g2d.setColor( background ); g2d.fillRect( 0, 0, 639, 479); //app.add(canvas); // Blit image and flip... graphics = buffer.getDrawGraphics(); graphics.drawImage( bi, 0, 0, null ); if( !buffer.contentsLost() ) buffer.show(); // Let the OS have a little time... Thread.yield(); } finally { // release resources if( graphics != null ) graphics.dispose(); if( g2d != null ) g2d.dispose(); } y=10; //play=false; } } } my canvas object doesn't have any method to add jbuttons or any thing so how will be able to display anything on it?

Software/Hardware used:
Eclipse IDE

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following