Java program the program has a page that shows the users

Assignment Help JAVA Programming
Reference no: EM13375993

Java program, the program has a page that shows the users name and program name. a second jpanel that shows 4 buttons (circle square rectangle and oval) the problem i am having is that my program is not dropping where i click and the shapes are not staying on the page. can anyone help?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class ShapeStamper extends JFrame
{
Random rand = new Random();
public int x;
public int y;
private JPanel panel1, panel2;
private JButton button1, button2, button3, button4;
private int choice = 0;
public ShapeStamper()
{
super("Shape Stamper!");
panel1 = new JPanel();
button1 = new JButton("Circle");
button2 = new JButton("Square");
button3 = new JButton("Rectangle");
button4 = new JButton("Oval");
  
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
choice = 1;
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
choice = 2;
}
});
button3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
choice = 3;
}
});
button4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
choice = 4;
}
});
panel2 = new JPanel();
panel2.setBackground(Color.WHITE);
MouseHandler mouse = new MouseHandler();
setVisible(true);
addMouseListener(mouse);
addMouseMotionListener(mouse);
add(panel2);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
  
add(panel1, BorderLayout.SOUTH);
setSize(500, 500);
setVisible(true);
}//end ShapeStamper()
private ArrayList<Rectangle2D> arrOfRect = new ArrayList<>();
private ArrayList<Ellipse2D> arrOfEllipse = new ArrayList<>();
  
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
for (Rectangle2D r: arrOfRect)
{
g2.draw(r);
}
for (Ellipse2D e: arrOfEllipse)
{
g2.draw(e);
}
}//end paintcomponenets(graphics g)
  
private class MouseHandler extends MouseAdapter implements MouseMotionListener
{
public void mousePressed(MouseEvent e)
{
x = e.getX();
x = e.getY();
  
repaint();
}
}//end mousehandler
  
public void paint(Graphics g)
{
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
if(choice == 0)
{
g.setFont(new Font("Serif", Font.BOLD, 32));
g.drawString("Shape Stamper!", 150, 220);
g.setFont(new Font("Serif", Font.ITALIC, 16));
g.drawString("Programmed by", 150, 230);
}
if(choice == 1)
{
Ellipse2D ellipse = new Ellipse2D.Double(x, y, 100,100);
arrOfEllipse.add(ellipse);
Color randColor1 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor1);
g2d.drawOval(x, y, 100, 100);
}
if(choice == 2)
{
Rectangle2D rectangle = new Rectangle2D.Double(x, y, 100, 100);
arrOfRect.add(rectangle);
Color randColor2 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor2);
g2d.drawRect(x, y, 100, 100);
}
if(choice == 3)
{
Rectangle2D rectangle = new Rectangle2D.Double(x, y, 150, 100);
arrOfRect.add(rectangle);
Color randColor3 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor3);
g2d.drawRect(x, y, 150, 100);
}
if(choice == 4)
{
Ellipse2D ellipse = new Ellipse2D.Double(x, y, 100,50);
arrOfEllipse.add(ellipse);
Color randColor4 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor4);
g2d.drawOval(x, y, 100, 50);
}
  
}
public static void main(String[] args)
{
ShapeStamper shape = new ShapeStamper();
shape.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

 

 

Reference no: EM13375993

Questions Cloud

For given color image with illumination colorwhite the : for given color image with illumination colorwhite the problem is to remove this illuminant color from the image. to
A bernstein copula is a nonparametric copula which can be : a bernstein copula is a nonparametric copula which can be described by a given grid resolution and an associated
The circuit arrangements for each of the installations the : the circuit arrangements for each of the installations the final circuit cable selection the maximum demand estimation
In this program you will implement a simple reverse polish : in this program you will implement a simple reverse polish notation rpn calculator. rpn is a notation in which
Java program the program has a page that shows the users : java program the program has a page that shows the users name and program name. a second jpanel that shows 4 buttons
One way of purifying gaseous h2 is to pass it under high : one way of purifying gaseous h2 is to pass it under high pressure through the holes of a metals crystal structure.
1nbsp below are six statementsnbsp indicate whether each : 1nbsp below are six statements.nbsp indicate whether each one pertains to microeconomics mic or macroeconomics
Lets take a look at how we might model the eect of : lets take a look at how we might model the eect of increased income or wealth on peoples preferences for dierent kinds
Bob and bette rhymes with jetty each have cobb-douglas : bob and bette rhymes with jetty each have cobb-douglas preferences for cheese c and peanut butter p each of which will

Reviews

Write a Review

JAVA Programming Questions & Answers

  Explain the legal doctrine benefits balancing

Explain the legal doctrine "Benefits Balancing" as it pertains to applying the reasonable standard of care doctrine in the medical fields. Does a defense that the majority of physicians normally do not give a particular diagnostic test in the normal ..

  Uml exercise

UML Exercise: Automated Teller Machine (ATM),   1. To allow authorized card holders to make transactions,   Brief Summary of Requirements:

  Program to track hourly employee arrival and departure time

THE JAVA SOURCE CODEA company hires you to write a program to track hourly employee arrival and departure times from work. In essence, you are tasked to make an online time clock. The time clock shall keep a history of an employee’s hours for a two-w..

  Java servlet uses doget to return markup document

Write down Java servlet which uses doGet to return markup document which provides your name, e-mail address, and mailing address along with a brief autobiography.

  Write a recursive public method

Write a recursive public method in our BST class that returns a reference to the information in the node containing the smallest value in the tree. The signature of the method is

  Assignment covers the subsequent eight topics and explore

assignment covers the subsequent eight topics and explore the implementation of memory management processes and

  Develop a program that will handle a golfer and his scores

develop a program that will handle a Golfer and his scores. The program will be comprised of two classes: a Golfer class that will manage all the golfer's scores and a Score class.This is a class designed to represent a golfer including his name, hom..

  Create an employee class.

Create an Employee class. Items to include as data members are employee number, name, date of hire, job description, department, and monthly salary.

  Creating the gui for the game interface

A GUI-based application that allows a user to play a simple trivia game

  Graphics program that draws 12 strings

Write a graphics program that draws 12 strings, one each for the 12 standard colors, besides Color.WHITE, each in its own color. Provide a class ColorNameViewer and a class ColorNameComponent.

  Develop class which implements interface

Let the ADT called SquareMatrix. (The matrix can be represented by 2-D array of ints w/ n rows and n columns.) Write specification for ADT as Java interface. Develop the class which implements interface.

  Organization structure change over time

How and why did the organization structure change over time, and in what ways were some of these changes responsible for some of their recent problems?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd