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

Computer Science 1 Java Programming Dr. David Cline Program 8: First GUI Through

ID: 3832413 • Letter: C

Question

Computer Science 1 Java Programming Dr. David Cline Program 8: First GUI Throughout the semester, we have been working on command line applications. While the command line can be a powerful form of interacting with the computer, most modern applications have a graphical user interface, or GUI. This program will give you some initial cxperience making GUI applications with Swing. Things you will learn How to set up a simple GUI bascd on JFrame Setting up listeners and responding to events Drawing simple shapes Dealing with mouse and mouse motion events Directions I. 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 "oval", Rectangle", and Special 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 ofthe drawing area should be a non-white color. 6. Pressing the Oval or Rectangle buttons should toggle (turn on or ot 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. 0. Once your program is working, pass it off directly to the instructor or TA. Also, tum in your code to D2L.

Explanation / Answer

gui.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;


public class gui {
/**
* @param args
*/
public static void main(String[] args)
{
final JFrame frame = new JFrame();

final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 800;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Simple Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());

JPanel panel = new JPanel();
frame.add(panel, BorderLayout.NORTH);

class RectangleDrawListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{   
RectangleComponent r2 = new RectangleComponent();
frame.add(r2);
r2.revalidate();
frame.getRootPane().revalidate();
r2.repaint();
}
}

class OvalDrawListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{   
OvalComponent r1 = new OvalComponent();
frame.add(r1);
r1.revalidate();
frame.getRootPane().revalidate();
r1.repaint();
}
}

class SpecialDrawListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{   
JOptionPane.showMessageDialog(null, "Hello World");
}
}


JButton ovalButton = new JButton("Oval");
JButton rectButton = new JButton("Rectangle");
JButton specialButton = new JButton("Special");

ActionListener ovalDrawListener = new OvalDrawListener();
ovalButton.addActionListener(ovalDrawListener);
panel.add(ovalButton);

ActionListener rectDrawListener = new RectangleDrawListener();
rectButton.addActionListener(rectDrawListener);
panel.add(rectButton);

ActionListener specialDrawListener = new SpecialDrawListener();
specialButton.addActionListener(specialDrawListener);
panel.add(specialButton);

frame.setVisible(true);
}
}

RectangleComponent.java

import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;

public class RectangleComponent extends JComponent
{
Rectangle rect;

public RectangleComponent()
{
rect = new Rectangle(100, 100, 200, 200);
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.draw(rect);
}
}

OvalComponent.java

import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;

public class OvalComponent extends JComponent
{


public OvalComponent()
{
  
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.drawOval (10, 10, 200, 200);
}
}

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