2

I am creating a simple Sudoku game. Since this is my first "big" i want to do everything myself (without the NetBeans interface designer that i normally use to make GUIs). So for the GUI i created a class that extends JApplet and i drew a simple sudoku field in the paint() method.

Now i need to make 81 text fields each of which will take in 1 number. How do i position them on the screen? Also, i was thinking of making an array so i'll be able to change the enitre matrix of fields with one for loop.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Squeazer
  • 1,234
  • 1
  • 14
  • 33
  • 2
    P.S. This is the first "real" GUI i'm making, take that into consideration :) – Squeazer Feb 18 '12 at 15:12
  • If you want to use Swing controls why don't you use one of the "grid" layouts: GridLayout or GridBagLayout. This doesn't really help with painting gridlines. – cdc Feb 18 '12 at 15:14
  • @HovercraftFullOfEels : :-), My Bad, today is really one such day I shouldn't answer people, I never thought about multiplying. – nIcE cOw Feb 18 '12 at 15:22

1 Answers1

6

Suggestions:

  • Never draw directly in the paint method of a top-level component such as a JApplet, JFrame, JDialog, or the like.
  • If you need to do custom drawing, do this instead in the paintComponent(...) method override of a component that extends JComponent such as a JPanel or JComponent itself.
  • Your problem doesn't really require custom painting, at least not yet, and is much better and more simply solved by other means.
  • Use components such as JLabels, JTextFields, etc... and position them using the Swing layout managers (the other means noted above). You can find the tutorial on how to use this here: Laying Out Components in a Container
  • Layouts to focus on first include GridLayout for your Sudoku "cells" and BorderLayout for the overall GUI. Avoid the GridBagLayout and GroupLayout, at least when starting out.
  • Remember that you can create complex applications by nesting JPanels that each use a simple layout manager.
  • A simple way to "paint" gridlines is to set the background color of the the JPanel that uses GridLayout and holds the JTextFields to Color.BLACK, and be sure to give your GridBagLayout a small vertical and horizontal gap so that the black shows through. The tutorials listed above will show you how to do this.
  • If this were my application, I'd gear my GUI towards creating a JPanel that held the application. Then if I needed to display it in a JApplet, I'd create a very small application that subclasses JApplet and then in the init() method, add my Sudoku JPanel into the JApplet's contentPane. This way, if I wanted to instead display my app in a JFrame, all I'd need to do would be to create another small class that creates a JFrame and add's my Sudoku JPanel into the JFrame's contentPane, then call pack() on the JFrame, and then setVisible(true).

Regarding your question on how to add a JPanel to a JApplet, again the tutorials will show you how to do this. If you haven't linked to the tutorial's big index, you will want to do so: The Really Big Index.

A very simple example goes like so:

import java.lang.reflect.InvocationTargetException;
import javax.swing.*;

public class MyApplet extends JApplet {
   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               getContentPane().add(new MyJPanel());             
            }
         });
      } catch (InterruptedException e) {
         e.printStackTrace();
      } catch (InvocationTargetException e) {
         e.printStackTrace();
      }
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Good advice; [`CellTest`](http://stackoverflow.com/a/4151403/230513) maybe of interest going forward. – trashgod Feb 18 '12 at 15:21
  • Thanks for the advice! I'll do the entire thing in a JPanel. But i always had a problem inserting a JPanel into JApplet, it never worked. How do you do it properly? For instance, if i had a JPanel with a custom drawing and a few text fields how do i insert that into JApplet? – Squeazer Feb 18 '12 at 15:27
  • It's nice to know there are people out there willing to help :) Thanks for the support! – Squeazer Feb 18 '12 at 15:38
  • @mikera: since this is all usually done in the JApplet's init method, you wouldn't use a JApplet variable such as your myJApplet. You would just use `getContentPane().add(...)` or even simpler, `add(...)`. – Hovercraft Full Of Eels Feb 19 '12 at 05:07
  • Hey one quick question...i finally got it to work. I have 2 classes, one extends JApplet the other one JPanel. I did a custom drawing in the JPanel class (inside paintComponent() ) then i inserted that panel into the JApplet class with the code above. But it didnt work, untill i removed the (empty) paint() method i had in the JApplet class. Why is that? – Squeazer Feb 19 '12 at 13:16
  • 1
    @Squeazer: the `paint(Graphics g)` method in the JApplet was overriding the applet's own paint method. Since your method was empty, no painting could be done since the applet's own paint method was being bypassed. If you need to override the JApplet's `paint(...)` method (which I don't recommend in general), *be sure* to call the `super.paint(g);` in the method override so that the applet's original paint method can do all that is needed for its painting needs. – Hovercraft Full Of Eels Feb 19 '12 at 13:19
  • @Squeazer: I'm not sure I understand your question. If you construct your GridLayout with 9, 9 parameters, you'll get 9 columns and 9 rows, which should work for your purposes. Alternatively you could nest 9 JPanels that each use GridLayout(3, 3) in a larger JPanel that also uses GridLayout(3, 3). – Hovercraft Full Of Eels Feb 21 '12 at 14:08
  • 1
    @HovercraftFullOfEels I figured it out. I was only putting 1 or 2 object at a time to test it, and they filled the entire row / column. When i added 81 objects to the scene it filled the entire grid correctly. – Squeazer Feb 21 '12 at 18:10