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....
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
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);...
   import javax.swing.*; import java.awt.*; import java.awt.event.*; //Class that paints according to the user wish. public...
   import javax.swing.*; import java.awt.*; import java.awt.event.*; //Class that paints according to the user wish. public class RapidPrototyping extends JFrame implements MouseListener,ItemListener,ActionListener,MouseMotionListener {       //panel to hold color,shapes and thickness components    JPanel panel;    //shapes combobox    JComboBox shapes;    //color radio buttons    JRadioButton red, green, blue;    //thickness combobox    JComboBox thicknesses;    //clear button.    JButton clear;    JPanel center;       /*values of each selection*/    Color color;    int thickness;    String shape;...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
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);...
what output is produced by the following code and explain how it works. public class A...
what output is produced by the following code and explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT