Java: Drawing a graphical text doesn't work

Discussion in 'Mac Programming' started by jsmwoolf, Dec 10, 2011.

  1. jsmwoolf macrumors regular

    Aug 17, 2011
    I'm trying to devise a Sudoku solver in order to solve a problem. While I would normally program this in C, I decided to do this one in Java because it would easier to plug in values and keep track of values in order to code it correctly. While it would be easier to use pen and paper, I want to work on my programming skills.

    I'm using the Solving Sudoku hint page to give me ideas on how to code this. Anyway, I'm trying to imitate the possible numbers per cell display in order to help me. The problem is that I'm trying to use the Graphics drawString() function, but no text appears. I'm drawing rectangles to closely imitate a Sudoku board.

    The code here is a separate window that display the possible numbers per cell and this is where I want to implement this. There is a control class and the main window, but it isn't necessary to include those as this is a graphics issue.
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class Settings {
    	JFrame window = new JFrame("Analysis");
    	JPanel row1 = new JPanel();
    	Box[][] box = new Box[9][9];
    	public Settings()
    		FlowLayout layout = new FlowLayout();
    		GridLayout layout1 = new GridLayout(9,9);
    		for(int x = 0; x < 9; x++)
    			for(int y = 0; y < 9; y++)
    			box[x][y]=new Box();
    			box[x][y].setPreferredSize(new Dimension(51,51));
    class Box extends JComponent
    	boolean[] isThere = new boolean[9];
    	boolean isFixed= false; //Determines whether this is fixed.
    	public Box()
    		for(int x= 0; x < 9; x++)
    	public void paintComponent(Graphics g)
    		g.drawRect(0, 0, 50, 50);
    	void list(Graphics g)
    		Graphics2D g2d = (Graphics2D)g;
    			g2d.drawString("1", 0, 0);
    How can I get a graphical string to appear? Thanks.
  2. chown33 macrumors 604

    Aug 9, 2009
    Two questions to ask yourself about your code:
    1. What color will the text be drawn in?
    2. Where is the text drawn in relation to the point you're telling it to draw text at?

    #1 means you should think about what color has been set for the graphics, or whether one was set at all. Also think about what else was just drawn, and in what color. Does it appear properly or not?

    #2 means if you tell it to draw a string at 0,0, is the text drawn with its top-left at 0,0? Its bottom-left? Its baseline? Something else?

    The above is nothing but basic debugging skills and reading-the-docs skills. It's basic debugging because it breaks the problem down into sub-parts (e.g. what's the color? what was just drawn?). It's basic read-the-docs skills because reading the doc for drawString() should tell you where the string is drawn in relation to the given point. If it doesn't, you should expand your reading-the-docs to include a basic Java tutorial on drawing strings into Graphics.
  3. jsmwoolf thread starter macrumors regular

    Aug 17, 2011
    #1 - I plan on drawing the text it in black. The rectangle is drawn in black as well. The text will appear within the rectangle.
    #2 - Numbers such as 1,4, and 7 are to be drawn on the left side where 1 is top, 4 is middle, and 7 is bottom. numbers such as 2,5, and 8 are to be drawn in the middle where 2 is top, 5 is middle, and 8 is bottom. Numbers such as 3,6, and 9 are to be drawn on the right side where 3 is top, 6 is middle, and 9 is bottom. However, point (0,0) should be where the Box origin is, which is at the top-left.
  4. chown33 macrumors 604

    Aug 9, 2009
    You've missed the point.

    It doesn't matter what you plan on your code doing. What does it do now? If nothing is drawn, not even the rectangle, what might that suggest? If the rectangle is drawn, what is that evidence of? What color, if any, is anything currently being drawn in? If something is being drawn, exactly what is being drawn? If nothing is being drawn, might it be because you aren't setting a color for it to be drawn in?

    Seriously, break the problem down into little steps: set color, draw box. Confirm that each is happening.

    And I'm not asking where different numbers are drawn in different boxes. I'm asking where in relation to point 0,0, any text at all will be drawn, for any Box?

    If the top-left corner of a box is 0,0, and you tell it to draw text at 0,0, exactly where in relation to the top-left corner of that box will the text be drawn? Alternatively, if you tell it to draw text at a point in the middle of a box, say 25,25, where in relation to that point will the text be drawn, for any box?

Share This Page