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
Consider the following Java program. What is the superclass of "Clicker"? import java.awt.event.*; import javax.swing.*; public...
Consider the following Java program. What is the superclass of "Clicker"? 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(); } } Select one: a. ActionEvent b....
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...
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...
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...
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"); } }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT