Java Create a Drawing tool using Java Frams Jframs 1 The win

Java:

Create a Drawing tool using Java Frams (Jframs)

1. The window will have three choices (buttons) at the top which allow:

a) Three levels of line width

b) Three colors of lines

c) Erase mode

2. Create a mouse driven drwing tool at the bottom of window

Solution

Solution:

// import required packages
import java.awt.*;
import java.awt.event.*;

// class to draw line withdifferent size, color with a erase option
class mousedrwingtool extends Frame implements ActionListener, ItemListener, MouseListener, MouseMotionListener, WindowListener
{
   static final int win_Wid = 800;
   static final int win_Hei = 600;
  
   int win_upperLeftX, win_upperLeftY;
   int win_width, win_height;
   int win_x1,win_y1,win_x2,win_y2;

  
   boolean win_fill = false;
   boolean win_erasure = false;
      
   String win_drawColor = new String(\"black\");
   String win_drawShape = new String(\"line\");
  
   TextField win_color = new TextField();
   TextField win_shape = new TextField();
   TextField win_position = new TextField();

   CheckboxGroup fillOutline = new CheckboxGroup();
  
   String[] win_colorNames =
   {\"black\",\"blue\",\"cyan\",\"gray\",\"green\",\"magenta\",\"red\",\"white\",\"yellow\"};
   String[] win_shapeNames = {\"line\"};
   String[] win_toolNames = {\"erase\",\"clearpad\"};
  
   // methods
   private void initializeTextFields()
   {
       win_color.setLocation(5,525);
       win_color.setSize(70,20);
       win_color.setBackground(Color.white);
       win_color.setText(win_drawColor);
       add(win_color);      
  
       win_shape.setLocation(5,550);
       win_shape.setSize(70,20);
       win_shape.setBackground(Color.white);
       win_shape.setText(win_drawShape);
       add(win_shape);

       win_position.setLocation(5,575);
       win_position.setSize(70,20);
       win_position.setBackground(Color.white);
       add(win_position);
      
   }

   private void initializeMenuComponents()
   {
       MenuBar win_mbar = new MenuBar();

       Menu win_mcolors = new Menu(\"COLORS\");
       for (int widx=0; widx != win_colorNames.length; widx++)
           win_mcolors.add(win_colorNames[widx]);
       win_mbar.add(win_mcolors);
       win_mcolors.addActionListener(this);

       Menu win_mshapes = new Menu(\"SHAPES\");
       for (int widx=0; widx != win_shapeNames.length; widx++)
           win_mshapes.add(win_shapeNames[widx]);
       win_mbar.add(win_mshapes);
       win_mshapes.addActionListener(this);      

       Menu win_mtools = new Menu(\"TOOLS\");
       for (int widx=0; widx != win_toolNames.length; widx++)
           win_mtools.add(win_toolNames[widx]);
       win_mbar.add(win_mtools);
       win_mtools.addActionListener(this);
        setMenuBar(win_mbar);
   }

   private void initializeRadioButtons()
   {
       Checkbox win_fill = new Checkbox(\"fill\", fillOutline, false);
       Checkbox win_outline = new Checkbox(\"outline\",fillOutline,true);

       win_fill.setLocation(5,500);
       win_fill.setSize(70,20);
       add(win_fill);
       win_fill.addItemListener(this);

       win_outline.setLocation(5,475);
       win_outline.setSize(70,20);
       add(win_outline);
       win_outline.addItemListener(this);
   }  
  
   // constructor
   public mousedrwing tool(String cnstr)
   {
       super(cnstr);
       setBackground(Color.yellow);  
                   setLayout(null);

       initializeTextFields();
       initializeMenuComponents();
       initializeRadioButtons();      
      
       addMouseMotionListener(this);
       addMouseListener(this);
       addWindowListener(this);
   }

   //-----------------------------------------------------------------

   public void actionPerformed(ActionEvent event)
   {
       Graphics wngrp = getGraphics();
       Object objsrc = event.getActionCommand();

       for (int widx=0; widx != win_colorNames.length; widx++)
           if (objsrc.equals(win_colorNames[widx]))
           {
               win_drawColor =win_colorNames[widx];
               win_color.setText(win_drawColor);
               return;
           }

       for (int widx=0; widx != win_shapeNames.length; widx++)
           if (objsrc.equals(win_shapeNames[widx]))
           {
               win_drawShape = win_shapeNames[widx];
               win_shape.setText(win_drawShape);
               return;
           }

       if (objsrc.equals(\"erase\"))
       {
           win_erasure = true;
           return;
       }
       else if (objsrc.equals(\"clearpad\"))
       {          
           wngrp.clearRect(0,0,800,600);
           return;
       }
   }

   //-----------------------------------------------------------------

   public void itemStateChanged(ItemEvent event)
   {
       if (event.getItem() == \"fill\")
           win_fill = true;
       else if (event.getItem() == \"outline\")
           win_fill = false;
   }


   //-----------------------------------------------------------------

   protected void selectColor(Graphics wngrp)
   {
       for (int widx=0; widx != win_colorNames.length; widx++)
       {
           if (win_drawColor.equals(win_colorNames[widx]))
           {
               switch (widx)
               {
                   case 0: wngrp.setColor(Color.black);break;
                   case 1: wngrp.setColor(Color.blue);break;
                   case 2: wngrp.setColor(Color.cyan);break;
                   case 3: wngrp.setColor(Color.gray);break;
                   case 4: wngrp.setColor(Color.green);break;
                   case 5: wngrp.setColor(Color.magenta);break;
                   case 6: wngrp.setColor(Color.red);break;
                   case 7: wngrp.setColor(Color.white);break;
                   case 8: wngrp.setColor(Color.yellow);
               }
           }
       }  
   }

   //------------------------------------------------------------------

   protected void displayMouseCoordinates(int X, int Y)
   {
       win_position.setText(\"[\"+String.valueOf(X)+\",\"+String.valueOf(Y)+\"]\");
   }

   public void mouseDragged(MouseEvent event)
   {
       Graphics wngrp = getGraphics();
      
       win_x2=event.getX();
       win_y2=event.getY();      
      
       if (win_erasure)
       {
           wngrp.setColor(Color.yellow);
           wngrp.fillRect(win_x2,win_y2,5,5);
       }

       displayMouseCoordinates(event.getX(), event.getY());      
   }

   //----------------------------------------------------------------

   public void mouseMoved(MouseEvent event)
   {
       displayMouseCoordinates(event.getX(), event.getY());
   }

   //----------------------------------------------------------------
   public void mouseClicked(MouseEvent event){}
   public void mouseEntered(MouseEvent event){}
   public void mouseExited(MouseEvent event){}  

   //----------------------------------------------------------------
   public void mousePressed(MouseEvent event)
   {
       if (win_erasure) return;

       win_upperLeftX=0; win_upperLeftY=0; win_width=0; win_height=0;
      
       win_x1=event.getX();
       win_y1=event.getY();

       Graphics wngrp = getGraphics();

       wngrp.drawString(\".\",win_x1,win_y1);
       displayMouseCoordinates(event.getX(), event.getY());
   }

   public void mouseReleased(MouseEvent event)
   {
       Graphics wngrp = getGraphics();  
       displayMouseCoordinates(event.getX(), event.getY());
       if (win_erasure)
       {
           win_erasure = false;
           return;
       }
       selectColor(wngrp);
       win_x2=event.getX();
       win_y2=event.getY();      
      
       if (win_drawShape.equals(\"line\"))
       {
           wngrp.drawLine(win_x1,win_y1,win_x2,win_y2);
       }      

       wngrp.setColor(Color.yellow);
       wngrp.drawString(\".\",win_x1,win_y1);
       wngrp.setColor(Color.black);
   }

   //------------------------------------------------------------------  
   public void windowClosed(WindowEvent event){}
   public void windowDeiconified(WindowEvent event){}
   public void windowIconified(WindowEvent event){}
   public void windowActivated(WindowEvent event){}
   public void windowDeactivated(WindowEvent event){}
   public void windowOpened(WindowEvent event){}

   //------------------------------------------------------------------

   public void windowClosing(WindowEvent event)
   {
       System.exit(0);      
   }
}


public class drwingtool
{
   public static void main(String[] args)
   {
       mousedrwing tool winscreen = new mousedrwing tool(\"mousedrwing tool\");
       winscreen.setSize(mousedrwing tool.win_Wid, mousedrwing tool.win_Hei);
       winscreen.setVisible(true);
   }
}

Java: Create a Drawing tool using Java Frams (Jframs) 1. The window will have three choices (buttons) at the top which allow: a) Three levels of line width b) T
Java: Create a Drawing tool using Java Frams (Jframs) 1. The window will have three choices (buttons) at the top which allow: a) Three levels of line width b) T
Java: Create a Drawing tool using Java Frams (Jframs) 1. The window will have three choices (buttons) at the top which allow: a) Three levels of line width b) T
Java: Create a Drawing tool using Java Frams (Jframs) 1. The window will have three choices (buttons) at the top which allow: a) Three levels of line width b) T
Java: Create a Drawing tool using Java Frams (Jframs) 1. The window will have three choices (buttons) at the top which allow: a) Three levels of line width b) T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site