How to set up a simple GUI based on JFrame Setting up listeners and responding t
ID: 3828930 • 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 Write a GUI application in Java using Swing that has three buttons and a drawing area. Give the window an initial size of 800 x 800 pixels, and put your name in the menu bar. The buttons should be at the top of the window' and be labeled "Oval", "Rectangle", and "Special". The drawing area should cover the remainder of the window. 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. Pressing the Oval or Rectangle buttons should toggle (turn on or oft') 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. You must also be able to click and drag the rectangle or oval around the screen with the mouse. 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. As always, make sure the proper block comment is at the top of your main file with your name. Once your program is working, pass it off directly to the instructor or TA. Also, turn in your code to D2L. Get the skeleton of your GUI working first. First get the buttons drawing properly, then go on to listen to and respond to events. Initially, hook up the event listeners so that they print something to the console when a button is pressed. That way you can know that your code is getting events. Make a subclass of JPanel called "DrawPanel" or something like that to draw the objects. Override the "paintComponent" method of this class. This class needs boolean variables to decide whether to draw the rectangle, oval, your name, and the special. It may also need other variables to indicate colors, positions of things, etc. You can measure a string using the FontMetrics class. This measures how much space (in pixels) the string will take when drawn. You would use this information to determine where to place the text and backing rectangle.Explanation / Answer
1.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class Swing1{
public static void main(String args[]){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300); // Values can be changed to change the window size of Output Window.
frame.setLayout(new GridLayout(3, 1)); //Layout of the output window can be changed according to //application requirement eg:BoxLayout(),FlowLayout() etc.
JButton button1 = new JButton("Button 1");
frame.getContentPane().add(button1); // Adds Button to content pane of frame
JButton button2 = new JButton("Button 2");
frame.getContentPane().add(button2); // Adds Button to content pane of frame
JButton button3 = new JButton("Button 3");
frame.getContentPane().add(button3); // Adds Button to content pane of frame
JPanel jp=new javax.swing.JPanel(){ //JPanel provides feature of Drawing .
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Line2D line = new Line2D.Double(20, 50, 40, 40);
g2.setColor(Color.black);
g2.setStroke(new BasicStroke(10));
g2.draw(line);
}
};
jp.setBackground(new Color(255, 255, 255)); //Setting background color of the JPanel as white.
frame.add( jp);
frame.setVisible(true);
}
}
2
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class gui {
public static void main(final String args[]) {
JFrame frame = new JFrame("Question 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Name"); //Item in menubar
fileMenu.setMnemonic(KeyEvent.VK_N); //Assigning keyboard shortcut to menubar Items
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("Menu", KeyEvent.VK_M);
fileMenu.add(newMenuItem);
frame.setJMenuBar(menuBar);
frame.setSize(800, 800);
frame.setVisible(true);
}
}
3.
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class gui {
public static void main(final String args[]) {
JFrame frame = new JFrame("Question 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1=new JButton("Oval");
frame.add(button1);
JButton button2=new JButton("Rectangle");
frame.add(button2);
JButton button3=new JButton("Special");
frame.add(button3);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
4.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class gui{
public static void main(String args[]){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300); // Values can be changed to change the window size of Output Window.
//application requirement eg:BoxLayout(),FlowLayout() etc.
JPanel jp=new javax.swing.JPanel(){ //JPanel provides feature of Drawing .
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;}
};
jp.setBackground(new Color(155,200,80)); //Setting background color of the JPanel other then white.
frame.add( jp);
jp.setVisible(true);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.