-
KeyListener java applet
i cant use keyboard ....
i need use d to set down = down - 100;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class car1 extends JApplet implements ActionListener,KeyListener {
Timer timer ;int line;
JButton btnStart,btnStop,btnWelcome,btnup,btndown,btnfire, btnpep,btnupspeed,btndounseppd;
public int x = 50,w=500,down = 0,count =1,pep = 1,y= 1+x/9+count*pep-2,speed = 0;
public void init() {
setSize(1200,1200);
setLayout(new FlowLayout());
timer = new Timer (50,this);
btnStart = new JButton ("Start ");
btndown = new JButton("down");
btnStop = new JButton ("STOP");
btnpep = new JButton("pepole");
btnWelcome = new JButton ("welcome" );
btnfire = new JButton("fire");
btnup = new JButton("up");
btnupspeed = new JButton("up speed ");
btndounseppd=new JButton("Down speed "+" braks");
btnupspeed.addActionListener(this);
btndounseppd.addActionListener(this);
btnpep.addActionListener(this);
btnup.addActionListener(this);
btndown.addActionListener(this);
btnStart.addActionListener(this);
btnStop.addActionListener(this);
btnWelcome.addActionListener(this);
btnfire.addActionListener(this);
add(btnStart);
add(btnStop);
add(btnWelcome);
add(btnup);
add(btndown);
add(btnfire);
add(btnpep);
add(btnupspeed);
add(btndounseppd);
addKeyListener(this);
}
public car1()
{
}
// handle press of any key
public void keyPressed( KeyEvent event )
{
char c = event.getKeyChar();
if(c=='d' )
down = down -100;
} // end method keyPressed
// handle release of any key
public void keyReleased( KeyEvent event )
{
} // end method keyReleased
// handle press of an action key
public void keyTyped( KeyEvent event )
{
}
// end method keyTyped
/////////////////////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnWelcome)
JOptionPane.showMessageDialog(null,"Anees Hikmat : give your car ","CAR on street :welcome",JOptionPane.INFORMATION_MESSAGE);
if(e.getSource()==btnStop)
{
timer.stop();
}
if(e.getSource()==btndown)
{ if(down == 0)
{ down = down + 100;
}
else
JOptionPane.showMessageDialog(null,"Error : you cant left ","warning",JOptionPane.ERROR_MESSAGE);
}
if(e.getSource()==btnup)
{
if(down==100)
{down = down -100;
}
}
if(e.getSource()==btnupspeed)
{ if(speed <5)
speed++;
}
if(e.getSource()==btndounseppd)
{
if(speed>0)
speed--;
}
if(e.getSource()==btnfire)
{
count = count +10;
}
if(e.getSource()==btnpep)
{
pep = pep+40;
}
else
if(e.getSource()==btnStart)
{ timer.start();
if(speed==0)
speed++;
}
else
if(e.getSource()==timer)
{ repaint();
x = x+8+speed*speed;
y ++;w=w+2;
if(x>=300)
x=x+10;
if(x>=900)
{ x=50;count=1;}
if(y==75)
y=1;
if(w>=900)
w=500;
if(x>=640&&x<=750&&y<1)
{ speed=0;
timer.stop();
y=35;
}
if(e.getSource()==btnStart)
{
y++;
timer.start();
}
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(0,100,900,100);
g.drawLine(0,350,900,350);
g.drawString("___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___",0,225);
g.setColor(Color.ORANGE);
g.fillRect(0,0,150,100);
g.setColor(Color.CYAN);
g.fillRect(25,25,25,25);
g.fillRect(100,25,25,25);
g.fillRect(62,50,25,50);
g.setColor(Color.PINK);
g.fillRect(150,0,150,100);
g.setColor(Color.GREEN);
g.fillRect(175,25,25,25);
g.fillRect(250,25,25,25);
g.fillRect(210,50,25,50);
g.setColor(Color.BLUE);
g.drawLine(x,130+down,220+x,130+down);
g.setColor(Color.RED);
g.drawRect(x,130+down,140,50);
g.setColor(Color.darkGray);
g.fillOval(x+10,180+down,25,25);
g.setColor(Color.WHITE);
g.fillOval(x+15,187+down,12,12);
g.setColor(Color.darkGray);
g.fillOval(x+100,180+down,25,25);
g.setColor(Color.WHITE);
g.fillOval(x+105,187+down,12,12);
g.setColor(Color.BLACK);
g.drawLine(x+70,130+down,x+70,155+down);
g.drawLine(x,155+down,x+140,155+down);
g.fillRect(800,25,10,75);
g.drawRect(800,25,30,30);
if(y<=35)
{
g.setColor(Color.red);
g.fillOval(812,25,15,15);
}
if(y>35&&y<=70)
{
g.setColor(Color.GREEN);
g.fillOval(812,40,15,15);
}
g.setColor(Color.BLACK);
g.fillRect(300,25,10,75);
g.setColor(Color.YELLOW);
g.fillOval(310,25,30,30);
g.setColor(Color.BLUE);
g.drawString("90",318,45);
g.setColor(Color.GRAY);
g.fillRect(0,350,150,100);
g.setColor(Color.YELLOW);
g.fillRect(25,375,25,25);
g.fillRect(100,375,25,25);
g.fillRect(62,400,25,50);
g.setColor(Color.BLUE);
g.fillRect(150,350,150,100);
g.setColor(Color.RED);
g.fillRect(175,375,25,25);
g.fillRect(250,375,25,25);
g.fillRect(210,400,25,50);
g.setColor(Color.blue);
g.fillRect(400,352,600,350);
g.setColor(Color.BLACK);
g.fillRect(w-70+25,600,150,20);
g.setColor(Color.WHITE);
g.drawLine(w-50+25,600,w+25,550);
g.setColor(Color.WHITE);
g.drawLine(w+25,600,w+25,550);
g.setColor(Color.WHITE);
g.drawLine(w+50+25,600,w+25,550);
g.setColor(Color.ORANGE);
if(count>5)
{
for(int i=1;i<=10;i++)
{
g.fillOval(x+220+count*2*i,130+down,30,10);
}
count --;
}
if(pep>1&&x+220+count*2*1<=500)
{
g.setColor(Color.MAGENTA);
g.fillOval(500,130,20,20);
g.drawLine(510,130,510,200);
g.drawLine(510,170,530,170);
g.drawLine(500,170,530,170);
g.drawLine(500,200,520,200);
pep--;
}
if(pep>=0)
pep--;
g.setColor(Color.BLACK);
g.fillOval(955,53,10,10);
for(int j=0;j<=speed;j++)
{
g.drawLine(960,60,1000,120-speed*20);
}
for(int i=0;i<=5;i++)
{ g.setColor(Color.BLUE);
g.drawString(i*20+" Km/sec",1000,120-i*20);
}
}
}
-
First of all use the "Code" tags when posting code.
You should NOT be overriding the paint() method. Custom painting is done by overriding the paintComponent() method of JPanel (or JComponent). Then you add the panel to the applet.
KeyEvents only work when the component has focus. So you will then need to use
Code:
panel.setFocusable( true );
-
thx