Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › I don't get this >_< (java)
New Posts  All Forums:Forum Nav:

I don't get this >_< (java)

post #1 of 4
Thread Starter 
I've been trying to setup a basic keylistener but to no avial.
Here is the code:

import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.*;
public class Arc extends JApplet implements KeyListener{
static boolean ard;
Ex vex;

public void init()
{
this.setSize(500,500);
this.addKeyListener(this);
vex = new Ex();
this.add(vex);

Thread b = new A(this);
b.start();
}

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == e.VK_K)
{
ard = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == e.VK_K)
{
ard = false;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

}
class Ex extends JComponent
{
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;

if (Arc.ard == true)
{
g2.setColor(Color.blue);
g2.draw3DRect(200, 300, 90, 60, false);
}
}
}

class A extends Thread
{
JApplet c;
public A(JApplet c)
{
this.c = c;
}
public void run()
{
while(true)
{
c.repaint();
}
}
}

What I wanted to happen was for it to paint a 3d rect when I press k...but it doesn't work. What exactly am I doing wronge thanks in advance
post #2 of 4
hey lemme forward this to my buddy... he prob knows more than I do. Your answer will likely come from blade19.
Maelstrom
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 Gigabyte DS3LR eVGA 9800GTX+ 4x2gb G.Skill Ripjaws 
Hard DriveOSMonitorKeyboard
Seagate 500gb perp Windows 7 x64, Ubuntu 9.10 beta 23" Samsung, 22" Acer AL2223, 19" Hanns-G Logitech G15 
PowerCaseMouse
Ultra X-Finity 600W NZXT Alpha Logitech G7 
  hide details  
Reply
Maelstrom
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 Gigabyte DS3LR eVGA 9800GTX+ 4x2gb G.Skill Ripjaws 
Hard DriveOSMonitorKeyboard
Seagate 500gb perp Windows 7 x64, Ubuntu 9.10 beta 23" Samsung, 22" Acer AL2223, 19" Hanns-G Logitech G15 
PowerCaseMouse
Ultra X-Finity 600W NZXT Alpha Logitech G7 
  hide details  
Reply
post #3 of 4
Code:
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.*;
public class Arc extends JApplet implements KeyListener{
//static boolean ard;
Ex vex;

public void init()
{
this.setSize(500,500);
this.addKeyListener(this);
vex = new Ex();
this.add(vex);

//Thread b = new A(this);
//b.start();
}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_K)
{
vex.ard = true;
}
}

public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == e.VK_K)
{
vex.ard = false;
}
}
public void keyTyped(KeyEvent e) {

}

}
class Ex extends JComponent
{
public static boolean ard = false;

public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;

if (ard == true)
{
g2.setColor(Color.blue);
g2.draw3DRect(200, 300, 90, 60, false);
}
}
}

/*class A extends Thread
{
JApplet c;
public A(JApplet c)
{
this.c = c;
}
public void run()
{
while(true)
{
c.repaint();
}
}
}*/
try that on for size; i haven;t played with applets for a while, so couldnt get it to actually run so i could test it - let me know what's up, bc apparently i'm good at java (@ted) :]
citrinitas
(14 items)
 
  
CPUMotherboardGraphicsRAM
QX9650 DFI LanParty DK P35-T2RS eVGA GTX260 SSC 4 x 1GB DDR2 Ballistix 1066MHz Dual Channel -D9MGH 
Hard DriveHard DriveOptical DriveOS
3 x 7200.11 500GB RAID 5 32GB Corsair V32 SSD (OS) none, and proud of it. ODD = obsolete tech! Win 7 
MonitorPowerCaseMouse
Acer X241W 24" WS TT Toughpower 700W Rocketfish + mods Logitech G9 
  hide details  
Reply
citrinitas
(14 items)
 
  
CPUMotherboardGraphicsRAM
QX9650 DFI LanParty DK P35-T2RS eVGA GTX260 SSC 4 x 1GB DDR2 Ballistix 1066MHz Dual Channel -D9MGH 
Hard DriveHard DriveOptical DriveOS
3 x 7200.11 500GB RAID 5 32GB Corsair V32 SSD (OS) none, and proud of it. ODD = obsolete tech! Win 7 
MonitorPowerCaseMouse
Acer X241W 24" WS TT Toughpower 700W Rocketfish + mods Logitech G9 
  hide details  
Reply
post #4 of 4
Well the thing that jumped out to me was your key method comparisons:

if(e.getKeyCode() == e.VK_K)

I always assumed that constants are static fields, rather than instance fields. I would do:

if(e.getKeyCode() == KeyEvent.VK_K)

and so on for the rest of the comparisons. Also, debugging statements help a ton. Like putting:

System.out.println("it recognized my key press");

In order to ensure you're actually recognizing the keys. THEN move on to the fancy behaviour. Give that a shot and post back.

Edit: you should also note that depending on the return type of getKeyCode(), you may have to use

if(e.getKeyCode().equals(KeyEvent.VK_K))

I don't recall off the top of my head.
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › I don't get this >_< (java)