Question

In: Computer Science

Run the following Java code and explain what it does. Show the image created by the...

Run the following Java code and explain what it does.

Show the image created by the code.

public class Main{

public static void main(String[] args) {

new MyFrame();

}

}

import javax.swing.*;

public class MyFrame extends JFrame{

MyPanel panel;

MyFrame(){

panel = new MyPanel();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.add(panel);

this.pack();

this.setLocationRelativeTo(null);

this.setVisible(true);

}

}

import java.awt.*;

import javax.swing.*;

public class MyPanel extends JPanel{

//Image image;

MyPanel(){

//image = new ImageIcon("sky.png").getImage();

this.setPreferredSize(new Dimension(500,500));

}

public void paint(Graphics g) {

Graphics2D g2D = (Graphics2D) g;

//g2D.drawImage(image, 0, 0, null);

g2D.setPaint(Color.blue);

g2D.setStroke(new BasicStroke(5));

g2D.drawLine(0, 0, 500, 500);

//g2D.setPaint(Color.pink);

//g2D.drawRect(0, 0, 100, 200);

//g2D.fillRect(0, 0, 100, 200);

//g2D.setPaint(Color.orange);

//g2D.drawOval(0, 0, 100, 100);

//g2D.fillOval(0, 0, 100, 100);

//g2D.setPaint(Color.red);

//g2D.drawArc(0, 0, 100, 100, 0, 180);

//g2D.fillArc(0, 0, 100, 100, 0, 180);

//g2D.setPaint(Color.white);

//g2D.fillArc(0, 0, 100, 100, 180, 180);

//int[] xPoints = {150,250,350};

//int[] yPoints = {300,150,300};

//g2D.setPaint(Color.yellow);

//g2D.drawPolygon(xPoints, yPoints, 3);

//g2D.fillPolygon(xPoints, yPoints, 3);

//g2D.setPaint(Color.magenta);

//g2D.setFont(new Font("Ink Free",Font.BOLD,50));

//g2D.drawString("U R A WINNER! :D", 50, 50);

}

}

Solutions

Expert Solution

EXPLANATION OF THE CODE

the given code contains 3 classes named as MyPanel,MyFrame and Main

MyPanel.java

this module imports java awt and java swing which is GUI widget toolkit for Java.

in MyPanel() method we create a panel(it is a container which provides space in which an application can attach any other component) of size 500 x 500

in paint() method we create the graphics object and then we set the color to blue by setPaint() method.

and we pass the start and end coordinate i.e 0,0 and 500,500 to drawLine() method which will draw a line from 0,0 to 500,500

MyFrame.java

in this module we create frame by extending JFrame.it is like the main window where components like labels, buttons, textfields are added to create a GUI.

we add panel to the frame and we set visibility of frame to true and we also defined close operation here.

Main.java

this module contains main method where we call MyFrame() method.

SCREENSHOT OF THE OUTPUT


Related Solutions

Run the following code and explain what the code does Run this code at least twice...
Run the following code and explain what the code does Run this code at least twice and take screenshots Explain what the code does What is the running result supposed to be What caused this issue? #include <thread> #include <iostream> using namespace std; const unsigned int NTHREADS = 20; const int ITERS = 10000000; int counter; void increment() {        for (int i = 0; i < ITERS; i++)                      counter++;               } void decrement() {        for (int i...
Q1: Explain the difference between a real image and a virtual image created by a thin...
Q1: Explain the difference between a real image and a virtual image created by a thin lens. Use one or more of the axioms of ray tracing in the explanation. What's not right with the real image when there is no viewing screen there, just air? Q2: Explain the difference between the inverted image and the real image. Q3: Name and explain two major steps in deriving the thin-lens equation. Write out the thin-lens equation and explain the variables featured.
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
Run the following RISC-V codes separately and explain what each code does and how you interpret...
Run the following RISC-V codes separately and explain what each code does and how you interpret the register results: Code 1. addi x3, x3, 1 slli x3, x3, 62 addi x4, x4, 7 mul x5, x4, x3 mulh x6, x4, x3 Code 2. addi x3, x3, 1 slli x3, x3, 63 addi x4, x4, 1 mul x5, x4, x3 mulhsu x6, x4, x3 Code 3. addi x3, x3, 1 slli x3, x3, 63 addi x4, x4, 1 mul x5, x4,...
Code the following in bash and run. Please show full code and don’t forget to comment...
Code the following in bash and run. Please show full code and don’t forget to comment your code. Make a code that takes any list of numbers and calculates and displays the mean, median and mode.
Does your image show an instance of geometric aberration?
In this question we are testing the geometric and chromatic aberrations we have studied. Use a cell phone camera or a web camera to capture an image of a white sheet of paper with your name on it. - Does your image show an instance of geometric aberration? - Does your image show chromatic aberration? Print your image and Mark the locations of these effects in the images. You can take images of other objects or scenes to show these...
Need Java Code and UML Design for the following program: Use the Account class created above...
Need Java Code and UML Design for the following program: Use the Account class created above to simulate an ATM machine. Create five accounts in an array with id 0, 1, ..., 4, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run (see below). You can...
The following code will generate a Decision Tree. You need to run the code and explain...
The following code will generate a Decision Tree. You need to run the code and explain the tree. After you get the Tree. You need to explain how does it draw like that. install.packages("rpart.plot") # install package rpart.plot ########################################## # section 7.1.1 Overview of a Decision Tree ########################################## library("rpart") library("rpart.plot") # Read the data setwd("c:/data/") banktrain <- read.table("bank-sample-test.csv",header=TRUE,sep=",") ## drop a few columns to simplify the tree drops<-c("age", "balance", "day", "campaign", "pdays", "previous", "month") banktrain <- banktrain [,!(names(banktrain) %in% drops)]...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT