Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to set up a simple GUI based on JFrame Setting up listeners and responding t

ID: 3826387 • Letter: H

Question

How to set up a simple GUI based on JFrame Setting up listeners and responding to events Drawing simple shapes Dealing with mouse and mouse motion events Directions l. Write a GUI application in Java using Swing that has three buttons and a drawing area 2. Give the window an initial size of 800 x 800 pixels, and put your name in the menu bar. 3. The buttons should be at the top of the window and be labeled "Ova Rectangle", and Specia 4. The drawing area should cover the remainder of the window. 5. When it starts, the program should show nothing in the drawing area, but the background of the drawing area should be a non-white color. 6. Pressing the Oval or Rectangle buttons should toggle (turn on or off the display of an oval or rectangle, either or both of which must be visible at the same time. When drawn, the oval and rectangle should be different colors of your choice 7. You must also be able to click and drag the rectangle or oval around the screen with the mouse 8. When the user presses the "special" button, the program should do something else not described in the assignment that is unique to your program, such as change the color of all the things you draw, switch the oval to be outlines instead of filled, or draw your name in the middle of the window. 9. As always, make sure the proper block comment is at the top of your main file with your name 10. Once your program is working, pass it off directly to the instructor or TA. Also, turn in your code to D2L.

Explanation / Answer


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Shapes extends JFrame implements ActionListener,MouseMotionListener {
JButton bt1,bt2,bt3;
JPanel pl1,pl2;
int choice=0;
int x,y;
public Shapes(){
bt1=new JButton("Oval");
bt2=new JButton("Rectangle");
bt3=new JButton("Special");
pl1=new JPanel();
pl2=new JPanel();
setLayout(new BorderLayout());
pl1.add(bt1);
pl1.add(bt2);
pl1.add(bt3);
add(pl1,BorderLayout.NORTH);
add(pl2,BorderLayout.CENTER);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
pl2.addMouseMotionListener(this);
}
public static void main(String args[]){
Shapes s=new Shapes();
s.setTitle("Your name");
s.setSize(800, 800);
s.setVisible(true);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
if(choice==1){
g.setColor(Color.red);
g.fillOval (100, 150, 50, 75);
}else if(choice==2){
g.setColor(Color.green);
g.fillRect (200, 250, 100, 75);
}else if(choice==3){
super.paint(g);
g.setColor(Color.blue);
g.fillRect (x, y, 50, 75);
}else{
g.setColor(Color.yellow);
g.fillRect (200, 350, 200, 150);
g.setColor(Color.cyan);
g.fillOval (200, 350, 200, 150);

}

}
@Override
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if(str.equals("Oval")){
choice=1;
repaint();
}else if(str.equals("Rectangle")){
choice=2;
repaint();
}else{
choice=0;
repaint();
}
}
@Override
public void mouseDragged(MouseEvent me){
x=me.getX();
y=me.getY();
choice=3;
repaint();

}
@Override
public void mouseMoved(MouseEvent me){
x=me.getX();
y=me.getY();
choice=3;
repaint();

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote