Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I already have the code below for my program. I Cannot figure out how to run thi

ID: 3554708 • Letter: I

Question

I already have the code below for my program. I Cannot figure out how to run this as an JApplet. Please convert this for me and leave instructions. I have tried to youtube and google it, but still cannot figure it out.

This week you learned how to create a program that runs as a application or an applet. Select a graphical program that you wrote for a previous assignment.

Notepad and file extensions

Changing Notepad default file extension to .htm or .html

If you are creating a new file in Notepad and typing everything in from scratch. When you go to save the file put " " (double quotes) around the file name or Notepad will add a .txt extension on the file. So, File, Save As

"Assign122.htm" produces: Assign122.htm

where as

Assign122.htm produces: Assign122.htm.txt

If you have files extensions turned off for known file types, you will not see the added .txt extension. To fix that go to a MyComputer window and from the menu, select: Tools, Folder Options, View tab and uncheck the check box that says "Hide extensions for known file types"

When you go to open an existing HTML file in Notepad, you need to change the Text Documents (*.txt) type to All Files (*.*) Then you will see your html files listed and can click on them to open. Otherwise, you can right click the file and select Open with and select Notepad from the list of files.

WINDOWS 7(from Microsoft):

Windows 8 (Hyperlink to website with pictures):

http://www.bleepingcomputer.com/tutorials/show-hidden-files-in-windows-8/

After you show hidden file extensions, rename the file from "Assign122.html.txt" to "Assign122.html" and it will prompt you about changing file extensions, click yes, and then run the file in the browser and will work.

Troubleshooting Applet not Running

If the applet does not display in your browser you may have to change some of your security settings.

You need to go to control panel and search for java control panel then changed security settings from high to medium. Hopefully that should allow the applet to display.

Change your Browser settings Java Security Settings to medium.

Check your Java Version

Below is a link to make sure you have the lasted version of Java it installed on your computer:

http://java.com/en/download/installed.jsp

---------------------------------------------------------------------------------------------------------------------------------------------------

package Assign72;

//imports
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.*;
import java.util.ArrayList;


public class Assign72 {

   // Create drawing fram
   public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
               DrawingFrame f = new DrawingFrame();
               f.setTitle("Drawing Program");
               f.setSize(600, 500);
               f.setLocationRelativeTo(null);
               f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               f.setVisible(true);
               f.setResizable(false);

           }
       }
               );
   }

   //Create radio buttons and group them.
   public static class DrawingFrame extends JFrame implements ActionListener {

       private static final long serialVersionUID = 1L;
       private DrawingPanel drawPanel;
       private JPanel panel;
       private JPanel colorPanel;
       private JPanel sizePanel;
       private JRadioButton redRadioButton;
       private JRadioButton blueRadioButton;
       private JRadioButton yellowRadioButton;
       private JRadioButton greenRadioButton;
       private JRadioButton magentaRadioButton;
       private JRadioButton blackRadioButton;
       private JRadioButton smallRadioButton;
       private JRadioButton mediumRadioButton;
       private JRadioButton largeRadioButton;
       private ButtonGroup colorButtonGroup;
       private ButtonGroup sizeButtonGroup;

       //set size values
       static final int SMALL = 5;
       static final int MEDIUM = 10;
       static final int LARGE = 15;
      

      
       public DrawingFrame() {
           colorPanel = new JPanel();
           sizePanel = new JPanel();

           redRadioButton = new JRadioButton();
           redRadioButton.setText("Red");
           colorPanel.add(redRadioButton);
           redRadioButton.addActionListener(this);

           blueRadioButton = new JRadioButton();
           blueRadioButton.setText("Blue");
           colorPanel.add(blueRadioButton);
           blueRadioButton.addActionListener(this);
          
           yellowRadioButton = new JRadioButton();
           yellowRadioButton.setText("Yellow");
           colorPanel.add(yellowRadioButton);
           yellowRadioButton.addActionListener(this);

           greenRadioButton = new JRadioButton();
           greenRadioButton.setText("Green");
           colorPanel.add(greenRadioButton);
           greenRadioButton.addActionListener(this);
          
           magentaRadioButton = new JRadioButton();
           magentaRadioButton.setText("Magenta");
           colorPanel.add(magentaRadioButton);
           magentaRadioButton.addActionListener(this);

           blackRadioButton = new JRadioButton();
           blackRadioButton.setText("Black");
           colorPanel.add(blackRadioButton);
           blackRadioButton.addActionListener(this);

           smallRadioButton = new JRadioButton();
           smallRadioButton.setText("Small");
           sizePanel.add(smallRadioButton);
           smallRadioButton.addActionListener(this);

           mediumRadioButton = new JRadioButton();
           mediumRadioButton.setText("Medium");
           sizePanel.add(mediumRadioButton);
           mediumRadioButton.addActionListener(this);

           largeRadioButton = new JRadioButton();
           largeRadioButton.setText("Large");
           sizePanel.add(largeRadioButton);
           largeRadioButton.addActionListener(this);
          

           colorButtonGroup = new ButtonGroup();
           sizeButtonGroup = new ButtonGroup();

           sizeButtonGroup.add(smallRadioButton);
           sizeButtonGroup.add(mediumRadioButton);
           sizeButtonGroup.add(largeRadioButton);


           colorButtonGroup.add(redRadioButton);
           colorButtonGroup.add(blueRadioButton);
           colorButtonGroup.add(yellowRadioButton);
           colorButtonGroup.add(greenRadioButton);
           colorButtonGroup.add(magentaRadioButton);
           colorButtonGroup.add(blackRadioButton);

           blackRadioButton.setSelected(true);
           largeRadioButton.setSelected(true);
           JPanel configurePanel = new JPanel();
           configurePanel.add(new JButton("Configure"));

           // Panel
           panel = new JPanel();
           panel.setBackground(Color.WHITE);

           drawPanel = new DrawingPanel(Color.BLACK, LARGE);
           drawPanel.setBackground(Color.WHITE);

           this.add(sizePanel, BorderLayout.PAGE_START);
           this.add(colorPanel, BorderLayout.PAGE_END);
           this.add(drawPanel, BorderLayout.CENTER);
       }
       // Actions performed
       @Override
       public void actionPerformed(ActionEvent e) {
           if (redRadioButton.isSelected())
               drawPanel.setCircleColor(Color.RED);
           if (greenRadioButton.isSelected())
               drawPanel.setCircleColor(Color.GREEN);
           if (blueRadioButton.isSelected())
               drawPanel.setCircleColor(Color.BLUE);
           if (yellowRadioButton.isSelected())
               drawPanel.setCircleColor(Color.YELLOW);
           if (magentaRadioButton.isSelected())
               drawPanel.setCircleColor(Color.MAGENTA);
           if (blackRadioButton.isSelected())
               drawPanel.setCircleColor(Color.BLACK);
           if (smallRadioButton.isSelected())
               drawPanel.setCircleDiameter(SMALL);
           if (mediumRadioButton.isSelected())
               drawPanel.setCircleDiameter(MEDIUM);
           if (largeRadioButton.isSelected())
               drawPanel.setCircleDiameter(LARGE);
          
       }
   }

   public static class DrawingPanel extends JPanel implements
           MouseMotionListener {

       private static final long serialVersionUID = 1L;
       private int circleSize;
       private Color circleColor;
       private Circle newCircle;
       private ArrayList<Circle> circleArrayList = new ArrayList<Circle>();

       DrawingPanel(Color colorValue, int size) {
           setCircleColor(colorValue);
           setCircleDiameter(size);
           addMouseMotionListener(this);
       }

       public void setOpaque(boolean isOpaque) {
          
       }

       public void setCircleColor(Color choice) {
           circleColor = choice;
       }

       public Color getCircleColor() {
           return circleColor;
       }

       public void setCircleDiameter(int diameter) {
           circleSize = diameter;
       }

       public int getCircleSize() {
           return circleSize;
       }

       @Override
       protected void paintComponent(Graphics g) {
           super.paintComponents(g);

           // use regular for loop
           for (Circle c : circleArrayList) {
               c.draw(g);
           }
       }

       @Override
       public void mouseDragged(MouseEvent event) {
           newCircle = new Circle(getCircleSize(), event.getPoint(),
                   getCircleColor());
           circleArrayList.add(newCircle);
           repaint();
       }

       @Override
       public void mouseMoved(MouseEvent arg0) {
       }
   }

   public static class Circle {
       private int size;
       private Point point;
       private Color color;

       public Circle(int size, Point point, Color color) {
           setSize(size);
           setLocation(point);
           setColor(color);
       }

       public int getSize() {
           return size;
       }

       public void setSize(int size) {
           this.size = size;
       }

       public Point getLocation() {
           return point;
       }

       public void setLocation(Point point) {
           this.point = point;
       }

       public Color getColor() {
           return color;
       }

       public void setColor(Color color) {
           this.color = color;
       }

       public void draw(Graphics g) {
           g.setColor(color);
           g.fillOval(point.x, point.y, size, size);
       }
   }
}

Write the code necessary to:

This week you learned how to create a program that runs as a application or an applet. Select a graphical program that you wrote for a previous assignment.

  1. Create the project:
    • Open Eclipse and create a new project named Assign122.
    • Locate the project you wish to convert into an applet. Copy the file(s) in the src folder of that project over to the src folder of the new project. Rename the file with the main()method inside it to Assign122.
    • Make sure your name, date, course, and the correct assignment number are in a comment at the top of the file.
  2. I don't know why I am so nice. Here are a few hints:
    • The main class should extend JApplet. If any of your other classes extend JFrame, they should now extend JApplet
    • Add an init() method to the class that has the main() method within it.
      • Have the init() method create an instance of the JFrame class that now extends JApplet. It will not be the Assign122 class, but rather the other JFrame class. For example, if you are doing the drawing program with a class named DrawingFrame that extends JFrame, the DrawingFrame should now extend aJApplet. That is the class instance you want to create.
      • add the object to the applet
      • Page 389 shows an "if" statement on line 15 being used because they are passing in parameters. None of the programs we have written have parameters passed in, so we don't really need the if.
    • Back in the main, create a JFrame object by calling the default constructor. There is no need to set title, as you probably do that later in the main when you call the setTitle()method (similar to line 39)
    • Create an instance of the applet to add to the JFrame object you created in the previous step (similar to line 42 - using your class name)
    • Add the applet to the frame (similar to line 51)
    • Call the applet's init() and start() methods (similar to line 54 and 55 on page 390)

Explanation / Answer

I'm pasting only the changes.

//.....All import statements remain same

public class Assign72 extends JApplet {
// Create drawing fram
@Override
public void init(){
}
@Override
public void start(){
this.main(null);
}
public void main(String[] args) {
DrawingFrame f = new DrawingFrame();
this.add(f);
f.setSize(600, 500);
f.setVisible(true);
}
//Create radio buttons and group them.
public static class DrawingFrame extends JApplet implements ActionListener {
private static final long serialVersionUID = 1L;

//.. ....All the lines below REMAIN SAME