In: Computer Science
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES:
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);
}
}
I will explain the code class by class
Main class have the main function which is executed and it just calls the MyFrame Consturctor.
MyFrame Constructor just creates a panel by calling the MyPanel class. It also extends the JFrame , So it creates the basic element of swing Graphics i.e. a Frame. And it also set some default properties to the frame like below:
1. what happens when the frame is closed. It set it to EXIT_ON_CLOSE. which means , that when frame is closed, program is exited.
2. It also adds the panel created by MyPanel class to the frame using the statement this.add(panel);
3. It also packs the frame to its subcomponent i.e. Panel using the line this.pack();. It means that Frame will take the size of its subcomponent i.e Panel.
4. It also sets the precondition that Frame will not be shown relative to any other window or frame using the statement this.setLocationRelativeTo(null);
5. Finally after setting the above combination, it just displays the Window or the Frame with a Panel created by MyPanel class inside it. This display happens because of the statement this.setVisible(true);
MyPanel.java has a lot of commented code, So first I will explain what it will do with the uncommented code.
MyPanel extends JPanel so it creates a panel that is displayed inside the frame created above.
It also sets the dimensions of the Panel to width and height = 500 using the statement this.setPreferredSize(new Dimension(500, 500)); Please note that, this size will also be taken by the frame as explained above.
Because MyPanel extends JPanel, It also overrides the paint method of JPanel. paint method takes a Graphic object as a paramter and it defines what needs to be done. It does nothing but divide the panel into a 2D graph and draw a line from (0,0) to (500,500) i.e. from top left to bottom right diaganol. Below is the output
Now if we see the commented code,
The MyPanel constructor first defines an image with below statement
// image = new
ImageIcon("sky.png").getImage();
Then the image is drawn to the panel from (0,0) with below statement
g2D.drawImage(image, 0, 0, null);
Then it tries to draw various shapes in panel like Rectangle, Oval, Arc , Polygon etc with different colors.
At last it also draws a String from (50,50) point in the 2D Panel
using statement below
g2D.drawString("U R A WINNER! :D", 50, 50);
Hope I was able to explain everything . If needed more info please comment. I'll happy to help