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.
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.
Get Answers For Free
Most questions answered within 1 hours.