Hey everyone. I've made a test file to remind me of how to make a graphic stand-alone application, and has some basic features too.
It displays the current coordinates of the mouse
It displays the keyCode and what the character is (if number or letter) of the last key event fired
It changes the colour between three values on the left, middle, and right clicks
The code is below:
Code:
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class GraphicsTest extends JFrame
implements MouseListener,MouseMotionListener,KeyListener
{
Color b=new Color(25,25,25),c=new Color(200,200,200);
final int Y_ZERO=31;
final int X_ZERO=5;
Color arch;
int keyyer=-1,mX=-1,mY=-1;
Graphics backstage;
Image buffer;
boolean keyOn=false;
public static void main(String[] args)
{
new GraphicsTest();
}
public GraphicsTest()
{
super("GraphicsTest");
addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent we){System.exit(0);}});
addMouseListener(this);addMouseMotionListener(this);addKeyListener(this);
setSize(500,100);
setVisible(true);
buffer=createImage(size().width,size().height);
backstage=buffer.getGraphics();
generateScreen();
}
public void generateScreen()
{
setBackground(b);
backstage.setColor(b);
backstage.fillRect(0,0,size().width,size().height);
backstage.setColor(c);
backstage.drawString("This shows how application graphics works",20+X_ZERO,20+Y_ZERO);
//-------------------------------------------------------------------------------------------------->>>>
if(mX!=-1&&mY!=-1)
backstage.drawString("Mouse co-ordinates: "+(mX-(X_ZERO-1))+","+(mY-(Y_ZERO-1)),20+X_ZERO,35+Y_ZERO);
if(keyyer!=-1)
{
if((keyyer>=48&&keyyer<=57)||(keyyer>=65&&keyyer<=90))
backstage.drawString("The code for the last key typed is "+keyyer+", which is the "+letOrNum(keyyer<60)+" \""+(char)keyyer+"\"",20+X_ZERO,50+Y_ZERO);
else
backstage.drawString("The code for the last key typed is "+keyyer+", which is a special key or character",20+X_ZERO,50+Y_ZERO);
}
repaint();
}
public void paint(Graphics g)
{
g.drawImage(buffer,0,0,this);
}
public String letOrNum(boolean gB){if(gB==true){return "number";}else{return "letter";}}
public void mouseReleased(MouseEvent m){}
public void mousePressed(MouseEvent m){}
public void mouseEntered(MouseEvent m){}
public void mouseExited(MouseEvent m){}
public void mouseClicked(MouseEvent m)
{
mX=m.getX();mY=m.getY();
if(m.getButton()==m.BUTTON1)
{
c=new Color(255,0,0);
}
if(m.getButton()==m.BUTTON2)
{
c=new Color(0,255,0);
}
if(m.getButton()==m.BUTTON3)
{
c=new Color(0,0,255);
}
generateScreen();
}
public void mouseMoved(MouseEvent m)
{
mX=m.getX();mY=m.getY();
generateScreen();
}
public void mouseDragged(MouseEvent m){}
public void keyTyped(KeyEvent k)
{
}
public void keyPressed(KeyEvent k)
{
}
public void keyReleased(KeyEvent k)
{
backstage.setColor(c);
keyyer=k.getKeyCode();
generateScreen();
}
}
I want to know if it's possible to detect key and mouse events fired OUTSIDE the application window.
Thanks for your help.
>I suck at this game, can you give me some pointers?
Sorry, I wasn't inferring that you're up to something you shouldn't be. Just that maybe it wouldn't work on most peoples' systems because if their firewall saw anything like that going on it'd put the kaibosh on it straight away.
Bookmarks