Wednesday, 12 February 2014

Paint Utility Application in Java

Wow It was fantastic experience to develop a paint Utility Application in Java:

Following is the program:

package com.mvc;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class Exercise29_3 extends JApplet{

    private ImageIcon lineImageIcon = new ImageIcon("C:\\Users\\TARA BAHADUR THAPA\\Desktop\\line.gif");
    private ImageIcon rectangleImageIcon = new ImageIcon("C:\\Users\\TARA BAHADUR THAPA\\Desktop\\rectangle.gif");
    private ImageIcon ovalImageIcon = new ImageIcon("C:\\Users\\TARA BAHADUR THAPA\\Desktop\\oval.gif");
    private JToolBar jToolBar = new JToolBar();
    private JButton jbtLine = new JButton(lineImageIcon);
    private JButton jbtRectangle = new JButton(rectangleImageIcon);
    private JButton jbtOval = new JButton(ovalImageIcon);
    private DrawPanel drawPanel = new DrawPanel();

    public Exercise29_3(){
        jToolBar.setOrientation(JToolBar.HORIZONTAL);
        jToolBar.setFloatable(true);
        jToolBar.add(jbtLine);
        jToolBar.add(jbtRectangle);
        jToolBar.add(jbtOval);
        add(jToolBar,BorderLayout.NORTH);
        add(drawPanel,BorderLayout.CENTER);
       
        jbtLine.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                drawPanel.setButtonPressed("LINE BUTTON");
            }
        });
       
       
        jbtRectangle.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent e) {
                drawPanel.setButtonPressed("RECTANGLE BUTTON");
            }
        });
       
        jbtOval.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent e) {
                drawPanel.setButtonPressed("OVAL BUTTON");
            }
        });
    }
}

class DrawPanel extends JPanel{
    private int xCoordinate = 0;
    private int yCoordinate = 0;
    private int x = 0;
    private int y = 0;
    private String buttonPressed = "";
   
    public DrawPanel(){
        addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e){
                xCoordinate = e.getX();
                yCoordinate = e.getY();
            }

        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e){
                System.out.println("Mouse Dragged");
                x = e.getX();
                y = e.getY();
                repaint();
            }
        });
    }
   
    public String getButtonPressed() {
        return buttonPressed;
    }
   
    public void setButtonPressed(String buttonPressed) {
        this.buttonPressed = buttonPressed;
    }
   
   
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        if(!(buttonPressed.equals(""))){
            if(buttonPressed.equals("LINE BUTTON")){
                g.drawLine(xCoordinate, yCoordinate, x, y);
            }
            else if(buttonPressed.equals("RECTANGLE BUTTON")){
                g.drawRect(xCoordinate, yCoordinate, x - xCoordinate, y - yCoordinate);
            }
            else if(buttonPressed.equals("OVAL BUTTON")){
                g.drawOval(xCoordinate, yCoordinate, x - xCoordinate, y - yCoordinate);
            }
        }
    }
}

Every one is welcome to suggest me improvements in this program....

No comments:

Post a Comment