Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Click to start and drawString fonts

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    Germany
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool Click to start and drawString fonts

    Hello,

    I've looked around and can't find answers i understand. Is it possible to have an applet that doesn't start until the user has clicked on the applet to give it focus? Maybe even have an area inside the applet that the user has to click to start it to running.

    Also how do you manipulate fonts using drawString under the Graphics g method?

    They should have an emoticon that looks like the guys brain is smoking.

    [edit]
    I've found out how to make start/pause/reset buttons and make them work. But now i can't get the focus off the buttons and on to the applet so the keylistener works. Fonts are still eluding me as well
    [/edit]

    Thanks!
    Last edited by Campos; July 23rd, 2009 at 11:06 AM. Reason: Figured out some of it.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Click to start and drawString fonts

    For starting/stopping, override the start/stop methods, and there's a setFont method to change the font.
    ex:
    this.setFont(Font.getFont(Font.SERIF));

    Can you post your code so we can see what your key listener code is doing?

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Location
    Germany
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Click to start and drawString fonts

    Here it goes. The paddle movement worked just fine until i put in the buttons. What about text size, bold or italics?

    [edit]
    solved the paddle moving problem by adding this.setFocusable(true); and this.requestFocus(); right under the run method. Now the buttons work AND I can move the paddles. Still need help changing the text size,font and face, i think its called, under the bufferGraphics.drawString area down near the bottom.
    [/edit]

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class BallApp extends Applet implements KeyListener,Runnable, ActionListener
          {
              Graphics bufferGraphics;		
              Image offscreen;
              Dimension dim;
              int rad = 10;
              int bwidth = 20;
              int ovspeed = 3;
              int ohspeed = 3;
              int pwidth = 20;
              int pheight = 60;
              int posx = 320;
              int posy = 240;
              int rbound = (640 - bwidth - rad);
              int lbound = (bwidth + rad);
              int ubound = (bwidth + rad);
              int bbound = (480 - bwidth - rad);					
              int lpposx = (bwidth + (3*rad));
              int lpposy = 200;
              int lpuedge = (lpposy - rad);
              int lpredge = (lpposx + rad + pwidth);
              int lpbedge = (lpposy + rad + pheight);
              int lpledge = (lpposx - rad);
              int rpposx = (640 - bwidth - pwidth - 3*rad);
              int rpposy = 200;
              int rpuedge = (rpposy - rad);						
              int rpbedge = (rpposy + rad + pheight);
              int rpledge = (rpposx - rad);
              int rpredge = (rpposx + rad + pwidth);
              int p1score = 0;
              int p2score = 0;
              Button startButton, pauseButton, resetButton;
              boolean readygo = false;
              boolean startover = false;
              boolean lpmoveup = false;
              boolean lpmovedown = false;
              boolean rpmoveup = false;
              boolean rpmovedown = false;
              public void keyPressed(KeyEvent e){
                          int key = e.getKeyCode();
    	if (key == KeyEvent.VK_W){
    		lpmoveup = true;}
    	else if (key == KeyEvent.VK_S){
    	lpmovedown = true;}
    	if (key == KeyEvent.VK_UP){
    		rpmoveup = true;}
    	else if (key == KeyEvent.VK_DOWN){
    		rpmovedown = true;}
    	}
              public void lpMove(){
    	if (lpuedge <= ubound && lpmoveup){
    		return;}
    	if (lpbedge >= bbound && lpmovedown){
    		return;}
    	if (lpmoveup)	{
    		lpposy = lpposy - 4;}
    	if (lpmovedown){
    		lpposy = lpposy + 4;}
    	this.repaint();
    	}
              public void rpMove()	{
    	if (rpuedge <= ubound && rpmoveup){
    		return;}
    	if (rpbedge >= bbound && rpmovedown){
    		return;}
    	if (rpmoveup){
    		rpposy = rpposy - 4;}
    	if (rpmovedown){
    		rpposy = rpposy + 4;}				
    	this.repaint();
    	}
              public void keyReleased(KeyEvent e){
    	int key = e.getKeyCode();
    	if (key == KeyEvent.VK_W){
    		lpmoveup = false;}
    	else if (key == KeyEvent.VK_S){
    		lpmovedown = false;}
    	else if (key == KeyEvent.VK_UP){
    		rpmoveup = false;}
    	else if (key == KeyEvent.VK_DOWN){
    		rpmovedown = false;}
    	}
              public void keyTyped(KeyEvent e){}
              public void init(){
    	addKeyListener(this);
    	dim = getSize();
    	offscreen = createImage(dim.width,dim.height);
    	bufferGraphics = offscreen.getGraphics();
    	startButton = new Button ("Start");
    	pauseButton = new Button ("Pause");
    	resetButton = new Button ("Reset");
    	add(startButton);
    	add(pauseButton);
    	add(resetButton);
    	startButton.addActionListener(this);
    	pauseButton.addActionListener(this);
    	resetButton.addActionListener(this);
    	startButton.setFocusable(false);
    	pauseButton.setFocusable(false);
    	resetButton.setFocusable(false);
    	pauseButton.setEnabled(false);
    	resetButton.setEnabled(false);
    	}
              public void actionPerformed(ActionEvent evt){
    	if (evt.getSource() == startButton){
    		startover = false;
                                               readygo = true;
    		startButton.setEnabled(false);
    		pauseButton.setEnabled(true);
    		resetButton.setEnabled(true);}
    	if (evt.getSource() == pauseButton){
    		readygo = false;
    		startButton.setEnabled(true);
    		pauseButton.setEnabled(false);}
    	if (evt.getSource() == resetButton){
    		startover = true;
    		startButton.setEnabled(true);
    		pauseButton.setEnabled(false);}
    	}
              public void start(){
    	Thread th = new Thread(this);
    	th.start();}
              public void stop(){}
              public void destroy(){}
              public void run (){
    	while (true){
    	               do{
    		try{
    		       Thread.sleep (10);}
    		catch (InterruptedException ex){}
    		}
    	               while (readygo == false);
    	Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    	posx = posx + ohspeed;
    	posy = posy + ovspeed;
    	lpuedge = (lpposy - rad);
    	lpredge = (lpposx + rad + pwidth);
    	lpbedge = (lpposy + rad + pheight);
    	lpledge = (lpposx - rad);
    	rpuedge = (rpposy - rad);						
    	rpbedge = (rpposy + rad + pheight);
    	rpledge = (rpposx - rad);
    	rpredge = (rpposx + rad + pwidth);
    	AudioClip bonk = getAudioClip(getDocumentBase(), "bonk.wav");
    	AudioClip bunk = getAudioClip(getDocumentBase(), "bunk.wav");
    	AudioClip bink = getAudioClip(getDocumentBase(), "bink.wav");
    	AudioClip score = getAudioClip(getDocumentBase(), "score.wav");
    	repaint();
    	Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    	if (posx == lpledge && posy >= lpuedge && posy <= lpbedge){
    		bonk.play();
    		ohspeed = -Math.abs(ohspeed);}
    	if (posx == lpredge && posy >= lpuedge && posy <= lpbedge){
    		bonk.play();
    		ohspeed = Math.abs(ohspeed);}
    	if (posy == lpuedge && posx >= lpledge && posx <= lpredge){
    		bonk.play();
    		ovspeed = -Math.abs(ovspeed);}
    	if (posy == lpbedge && posx <= lpredge && posx >= lpledge){
    		bonk.play();
    		ovspeed = Math.abs(ovspeed);}
    	if (posx == rpledge && posy >= rpuedge && posy <= rpbedge){
    		bunk.play();
    		ohspeed = -Math.abs(ohspeed);}
    	if (posx == rpredge && posy >= rpuedge && posy <= rpbedge){
    		bunk.play();
    		ohspeed = Math.abs(ohspeed);}
    	if (posy == rpuedge && posx >= rpledge && posx <= rpredge){
    		bunk.play();
    		ovspeed = -Math.abs(ovspeed);}
    	if (posy == rpbedge && posx >= rpledge && posx <= rpredge){
    		bunk.play();
    		ovspeed = Math.abs(ovspeed);}
    	if (posx >= rbound){
    		bink.play();
    		ohspeed = -Math.abs(ohspeed);}
    	if (posx <= lbound){
    		bink.play();
    		ohspeed = Math.abs(ohspeed);}
    	if (posy >= bbound){
    		bink.play();
    		ovspeed = -Math.abs(ovspeed);}
    	if (posy <= ubound){
    		bink.play();
    		ovspeed =  Math.abs(ovspeed);}
    	if (lpmovedown == true || lpmoveup == true){
    		lpMove();}
     	if (rpmovedown == true || rpmoveup == true){
    		rpMove();}
    	if (posx <= lbound && posy >= (dim.height/2) - pheight && posy <= (dim.height/2) + pheight){
    		p2score++;
    		score.play();
    		posx = 320;
    		posy = 240;
    		lpposy = 200;
    		rpposy = 200;
    		repaint();
    		try{
    			Thread.sleep (5000);}
    		catch (InterruptedException ex){}
    		}
    	if (posx >= rbound && posy >= (dim.height/2) - pheight && posy <= (dim.height/2) + pheight){
    		p1score++;
    		score.play();
    		posx = 320;
    		posy = 240;
    		lpposy = 200;
    		rpposy = 200;
    		repaint();
    		try{
    			Thread.sleep (5000);}
    		catch (InterruptedException ex){}
    		}
    	if (startover == true){
    		readygo = false;
    		resetButton.setEnabled(false);
    		p1score = 0;
    		p2score = 0;
    		posx = 320;
    		posy = 240;
    		lpposy = 200;
    		rpposy = 200;
    		repaint();
    		}
    	}
              }
              public void paint(Graphics g){
    	bufferGraphics.setColor (Color.blue);
    	bufferGraphics.fillRect (0,0,dim.width,dim.height);
    	bufferGraphics.setColor (Color.white);
    	bufferGraphics.fillRect (((dim.width/2) - 5),0,10,dim.height);
    	bufferGraphics.fillOval (((dim.width/2) - (3*rad)),((dim.height/2) - (3*rad)),6*rad,6*rad);
    	bufferGraphics.setColor (Color.orange);
    	bufferGraphics.fillOval (posx - rad,posy - rad,2*rad,2*rad);
    	bufferGraphics.setColor (Color.gray);
    	bufferGraphics.fillRect (0,0,bwidth,dim.height);
    	bufferGraphics.fillRect (bwidth,0,dim.width,bwidth);
    	bufferGraphics.fillRect ((dim.width - bwidth),0,bwidth,dim.height);
    	bufferGraphics.fillRect (bwidth,(dim.height - bwidth),dim.width,bwidth);
    	bufferGraphics.setColor (Color.black);
    	bufferGraphics.fillRect (0,(dim.height/2) - pheight,bwidth,2*pheight);
    	bufferGraphics.fillRect ((dim.width - bwidth),(dim.height/2) - pheight,bwidth,2*pheight);
    	bufferGraphics.drawString ("SCORE:",bwidth,dim.height - (bwidth/2));
    	bufferGraphics.drawString ("P1: " + p1score,(dim.width/3),dim.height - (bwidth/2));
    	bufferGraphics.drawString ("P2: " + p2score,((2*dim.width)/3),dim.height - (bwidth/2));
    	bufferGraphics.setColor (Color.white);
    	bufferGraphics.fillRect (lpposx,lpposy,pwidth,pheight);
    	bufferGraphics.fillRect (rpposx,rpposy,pwidth,pheight);
    	g.drawImage(offscreen,0,0,this);	
    	}
              public void update(Graphics g){
    	paint(g);}
    }
    Last edited by Campos; July 24th, 2009 at 06:46 AM. Reason: Forgot something, Solved focus problem.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Click to start and drawString fonts

    add this before you draw your text (of course, you can change what kind of font you want):
    Font font = new Font(Font.SERIF",Font.PLAIN,14);
    g.setFont(Font font);

Similar Threads

  1. Replies: 1
    Last Post: June 21st, 2009, 11:05 AM
  2. How should i write and compile java with Ubuntu?
    By Talk Binary in forum Java IDEs
    Replies: 19
    Last Post: May 7th, 2009, 05:29 AM
  3. Replies: 1
    Last Post: May 7th, 2009, 04:31 AM
  4. [SOLVED] Java code to embedding xml tags at start and end of file
    By John in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 30th, 2009, 03:02 PM
  5. Best way to learn java for beginners
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: May 8th, 2008, 04:37 AM