In this program, we will create a simple mini paint application using Frame class. We have to implement two types of events here: Mouse event and Key event. To use this program you have to use numpad keys on keyboard. The following keys are used for specific purpose:
- Press '2' to move DOWN
- Press '8' to move UP
- Press '4' to move LEFT
- Press '6' to move RIGHT
- Press '7' to move TOP LEFT
- Press '9' to move TOP RIGHT
- Press '1' to move BOTTOM LEFT
- Press '3' to move BOTTOM RIGHT
PROGRAM
import java.awt.*; import java.awt.event.*; import java.applet.*; class MiniPaintApp extends Frame implements KeyListener { String msg=""; int x,y; MiniPaintApp() { setBounds(0,0,550,550); setTitle("Mini Paint Application"); addKeyListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent me) { x = me.getX(); y = me.getY(); } }); } public void keyPressed(KeyEvent ke) { int keyCode = ke.getKeyCode(); System.out.println("KeyTyped = "+ke.getKeyChar()+" keyCode = "+keyCode); System.out.println(KeyEvent.VK_NUMPAD8); switch( keyCode ) { case KeyEvent.VK_NUMPAD8: // move up if(y > 51) y-=1; break; case KeyEvent.VK_NUMPAD2: // move down if(y < 419) y+=1; break; case KeyEvent.VK_NUMPAD4: // move left if(x > 41) x-=1; break; case KeyEvent.VK_NUMPAD6 : // move right if(x < 389) x+=1; break; case KeyEvent.VK_NUMPAD9 : // move upper right if(x < 389 && y > 41) { x+=1; y-=1; } break; case KeyEvent.VK_NUMPAD7 : // move upper left if(x > 41 && y > 41) { x-=1; y-=1; } break; case KeyEvent.VK_NUMPAD1 : // move lower left if(x > 41 && y < 419) { x-=1; y+=1; } break; case KeyEvent.VK_NUMPAD3 : // move lower right if(x < 389 && y < 419) { x+=1; y+=1; } break; } paint(getGraphics()); } public void keyTyped(KeyEvent ke) { } public void keyReleased(KeyEvent ke) { } public void paint(Graphics g) { g.fillRect(x,y,2,2); g.setFont(new Font("Cambria",Font.BOLD,17)); g.drawString("First click to set starting point !!!",40,466); g.drawString("Press 8 to move UP",40,480); g.drawString("Press 2 to move DOWN",40,495); g.drawString("Press 4 to move LEFT",40,510); g.drawString("Press 6 to move RIGHT",40,525); g.drawString("Press 9 to move TOP RIGHT",240,480); g.drawString("Press 7 to move TOP LEFT",240,495); g.drawString("Press 1 to move BOTTOM LEFT",240,510); g.drawString("Press 3 to move BOTTOM RIGHT",240,525); g.setColor(Color.red); g.drawRect(40,50,400,400); } public static void main(String args[]) { new MiniPaintApp().show(); } }OUTPUT
C:\>javac MiniPaintApp.java C:\>java MiniPaintApp
Nice Article Thanks for sharing this. It has very important content for beginners. Please keep sharing like these posts with us; I am very glad to read your post.
ReplyDeleteWeb designing Job oriented courses in jaipur
Thanks! Really effective to boost up ranking in search engine. I am very glad to read your post; it is very helpful for new user. Please keep posting.
ReplyDeleteBrand Promotion Company near me
Thanks! Really effective to boost up ranking in search engine. I am very glad to read your post; it is very helpful for new user. Please keep posting.
ReplyDeleteWeb Designing company in jaipur
Excellent job, Positive thoughts, I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Please keep posting and sharing.
ReplyDeleteLaravel industrial Training in jaipur
I have read your article from start to end and it`s really this goes far beyond the commenting. The thoughts while reading the article is amazing. Thanks for sharing this blog with us, please keep posting.
ReplyDeleteSMO Agency near me
Java java
ReplyDelete