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

  Create a book class for the library application project

Create a Book class for the Library application project. In addition to properties of author and isbn, with setters/getters, the Book should override Object's base method "boolean equals(Object)" and have a "boolean validate()" method.

  Write a program using an array that will store

Using java programing : Write a program using an array that will store input data. Prompt the user for and read 6 numbers between 70 and 90. Verify the numbers should be greater than 70 and less than 90. If the number is less than

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  Write down java console application to add new student

Write down java console application presents the following menu. Add new student and scores in Biology, Physics and chemistry Search for a given student.

  The objective is to implement a menu-based system

The objective is to implement a menu-based system for Bank Accounts in order to simulate a very simple banking system. Many structures have to be declared to manage bank accounts.

  Write program that uses top five countries national wealth

Write a program that uses the top five countries' National Wealth to calculate the average of those five countries.

  Implementation of memory management

Implementation of memory management

  Consider a typical certification hierarchy

Why might Bob still not trust that the certificate for Alice truly establishes Alice as the owner of the public key in the certificate?

  Write program with jframe to enter series of names

Write a program with JFrame which permits user to enter series of friends' names and phone numbers and creates file from entered data.

  Write a program to register students for a college

Students have names, addresses and courses. Implement the interface class RegisterStudent. RegisterStudent has one method, public boolean register, which returns the boolean value of true or false if the student is successfully registered for the ..

  Multiple choice java programming questions

Determine which of the following may be a violation of information hiding if inserted for the comment above?

  Write an application that uses string method region

Write an application that uses String method region - Matches to compare two strings input by the user. The application should input the number of characters to be compared and the starting index of the comparison.

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