0

I am back with a new question about the same code as last time. I took a lot of the suggestions that were given, and changed a few things. The program compiles, but no rectangle appears with any input. People in the last thread said that the code would work with the issues fixed, but this doesn't seem to work. I don't know if it a problem with the graphics (I think someone else suggested to do what I did in the code I have now) or with the way I am accepting key inputs.

Anyways, the program below is supposed to have a rectangle drawn, and for that rectangle be moved one coordinate to the right or left depending on if you press the <- or -> key on your keyboard. However, no rectangle appears with any input.

import java.awt.*;
import java.net.*;
import java.util.*;
import java.applet.Applet;


public class game extends Applet
{
    Thread loopThread;
    boolean left  = false;
    boolean right = false;
    int platPos = 50;


    public void run() 
    { 
        Graphics g = null;

        int i, j;
        long startTime;

        if (loopThread == null) 
        {
            loopThread = new Thread();
            loopThread.start();
        }

        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        startTime = System.currentTimeMillis();
        while(Thread.currentThread() == loopThread) 
        {
            updatePlatter(g);
        }

    }


     public void updatePlatter(Graphics g) 
    {

         if(left)
         {
             g.setColor(new Color(255,255,255)); //sets color to white
             g.fillRect(50+platPos, 200, 100, 20); //draws over whatever existing rectangle there is with white
             platPos--; 
             g.setColor(new Color(100,100,100)); //sets new color
             g.fillRect(50+platPos,200, 100,20); //draws new rectangle 
             left = false; 
         }

         if(right)
         {
             g.setColor(new Color(255,255,255));
             g.fillRect(50+platPos,200,100,20);
             platPos++;
             g.setColor(new Color(100,100,100));
             g.fillRect(50+platPos,200,100,20);
             right = false;
         }

    }

    public boolean keyDown(Event e, int key) 
    {
            if (key == Event.LEFT)
            left = true;

            if (key == Event.RIGHT)
            right = true;

            return true;
     }
}

Also I want to use applet, not JFrame. Just a personal preference. Thanks for any help! ^.^

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Socks
  • 5
  • 1
  • Sidenote: Although using lowercase names for classes might be another *preference* I would still recommend you to abide to the naming conventions. – Ben May 29 '18 at 12:40
  • Also you are assinging `Graphics g = null` and then call a bunch of methods on it. That's not going to work probably. – Ben May 29 '18 at 12:42
  • @Ben yeah, that is what I think the problem might be. I wanted to make sure that was the issue before I try to figure out how to fix it / properly initialize a Graphics (though the other questions that are asked about it seem to take a very different approach to it that I don't think I can make my program do) – Socks May 29 '18 at 12:45
  • you don't initialize graphics. The applet gives it to you – Veselin Davidov May 29 '18 at 12:48
  • But then I get an error that "variable g might not have been initialized" @VeselinDavidov. I didn't think that a Graphics needed to be initialized in the first place, but I am getting a compile error because it might not have been, – Socks May 29 '18 at 12:51

1 Answers1

0

First of all the graphics is provided to your applet. It's an abstract class and when you use the applet Java provides you with the proper implementation. You cannot just instance it. There are methods which take care of painting / repainting the applet. So I would suggest using the paint method for initial painting of the rectangle and then calling repaint in your loop. By the way using thread for a loop like that is not readable code (and it doesn't work). You don't even need run() method ;) When you create the applet it starts working.

So we can do it like that:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;


public class Game extends Applet
{
    boolean left  = false;
    boolean right = false;
    int platPos = 0;


    @Override
     public void paint(Graphics g) 
    {
         g.fillRect(50+platPos, 0, 100, 20); 
         System.out.println("repaint");
         if(left)
         {

             g.setColor(new Color(255,255,255)); //sets color to white
             g.fillRect(50+platPos, 0, 100, 20); //draws over whatever existing rectangle there is with white
             platPos--; 
             g.setColor(new Color(100,100,100)); //sets new color
             g.fillRect(50+platPos,0, 100,20); //draws new rectangle 
             left = false; 
         }

         if(right)
         {
             g.setColor(new Color(255,255,255));
             g.fillRect(50+platPos,200,100,20);
             platPos++;
             g.setColor(new Color(100,100,100));
             g.fillRect(50+platPos,200,100,20);
             right = false;
         }

    }
    @Override
    public boolean keyDown(Event e, int key) 
    {
             if (key == Event.LEFT) {
                left = true;
             }

            if (key == Event.RIGHT) {
                right = true;
            }

            repaint();
            return true;
     }
}
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • Also check https://docs.oracle.com/javase/tutorial/deployment/applet/lifeCycle.html and http://www.oracle.com/technetwork/java/painting-140037.html – Davide Cavestro May 29 '18 at 13:10