SIMPLE PAINTING GUI APP in Java
Your mission in this exercise is to implement a very simple Java painting application. Rapid Protyping
The JFrame app must support the following functions:
(you can use any other programming languages that you are comfortable)
Draw curves, specified by a mouse drag.
Draw filled rectangles or ovals, specified by a mouse drag (don't worry about dynamically drawing the shape during the drag - just draw the final shape indicated).
Shape selection (line, rectangle or oval) selected by a combo box OR menu.
Color selection using radio buttons OR menu.
Line thickness using a combo box OR menu.
A CLEAR button.
You should read through the Java Swing Tutorial on Writing Event Listeners first.
For help on specific Swing components see How to
Some other tips to get you started:
Put import java.awt.*; and import java.awt.event.*; at the top of your java source.
To find the mouse coordinates of a mouse event (e.g., a click), use the int getX() and int getY() methods on the MouseEvent object.
Remember that getGraphics() returns a Graphics object that represents one set of drawing parameter settings for the JFrame (there is no global Graphics object!).
Heres a code snippet to draw a blue dot at X=10, Y=100 on the JFrame:
Graphics G=getGraphics();
G.setColor(Color.BLUE);
G.drawRect(10,100,1,1);
Here's the mouse dragged handler we wrote in class for a (lame) painting function on the JFrame:
private void myMouseDragged(java.awt.event.MouseEvent evt) { int x=evt.getX(); int y=evt.getY(); java.awt.Graphics G=getGraphics(); G.drawRect(x, y, 1, 1); }
And another snippet to find out which item was selected from a combo box:
public void actionPerformed(ActionEvent e) {
JComboBox cb=(JComboBox)e.getSource();
String itemName=(String)cb.getSelectedItem();
}
---------- >>>>> Please, also include a brief writeup(2 paragraphs) to describe about the program structure. Thank You
code
solution
//output
//copyable code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
//Class that paints according to the user wish.
public class SimplePainting extends JFrame implements MouseListener,ItemListener,ActionListener,MouseMotionListener {
//color,shapes and thickness
JPanel panel1;
//combobox
JComboBox shapes;
// radio buttons
JRadioButton RED, green, BLUE;
//combobox
JComboBox line_thicknesses;
//button.
JButton clear;
JPanel center;
/*color selection*/
Color color;
int thickness;
String shape;
//start and end positions
int start_X, start_Y;
int end_X, end_Y;
boolean start_Set;
final String shapes_List[] = { "RECTANGLE", "OVAL", "LINE" };
final String line_thicknessesList[] = { "3", "6", "9", "12", "15" };
SimplePainting() {
//initialize values.
setSize(600, 400);
setLayout(new BorderLayout());
panel1 = new JPanel(new FlowLayout());
JLabel shape_Label = new JLabel("Shape :");
shapes = new JComboBox(shapes_List);
JLabel col_Label = 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 thickness_Label = new JLabel("thickness :");
line_thicknesses = new JComboBox(line_thicknessesList);
clear = new JButton("Clear");
center = new JPanel();
//add elements to panel
panel1.add(shape_Label);
panel1.add(shapes);
panel1.add(col_Label);
panel1.add(RED);
panel1.add(green);
panel1.add(BLUE);
panel1.add(thickness_Label);
panel1.add(line_thicknesses);
panel1.add(clear);
add(panel1, BorderLayout.NORTH);
add(center,BorderLayout.CENTER);
/*Listeners*/
addMouseListener(this);
RED.addItemListener(this);
BLUE.addItemListener(this);
green.addItemListener(this);
shapes.addActionListener(this);
clear.addActionListener(this);
line_thicknesses.addActionListener(this);
/* Default Values */
color = Color.RED;
RED.setSelected(true);
thickness = 3;
shape = "RECTANGLE";
start_Set = false;
}
//action listener
public void actionPerformed(ActionEvent ae1)
{
//set shape value
if(ae1.getSource() == shapes)
{
shape = ""+shapes.getItemAt(shapes.getSelectedIndex());
}
//repaint the frame
else if(ae1.getSource() == clear)
{
repaint();
}
//change value.
else if(ae1.getSource() == line_thicknesses)
{
try
{
thickness = Integer.parseInt(""+line_thicknesses.getItemAt(line_thicknesses.getSelectedIndex()));
}
catch(Exception e)
{
thickness = 3;
}
}
}
//set color value
public void itemStateChanged(ItemEvent ie1)
{
if(ie1.getSource() == RED)
{
color = Color.RED;
}
else if(ie1.getSource() == BLUE)
{
color = Color.BLUE;
}
else if(ie1.getSource() == green)
{
color = Color.GREEN;
}
}
//mouse pressed
public void mousePressed(MouseEvent e)
{
if(start_Set == false)
{
start_X = e.getX();
start_Y = e.getY();
start_Set = true;
}
}
// mouse is released
public void mouseReleased(MouseEvent e)
{
if(start_Set)
{
end_X = e.getX();
end_Y = e.getY();
start_Set = false;
drawShape();
}
}
//when mouse is dragged
public void check_Swap()
{
//swap value of x and y
if(start_X > end_X)
{
int temp = start_X;
start_X = end_X;
end_X = temp;
}
if(start_Y > end_Y)
{
int temp = start_Y;
start_Y = end_Y;
end_Y = temp;
}
}
//method drawShape() t
public void drawShape()
{
Graphics2D g21 = (Graphics2D) getGraphics();
g21.setStroke(new BasicStroke(thickness));
g21.setColor(color);
int width = Math.abs(end_X-start_X);
int height = Math.abs(end_Y-start_Y);
System.out.println("\nThickness: "+thickness);
if(shape.equals(("RECTANGLE")))
{
//check shape
check_Swap();
System.out.printf("Drawing %s : at -> %d %d with width and height %d,%d end point: %d %d\n",shape,start_X,start_Y,width,height,end_X,end_Y);
g21.fillRect(start_X,start_Y,width,height);
}
else if(shape.equals("OVAL"))
{
check_Swap();
System.out.printf("Drawing %s : at -> %d %d with width and height %d,%d end point: %d %d\n",shape,start_X,start_Y,width,height,end_X,end_Y);
g21.fillOval(start_X,start_Y,width,height);
}
else
{
System.out.printf("Drawing %s : at -> %d %d end point: %d %d\n",shape,start_X,start_Y,width,height,end_X,end_Y);
g21.drawLine(start_X, start_Y, end_X, end_Y);
}
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
}
public static void main(String[] args) {
SimplePainting frame1 = new SimplePainting();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
frame1.setResizable(false);
frame1.setTitle("simple painting");
}
}
Get Answers For Free
Most questions answered within 1 hours.