Question

   import javax.swing.*; import java.awt.*; import java.awt.event.*; //Class that paints according to the user wish. public...

  

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Class that paints according to the user wish.
public class RapidPrototyping extends JFrame implements MouseListener,ItemListener,ActionListener,MouseMotionListener {
  
   //panel to hold color,shapes and thickness components
   JPanel panel;
   //shapes combobox
   JComboBox shapes;
   //color radio buttons
   JRadioButton red, green, blue;
   //thickness combobox
   JComboBox thicknesses;
   //clear button.
   JButton clear;
   JPanel center;
  
   /*values of each selection*/
   Color color;
   int thickness;
   String shape;

   //start and end positions of mouse start and end positions of drag.
   int startX, startY;
   int endX, endY;
   boolean startSet;

   final String shapesList[] = { "RECTANGLE", "OVAL", "LINE" };

   final String thicknessesList[] = { "3", "6", "9", "12", "15" };

   RapidPrototyping() {
       //initialize values.
       setSize(600, 400);
       setLayout(new BorderLayout());

       panel = new JPanel(new FlowLayout());

       JLabel shapeLabel = new JLabel("Shape :");
       shapes = new JComboBox(shapesList);

       JLabel colorLabel = new JLabel("Color :");
       red = new JRadioButton("Red");
       green = new JRadioButton("Green");
       blue = new JRadioButton("Blue");
       ButtonGroup g = new ButtonGroup();
       g.add(red);
       g.add(blue);
       g.add(green);

       JLabel thicknessLabel = new JLabel("thickness :");
       thicknesses = new JComboBox(thicknessesList);

       clear = new JButton("Clear");
      
       center = new JPanel();
      
       //add elements to panel
       panel.add(shapeLabel);
       panel.add(shapes);
  
  
  
       panel.add(colorLabel);
       panel.add(red);
       panel.add(green);
       panel.add(blue);

       panel.add(thicknessLabel);
       panel.add(thicknesses);

       panel.add(clear);
       add(panel, BorderLayout.NORTH);
       add(center,BorderLayout.CENTER);
      
       /*Listeners*/
       addMouseListener(this);
       //adding mouse motionlistener is good but there is a drawback in that.
       //code will draw a shape without dragging.
       //it takes the previous point location and the curred dragged position.
       red.addItemListener(this);
       blue.addItemListener(this);
       green.addItemListener(this);
      
       shapes.addActionListener(this);
      
       clear.addActionListener(this);
      
       thicknesses.addActionListener(this);

       /* Default Values Setting */
       color = Color.RED;
       red.setSelected(true);
      
       thickness = 3;
       shape = "RECTANGLE";
       startSet = false;
      
      
   }
   //action listener
   public void actionPerformed(ActionEvent ae)
   {
       //set shape value on shapes selection changed.
       if(ae.getSource() == shapes)
       {
           shape = ""+shapes.getItemAt(shapes.getSelectedIndex());
       }
       //repaint the frame if clear button clicked.
       else if(ae.getSource() == clear)
       {
           repaint();
       }
       //change the thickness value.
       else if(ae.getSource() == thicknesses)
       {
           try
           {
               thickness = Integer.parseInt(""+thicknesses.getItemAt(thicknesses.getSelectedIndex()));
           }
           catch(Exception e)
           {
               thickness = 3;
           }
       }
   }
   //set color value according to the selection in color selectin of radio buttons.
   public void itemStateChanged(ItemEvent ie)
   {
       if(ie.getSource() == red)
       {
           color = Color.RED;
       }
       else if(ie.getSource() == blue)
       {
           color = Color.BLUE;
       }
       else if(ie.getSource() == green)
       {
           color = Color.GREEN;
       }
   }
   //mouse pressed is the first step in clicking mouse.
   //when a button is presesd this will invoke
   public void mousePressed(MouseEvent e)
   {
       //if the start point is not set
       if(startSet == false)
       {
           //then set start point as current mouse location
           startX = e.getX();
           startY = e.getY();
           startSet = true;
          
       }
   }

   // this function is invoked when the mouse is released
   public void mouseReleased(MouseEvent e)
   {
       //when mouse click is released .set end point.
       if(startSet)
       {
           endX = e.getX();
           endY = e.getY();
           startSet = false;
           //call drawShape() to draw current shape.
           drawShape();
          
       }
   }
   //when mouse is dragged in right to left or bottom to up
   //we have to change the start and end points for drawing rectangel and oval
   //because they take start position and width,height(not end position like line).
   public void checkAndSwap()
   {
       //swap value of x and y
       if(startX > endX)
       {
           int temp = startX;
           startX = endX;
           endX = temp;
       }
       if(startY > endY)
       {
           int temp = startY;
           startY = endY;
           endY = temp;
       }
   }
   //method drawShape() that draws current selected properties of paint
   //and draws them .
   public void drawShape()
   {
      
       //Graphics2D will be more useful than Graphics class.
       Graphics2D g2 = (Graphics2D) getGraphics();
       g2.setStroke(new BasicStroke(thickness));
       g2.setColor(color);
       //cal width and height
       int width = Math.abs(endX-startX);
       int height = Math.abs(endY-startY);
       System.out.println("\nThickness: "+thickness);
      
       //if shape is rectangle draw shape
       if(shape.equals(("RECTANGLE")))
       {
           //check and swap call only for rectangle and oval
           checkAndSwap();
           System.out.printf("Drawing %s : at -> %d %d with width and height %d,%d end point: %d %d\n",shape,startX,startY,width,height,endX,endY);
           //fillRect with following fields.
           g2.fillRect(startX,startY,width,height);
          
       }
       else if(shape.equals("OVAL"))
       {
           checkAndSwap();
           System.out.printf("Drawing %s : at -> %d %d with width and height %d,%d end point: %d %d\n",shape,startX,startY,width,height,endX,endY);
           g2.fillOval(startX,startY,width,height);
       }
       else
       {
           System.out.printf("Drawing %s : at -> %d %d end point: %d %d\n",shape,startX,startY,width,height,endX,endY);
           g2.drawLine(startX, startY, endX, endY);
       }
              
   }
   // this function is invoked when the mouse exits the component
   public void mouseExited(MouseEvent e)
   {

   }

   // this function is invoked when the mouse enters the component
   public void mouseEntered(MouseEvent e)
   {

   }

   // this function is invoked when the mouse is pressed or released
   public void mouseClicked(MouseEvent e)
   {
      
         
   }
   //MouseMotionListener methods are not used.
   public void mouseDragged(MouseEvent e)
   {
   }
   public void mouseMoved(MouseEvent e)
   {
   }
   //create jframe object and set its properties.
   public static void main(String[] args) {
       RapidPrototyping frame = new RapidPrototyping();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
       frame.setResizable(false);
       frame.setTitle("Rapid Prototyping");
   }
}

NEED a two paragraph report on this GUI painting app.

Homework Answers

Answer #1

The GUI Panel contains a set of options to draw the different shapes.. We can select the shape of drawing using the dropdown, While the color of the shape is choosen using the radio buttons..
GUI also contains a clear button, which is use to remove all the drawings on the canvas. We also can change the thickness of lines using a dropdown.
We keep the current color as the class attributes, which is updated everytime a different radio button is selected using the itemStateChanged method of ComboBox. Similarly, Using the MousePressed and MouseReleased method, we capture the start and end position of the mouse drag and on basis of that, the shape is drawn.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT