Shows the users name and program name

Assignment Help JAVA Programming
Reference no: EM13161794

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: EM13161794

Questions Cloud

Write a program that asks a user for a file name and prints : Write a program that asks a user for a file name and prints the number of characters, words, and lines in that file.
The rpn calculator program : The RPN calculator program should read the RPN expression as an entire line from stdin.Input will consist of a single line. After completing the evaluation of the expression, the program should print the contents of the entire stack, starting with th..
Function which correctly sorts three : Write an x8086 HLA Assembly language program that implements a function which correctly sorts three parameters and returns a boolean value in AL which should be set to true if any swaps were performed to sort the sequence in increasing order.
Cashregister class that can be used with the retailitem clas : Write a CashRegister class that can be used with the RetailItem class that you wrote in Part 1. The CashRegister class should simulate the sale of a retail item. It should have a constructor that accepts a RetailItem object as an argument.
Shows the users name and program name : 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 s..
The program should not accept quantities : Input Validation: The program should not accept quantities, or wholesale or retail costs, less than 0. The program should not accept dates that the programmer deter- mines are unreasonable.
Perform an insertion sort on the file pointed : Using only the local data already supplied in FileSort, perform an insertion sort on the file pointed to by fd. Use lseeks for this; do not try to create any sort of array or list. An array-based version of insertion is supplied for your reference.
Using matlab and for loops : Using MATLAB and for loops, provide an animation that follows the below steps: Start with a square at the origin with each side being 5 units long. Imagine someone kicked the box and animate it moving on a projectile motion trajectory. Hint: look at ..
Postorder traversal print a heap in sorted order? : Will preorder, inorder, or postorder traversal print a heap in sorted order? why or why not? use the following numbers to prove your point 85 86 88 89 90 91 92

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write java program to compute how much federal need to pay

Write a java application to calculate how much federal and state tax you need to pay. The program should accomplish the following task.

  Write a method called writenums

Write a method called writeNums that takes an integer n as a parameter and prints to the console the first n integers starting with 1 in sequential order, separated by commas. For example, consider the

  Write java application to input three integers from user

Write Java application that inputs three integers from user and displays sum, average, product, smallest, and largest of the numbers.

  Java program for line item application

This exercise explains you the process of testing and enhancing Line Item application. Open LineItemApp, Validator, Product, LineItem, and ProductDB classes that are in the c:java1.6ch06LineItem directory and review this code.

  Java threads

This is an introductory assignment on Java synchronization. You will use Java Threads while learning more about concurrency and achieving atomicity using Java’s inbuilt mechanisms.

  Write a class array that encapsulates an array

Write a class Array that encapsulates an array and provides bounds-checked access. The private instance variables should be int index and inarray[10]. The public members should be a default constructor and methods (signatures shown below) to provide ..

  Java program which demonstrates cellular automata

Using the internet, locate a Java program (or Java applet) which demonstrates "Cellular Automata", in particular "the Game of Life".

  Develop a gui based java program

Designing and developing a College Registration program

  Method to store the product of the two arrays in third array

Write a method to store the product of the two arrays in the third array

  Write java program to select pine for furniture company

Write down the Java program for the furniture company. Ask user to select P for Pine, O for Oak or M for Mahogany.

  Describe the steps to program development

What is the function of parseFloat and parseInt and what will be result if we send "abcd" through a prompt() input and pass it through parseInt()?

  Write java program to store values retrieved from user-array

Write a Java program named Arrays6_2GF.java Use an array to store values retrieved from user input. The number of integers to be entered (the length of the array )should also be retrieved through user input.

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