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: 3826047 • 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.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;

public class ButtonExample
{
public static void main(String[] args)
{
final JFrame frame=new JFrame("Button Example");
//final JTextField tf=new JTextField();
//tf.setBounds(50,50, 150,20);
final JButton button1=new JButton("Oval");
final JButton button2=new JButton("Recangle");
final JButton button3=new JButton("special");
button1.setBounds(100,100,95,30);
frame.add(button1);
button2.setBounds(200,100,95,30);
frame.add(button2);
button3.setBounds(300,100,95,30);
frame.add(button3);

frame.setSize(500,500);
frame.setLayout(null);
frame.setVisible(true);

MyPanel panel = new MyPanel();
frame.add(panel);
  
button1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
button1.setBackground(Color.red);
  
}
});
button2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
button2.setBackground(Color.blue);
}
});
button3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
button3.setBackground(Color.green);
}
});

}
}
class MyPanel extends JPanel {
public void paint(Graphics g) {
super.paintComponent(g);
g.drawRect(100,100,10,10);
g.setColor(Color.RED);
g.fillRect(230,80,10,10);
}
}

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