1

So for my AP Comp. science class I have to make an applet where we have to use separate class files to make a building skyline-line thing. At the moment I have a window class that has a draw method and a method to turn the window object on. I would like to have the window turn on with a keyListener, but I'm semi-new to those.

this is the Window class I have

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JComponent;
public class windows {

    private int x,y,height,width;

    public windows(){
        x=0;
        y=0;
        height = 0;
        width = 0;

    }//default constructor

    public windows(int x, int y, int height, int width){
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
    }//windows constructor

    public void windowDraw(Graphics g){
        Color window = new Color(0x05030D);
        g.setColor(window);
        g.fillRect(x, y, width, height);
    }//windowDraw

    public void windowOn(Graphics g){
        Color on = new Color(0xFFD200);
        g.setColor(on);
        g.fillRect(x, y, width, height);
    }//window on


}//class

Here is the client. I have some test prints inside that I used to play around with.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//Matteo Sabato
public class TeenTitans_Client extends Applet implements KeyListener {
    private final int APPLET_WIDTH = 1000;
    private final int APPLET_HEIGHT = 700;
    private final int HEIGHT_MIN = 400;
    private final int VARIANCE = 40;
    Tower main,top;//(x, y, height, width)
    backGround ground;//x,y,height,width
    windows Right, middleTop, Left, Bottom, Middle, Tip;
    boolean power = false;
    Listener on;
    int key;

    public void init(){
        addKeyListener(this);

        Random gen = new Random();

        Color back = new Color(0x37E2EA);

        setBackground(back);
        setSize(APPLET_WIDTH,APPLET_HEIGHT);

        main = new Tower(550,180,460,150);
        top = new Tower(370,40,150,505);
        ground = new backGround(-200,500,400,1800);
        Right  = new windows(380,50,130,130);
        Left  = new windows(735,50,130,130);
        middleTop = new windows(520,50,130,205);
        Bottom = new windows(560,490,140,130);
        Middle = new windows(560,340,140,130);
        Tip = new windows(560,190,140,130);


    }

    public void paint(Graphics g){
    ground.circleDraw(g);
    main.towerDraw(g);
    top.towerDraw(g);
    Left.windowDraw(g);
    Right.windowDraw(g);
    Bottom.windowDraw(g);
    middleTop.windowDraw(g);
    Middle.windowDraw(g);
    Tip.windowDraw(g);

    if (power == true)
        On(g);





    }//paint





    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub.
        System.out.println(e.getKeyCode());
        this.key = e.getKeyCode();
        if (key==10){

            power = true;

        }

    }//keyPressed

    public void On(Graphics g){
        Tip.windowOn(g);        
        }//On




    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}//class

The problem I'm running into is that I don't know how to call my On method in the client because I need to have Graphics g as a parameter, which key event methods can't take. If I make a loop somewhere else, I won't be able to check for key presses. I tried laying it out a bunch of different ways, but I just can't figure it out.

  • Follow the naming conventions: Classes in `UpperCamelCase`, fields/variables in `lowerCamelCase`, and don't use `_` underscores. Regarding the question: The pattern here is always the same. You have to paint everything from the `paint` method, passing on its `g` parameter as necessary. If you want to switch things "on" or "off", you have to use a `boolean` flag or so. And from skimming the code, it looks like **you already did this!**. Maybe the problem is already solved when you add a call to `repaint();` after setting `power = true;` ? – Marco13 Dec 15 '15 at 18:37
  • I tried but it seems like it just sets the boolean to true but doesn't update the paint method so it doesn't get to `On(g);` – Matteo Sabato Dec 18 '15 at 18:32
  • After copying the code together, commenting out the missing `Tower` etc. classes, and adding a `repaint();` after `power = true;`, I could start the applet, press "return", and one rectangle turned yellow. Calling `repaint()` should cause a call to `paint` as soon as possible. In doubt, add `System.out.println("power is "+power);` in your `paint` and `keyPressed` methods: What does it print? – Marco13 Dec 18 '15 at 18:43

0 Answers0