Sunday, 12 November 2017

Program to create a simple mini paint application

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:





  1. Press '2' to move DOWN
  2. Press '8' to move UP  
  3. Press '4' to move LEFT
  4. Press '6' to move RIGHT
  5. Press '7' to move TOP LEFT
  6. Press '9' to move TOP RIGHT
  7. Press '1' to move BOTTOM LEFT
  8. 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

5 comments:

  1. 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.

    Web designing Job oriented courses in jaipur

    ReplyDelete
  2. 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.

    Brand Promotion Company near me

    ReplyDelete
  3. 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.

    Web Designing company in jaipur

    ReplyDelete
  4. 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.

    Laravel industrial Training in jaipur

    ReplyDelete
  5. 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.

    SMO Agency near me

    ReplyDelete