2 Replies - 1526 Views - Last Post: 30 November 2009 - 08:16 PM Rate Topic: -----

#1 Ryan2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-November 09

Canvas in JFrame

Posted 30 November 2009 - 06:16 PM

I am trying to add canvas to my JFrame, but its not showing up and also what do i have to do for the JTextField to show up on the same Frame on West direction.
Thanks


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game implements ActionListener, MouseMotionListener, MouseListener{
	private Canvas c;
	private Alien[] alien;
	private int xPos;
	private int yPos;
	private int width;
	private int height;
	private Missile missile;
	private JMenuItem quit;
	private JButton play;
	private JTextField message;
	private JFrame f;

	/**
	 * Constructor for objects of class Game
	 */
	public Game()
	{
		f = new JFrame("Alien Attack");
		f.setLayout(new BorderLayout());
		f.setVisible(true);
		f.setSize(400,400);
		
		c = new Canvas();
		c.setVisible(true);
		f.add(c, BorderLayout.CENTER);
		c.addMouseListener(this);
		c.addMouseMotionListener(this);

		
		message = new JTextField();
		
		play = new JButton("Play");
		f.add(play, BorderLayout.SOUTH);
		play.addActionListener(this);

		setMenus();
		
		alien = new Alien[4];
		xPos = 0;
		yPos = 0;
		width = 0;
		height = 0;
		//missile = new Missile();
	}
	
	public void setMenus(){
		JMenuBar menubar = new JMenuBar();
		f.setJMenuBar(menubar);
		JMenu operation = new JMenu("Operation");
		menubar.add(operation);
		quit = new JMenuItem("QUIT");
		quit.addActionListener(this);
		operation.add(quit);
	}
		
	
	public void actionPerformed(ActionEvent e){
	}
	public void mouseMoved(MouseEvent e){
	}
	public void mouseClicked(MouseEvent e){
	}
	public void mouseDragged(MouseEvent e){
	}
	public void mousePressed(MouseEvent e){
	}
	public void mouseReleased(MouseEvent e){
	}
	public void mouseEntered(MouseEvent e){
	}
	public void mouseExited(MouseEvent e){
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Canvas in JFrame

#2 chaos386  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-February 09

Re: Canvas in JFrame

Posted 30 November 2009 - 06:51 PM


f.add(new JTextField("what ever",25), BorderLayout.WEST); 



for the west text field and replace the canvas with JPanel awt and swing dont mix i dont know why but ever where on line is saying the same thing ever thing you can do with canvas you can do with JPanel and it starts of dubble buffered
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Canvas in JFrame

Posted 30 November 2009 - 08:16 PM

You can have a Canvas in Swing
it is actually the only way, when making a game, to doubleBuffer and disable autorepaint

but don't add JComponent to the Canvas
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1