Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

JButton displaying problem

Started by GekkoCube Sep 26, 2005 at 10:06 PM 0 replies 350+ views
Original Post
GekkoCube
GekkoCube
(this may need to be posted in the scripting language forum) I'm creating a JFrame and am trying to add a JButton. So far it compiles and runs, but i do not see the JButton anywhere! i think it might have to do something with my JFrame implementing the run(). so maybe im missing a paint or update call for the content-pane??? here is source.


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

public class typeright extends JFrame implements Runnable
{
   // constants.
	static final String ALPHA_FILE    = "res/alphabet.jpg";
	static final String VERION        = "1.0";
   static final int    NUM_CHARS     = 36;
   static final int    MAX_DISPLAY   = 16;
   static final int    LETTER_WIDTH  = 27;
   static final int    LETTER_HEIGHT = 40;

   // unchanging variables (could be constant, but isn't).
   Thread  thread      = null;
   Image   alphabet    = null;
	int     alphaWidth  = 0;
	int     alphaHeight = 0;
	int     letterPos[] = new int[MAX_DISPLAY * 2]; // num * (x,y)

	// changing variables.
	int     level       = 1;   // level 1 to N.
	int     updateRate  = 500; // in milli-seconds.

	int     numCorrect   = 0;  // correctly typed letters.
   int     numIncorrect = 0;  // mistyped letters.
	int     numMissed    = 0;	// letters dropped to the floor.
	int     numLetters   = 0;  // total letters displayed.

	int debugCount = 0;

   ///----------------------------------------------------------
   /// constructor
   ///----------------------------------------------------------
   public typeright(String title)
   {
      super(title);

      try
      {
         alphabet = Toolkit.getDefaultToolkit().createImage(ALPHA_FILE);
      }
      catch(Exception e)
      {
         e.printStackTrace();
         System.out.println("Couldn't load file: " + ALPHA_FILE);
      }

      System.out.println("typeright version " + VERION);

      if (alphabet != null)
      {
         alphaWidth  = alphabet.getWidth(this) / NUM_CHARS;
         alphaHeight = alphabet.getHeight(this);
         System.out.println("letter dimensions (" + alphaWidth +", "+alphaHeight+")");
      }

      letterPos[1] = 0;
   }

   ///----------------------------------------------------------
   /// start app thread
   ///----------------------------------------------------------
   public void start()
   {
      if (thread == null)
      {
         System.out.println("start thread");
         thread = new Thread(this);
         thread.start();
      }
   }

   ///----------------------------------------------------------
   /// stop app thread
   ///----------------------------------------------------------
   public void stop()
   {
      System.out.println("stop thread");
      thread = null;
   }

   ///----------------------------------------------------------
   /// reset game
   ///----------------------------------------------------------
   public void reset()
   {
      level       = 1;   // level 1 to N.
      updateRate  = 500; // in milli-seconds.

      numCorrect   = 0;  // correctly typed letters.
      numIncorrect = 0;  // mistyped letters.
      numMissed    = 0;  // letters dropped to the floor.
      numLetters   = 0;  // total letters displayed.

      debugCount = 0;
   }

   ///----------------------------------------------------------
   /// draw
   ///----------------------------------------------------------
   public void updateLetters()
   {
      letterPos[0] = 20;
      letterPos[1] += 1;;
   }

   ///----------------------------------------------------------
   /// imageUpdate - asynchronous callback for image-info requests
   ///----------------------------------------------------------
   public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
   {
      if (img == alphabet)
      {
         alphaWidth  = width / NUM_CHARS;
         alphaHeight = height;
      }

      //System.out.println("letter dimensions (" + alphaWidth +", "+alphaHeight+")");
      //System.out.println("debug: " + debugCount++);

      return true;
   }

   ///----------------------------------------------------------
   /// draw
   ///----------------------------------------------------------
	public void paint(Graphics g)
	{
      Graphics2D g2 = (Graphics2D)g;
      Dimension  d  = getSize();

      g2.setBackground(Color.white);
      g2.clearRect(0, 0, d.width, d.height);

      if (alphabet != null)
      {
         g2.setClip(letterPos[0], letterPos[1], alphaWidth, alphaHeight);
         g2.drawImage(alphabet, letterPos[0], letterPos[1], this);
      }

      //paintComponents(g);
   }

   ///----------------------------------------------------------
   /// run process
   ///----------------------------------------------------------
	public void run()
	{
		while (thread != null)
		{
		   // update letters.
		   updateLetters();

		   // draw letters.
			repaint();

			try
			{
            thread.sleep(updateRate);
			}
			catch (InterruptedException e) {;}
		}
	}

   ///----------------------------------------------------------
   /// button action listener.
   ///----------------------------------------------------------
   public void action()
   {
      Container content = this.getContentPane();
      content.setBackground(Color.white);
      content.setLayout(new FlowLayout());
      content.add(new JButton("Restart"));
   }

   ///----------------------------------------------------------
   /// mouse click listener.
   ///----------------------------------------------------------
   public void mouse()
   {
      addMouseListener( new MouseAdapter() {
         public void mouseClicked(MouseEvent e)
         {
            System.out.println("Mouse clicked.");
         }
      } );
   }

   ///----------------------------------------------------------
   /// main
   ///----------------------------------------------------------
   public static void main(String[] args)
   {
      typeright tw = new typeright("TypeRight v1.0");

      tw.setSize(400, 500);
      tw.setLocation(200, 200);

      // this allows user to exit the program.
      tw.addWindowListener( new WindowAdapter(){
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      } );

      tw.action();         // button listener.
      tw.show();
      tw.setVisible(true); // display reset button???
      tw.mouse();      // mouse listener.

      tw.start();
   }
}

// eof: typeright.java




Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.