Question

Describe the following Java code. Give screenshots of the image it produces public class main{ public...

Describe the following Java code. Give screenshots of the image it produces

public class main{

public static void main(String[] args{

new MyFrame();

}

}

import javax.swing.*;

public class MyFrame extends JFrame{

MyPanel panel;

MyFrame(){

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(Colo.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!:",50,50);

}

}

Homework Answers

Answer #1

MyPanel is the drawing program.

It draws the following shapes.

1. It draws a diagonal line from top left to bottom right. g2D.drawLine(0, 0, 500, 500);

2. A rectangle in top left corner. g2D.drawRect(0,0,100,200);

3. It creates a circle and fills with orange color. g2D.drawOval(0,0,100,100);

4. It creates a semi-circle inside the circle with white color. g2D.fillArc(0,0,100,100,180,180);

5. It creates a triangle by drawing a polygon with 3 sides. g2D.drawPolygon(xPoints,yPoints,3);

6. All the images are placed in a sky background.

Following is the output of the program.

Following is the modified MyFrame.java

import javax.swing.*;

public class MyFrame extends JFrame {

   MyPanel panel = new MyPanel();

   MyFrame() {

       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       this.add(panel);

       this.pack();

       this.setLocationRelativeTo(null);

       this.setVisible(true);

   }

}

Following is the modified MyPanel.java

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.*;

public class MyPanel extends JPanel {

java.awt.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!:",50,50);

   }

}

  

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Consider the following Java program. Describe what it does in response to specific operations of the...
Consider the following Java program. Describe what it does in response to specific operations of the mouse, and how it does it. (You are encouraged to run the program for yourself to test its behavior. Then read through the program carefully to understand how that behavior arises.) import java.awt.event.*; import javax.swing.*; public class MouseWhisperer extends JFrame implements MouseListener {     MouseWhisperer() {         super("COME CLOSER");         setSize(300,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         addMouseListener(this);         setVisible(true);     }     public void mouseClicked(MouseEvent e)...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Hello, I need a output for the following code as well as screenshots from the eclipse...
Hello, I need a output for the following code as well as screenshots from the eclipse page showing the output. Thank you public class COMP1050 { public static void main(String[] args) { System.out.printf("before"); new CS(); System.out.printf(".after"); } } public class CS extends WIT { public CS() { System.out.printf(".cs"); } } public class WIT { public WIT() { System.out.printf(".wit"); } }
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...