adrianmcmenamin
adrianmcmenamin

Reputation: 1129

Getting my head round Java graphics

I am trying to do some very simple and crude graphics stuff with Java (deliberately crude and simple as it emulates a computing environment from 1980/1981).

The Java (Groovy in fact) code runs some very simple BASIC:

10 REM Testing Plotting
20 FOR X = 0 TO 31
30 LET Y = SIN(X/10)
40 PLOT (X, 11 - Y * 11)
50 NEXT X

Plot is provided in Java/Groovy like this:

grafix = textArea.getGraphics()

    ....
def plot(def x, def y)
{
    grafix.fillRect(x * 20 as Integer, y * 20 as Integer, 20, 20)
}

Now the code works after a fashion - I briefly see a sine curve flash on the screen but it disappears almost instantaneously (presumably on next repaint of the text area). How can I get the graphics to stay there - the only way I can think of is to save the plots (and unplots) into a list and then have that "play" every paint - but that seems quite an extreme solution.

Upvotes: 1

Views: 141

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168815

textArea.getGraphics()

Don't do that. The graphics instance is momentary. Instead paint the component when told to do so (by overriding paintComponent(Graphics)).

Upvotes: 3

Related Questions