Question

SIMPLE PAINTING GUI APP in Java Your mission in this exercise is to implement a very...

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

Homework Answers

Answer #1

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");

   }

}

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
   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;...
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...
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...
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
The premise of this project is to make a basic GUI and code for the game...
The premise of this project is to make a basic GUI and code for the game Keno. The specifics of the gui and game are below. Description: In this project you will implement the popular casino and state lottery game, Keno. This is a somewhat simple game to understand and play which should allow you to focus on learning GUI development in JavaFX and trying your hand at event driven programing. Implementation Details: You may add as many classes, data...
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...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...