1

This is my code, it''s just a small part of a program for a Tic Tac Toe game. This part is supposed to draw the board on the applet, but everytime, nothing appears.

import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class TTTb extends GraphicsProgram {

    private double Height = getHeight();
    private double Width = getWidth();
    private static int col_num = 3;
    private static int row_num = 3;

    public void run() {
        GLine Border1 = new GLine(0, Height/3, Width, Height/3);
        GLine Border2 = new GLine(0, Height*2/3, Width, Height*2/3);
        GLine Border3 = new GLine(Width/3, 0, Width/3, Height);
        GLine Border4 = new GLine(Width*2/3, 0, Width*2/3, Height);
        add(Border1);
        add(Border2);
        add(Border3);
        add(Border4);
    }
}

I've also tried other Gobjects, but there was no success.

user113377
  • 29
  • 6

2 Answers2

0

From the documentation for run():

Specifies the code to be executed as the program runs. The run method is required for applications that have a thread of control that runs even in the absence of user actions, such as a program that uses console interation or that involves animation. GUI-based programs that operate by setting up an initial configuration and then wait for user events usually do not specify a run method and supply a new definition for init instead.

I read that to mean that it will only be called if the app. creates a Thread based on the GraphicsProgram (a Runnable) and explicitly calls Thread.start().

Change it to init() and it should work. The init method is explicitly called by the Java Virtual Machine that loads an applet.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

After hours of pointless code tweaking, i found out that by defining the height and width variables inside the run method, the board was showing up just fine.

user113377
  • 29
  • 6