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.