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

  Java''s type int has limit onhow large aninteger it can store

Java's type int has a limit on how large an integer it can store. This limit can be circumvented by representing an integer as an array of digits. Write an interactive program that adds two integers of up to 50 digits each.

  Write java program to accept two words as input

Write a Java program that accepts two words as input and determines if one of them is resulting from changing the order of the others' letters.

  Write java program to display results in java applet

Write down the java program which displays following results in java applet. Permits the user to enter three numbers (use JOptionPane for this) and prints out average of those value on screen.

  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.

  The drink machine should have a supply

When the applet starts, the drink machine should have a supply of 20 of each of the drinks. The applet should have a text field where the user can enter the amount of money he or she is giving the machine. The user can then click on a button to selec..

  Write a java class

Write a Java class called PQueue that extends the provided abstract QueueADT class.

  Create the html form and call the servlet to print out

1. write a calculator Servlet that adds, subtract, multiples and divides. Create the html form and call the servlet to print out the answer

  Develop a reliable transfer protocol over udp

Develop a reliable transfer protocol over UDP. Focus on a Stop- and-Wait protocol.

  Create a driver class in java

Your project is to create a driver class that uses SuperJavaIceCreamClass.

  Print the contents of the array

Prepare a second loop that prints the contents of the array

  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

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