Question

"Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import...

"Polygon Drawer
Write an applet that lets the user click on six points. After the sixth point is clicked, the
applet should draw a polygon with a vertex at each point the user clicked."

import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.*;
public class PolygonDrawer extends Applet implements MouseListener{
//Declares variables
   private int[] X, Y;
   private int ptCT;
   private final static Color polygonColor = Color.BLUE;
//Color stuff
   public void init() {
       setBackground(Color.BLACK);
       addMouseListener(this);
       X = new int [450];
       Y = new int [450];
       ptCT = 0;
   }
   public void paint (Graphics g) {
       g.setColor(Color.WHITE);
       g.drawRect(0, 0, getSize().width-1, getSize().height-1);
   }
   private void putLine(int X1, int Y1, int X2, int Y2) {
       Graphics g = getGraphics();
       g.drawLine(X1, Y1, X2, Y2);
       g.dispose();
   }
   private void putPolygon() {
       if(ptCT<2)
           return;
       Graphics g = getGraphics();
       if (ptCT==2) {
           g.drawLine(X[0], Y[0], X[1], Y[1]);
       }
       else {
           g.setColor(Color.BLUE);
           g.fillPolygon(X, Y, ptCT);
           g.setColor(Color.BLACK);
           g.drawPolygon(X,Y,ptCT);
       }
       g.dispose();
   }
   //Mouse clicking
   public void MousePressed(MouseEvent evt) {
       if(evt.isShiftDown()) {
           ptCT = 0;
           repaint();
       }
       else if (ptCT>0&&(Math.abs(X[0]-evt.getX())<=2)&&(Math.abs(Y[0]-evt.getY())<=2)){
           putPolygon();
           ptCT=0;
       }
       else if(evt.isMetaDown()||ptCT==450) {
           putPolygon();
           ptCT=0;
       }
       else {
           X[ptCT]=evt.getX();
           Y[ptCT]=evt.getY();
           ptCT++;
           if(ptCT>=2) {
               putLine(X[ptCT-1],Y[ptCT-2],X[ptCT-1],Y[ptCT-1]);
           }
       }
   }
   public void MouseReleased(MouseEvent evt) {}
   public void MouseClicked(MouseEvent evt) {}
   public void MouseEntered(MouseEvent evt) {}
   public void MouseExited(MouseEvent evt) {}
}

I'm having issues with the "public class PolygonDrawer extends Applet implements MouseListener". How can I fix it?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since your class implements MouseListener it should override every one of the methods in that interface. Even if it won't be used, you need to provide it, so just make it empty:

import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.*;
public class PolygonDrawer extends Applet implements MouseListener{
//Declares variables
   private int[] X, Y;
   private int ptCT;
   private final static Color polygonColor = Color.BLUE;
//Color stuff
   public void init() {
       setBackground(Color.BLACK);
       addMouseListener(this);
       X = new int [450];
       Y = new int [450];
       ptCT = 0;
   }
   public void paint (Graphics g) {
       g.setColor(Color.WHITE);
       g.drawRect(0, 0, getSize().width-1, getSize().height-1);
   }
   private void putLine(int X1, int Y1, int X2, int Y2) {
       Graphics g = getGraphics();
       g.drawLine(X1, Y1, X2, Y2);
       g.dispose();
   }
   private void putPolygon() {
       if(ptCT<2)
           return;
       Graphics g = getGraphics();
       if (ptCT==2) {
           g.drawLine(X[0], Y[0], X[1], Y[1]);
       }
       else {
           g.setColor(Color.BLUE);
           g.fillPolygon(X, Y, ptCT);
           g.setColor(Color.BLACK);
           g.drawPolygon(X,Y,ptCT);
       }
       g.dispose();
   }
   //Mouse clicking
   public void mousePressed(MouseEvent evt) {
       if(evt.isShiftDown()) {
           ptCT = 0;
           repaint();
       }
       else if (ptCT>0&&(Math.abs(X[0]-evt.getX())<=2)&&(Math.abs(Y[0]-evt.getY())<=2)){
           putPolygon();
           ptCT=0;
       }
       else if(evt.isMetaDown()||ptCT==450) {
           putPolygon();
           ptCT=0;
       }
       else {
           X[ptCT]=evt.getX();
           Y[ptCT]=evt.getY();
           ptCT++;
           if(ptCT>=2) {
               putLine(X[ptCT-1],Y[ptCT-2],X[ptCT-1],Y[ptCT-1]);
           }
       }
   }
   public void mouseReleased(MouseEvent evt) {}
   public void mouseClicked(MouseEvent evt) {}
   public void mouseEntered(MouseEvent evt) {}
   public void mouseExited(MouseEvent evt) {}
}

i highlighted the issue. it's a case-sensitive issue. function name should start with small letter.

check this url: https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html

Add a comment
Know the answer?
Add Answer to:
"Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The assignment is to take the current program that draws a line and to add 4...

    The assignment is to take the current program that draws a line and to add 4 buttons at the bottom of the frame that will change the line drawn to a different color (ie. No button pressed draws a black line, Button labeled Red changes any new lines drawn to red and retains any previously colored lines, etc for all the buttons). This is the code I have so far but I'm having issues with the buttons. This is also...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import...

    please help me to creating UML class diagrams. Snake.java package graphichal2; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Random; enum Dir{L, U, R, D}; class Food { public static int FOODSTYLE = 1; private int m = r.nextInt(Yard.WIDTH / Yard.B_WIDTH); private int n = r.nextInt(Yard.HEIGHT / Yard.B_WIDTH - 30/Yard.B_WIDTH) + 30/Yard.B_WIDTH;// 竖格 public static Random r = new Random(); public int getM() { return m; } public void setM(int m)...

  • ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...

    ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions...

  • java ****Please fix this tic tac toe coding error *****following is the error I've found difficult...

    java ****Please fix this tic tac toe coding error *****following is the error I've found difficult for me to fix. 1. Before I click the board, the grid doesn’t show up at all 2. It is not an automatic system.(if I click, it doesn’t move to next move) 3. if you click a button twice it shows o, x both 4. it is hard to close (you have to close the instruction) 5. instruction keep pops up (start->if you lose...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • In Pl you will create an interface, Drawable, and an abstract class Polygon. The Point class...

    In Pl you will create an interface, Drawable, and an abstract class Polygon. The Point class is provided. Briefly go over the JavaDocs comments to see what is available to you. «interface Drawable +printToConsole(): void Polygon - points : List<Point> - colour : String Point + Polygon(String colour) + getPoints(): List<Point> + addPoint(Point p): void + equals(Object other): boolean +getArea(): double • The Polygon constructor initializes the List to a List collection of your choosing • add Point: Adds a...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • Keep the ball in bounds by adding conditional statements after the TODO comments in the paintComponent()...

    Keep the ball in bounds by adding conditional statements after the TODO comments in the paintComponent() method to check when the ball has hit the edge of the window. When the ball reaches an edge, you will need to reverse the corresponding delta direction and position the ball back in bounds. Make sure that your solution still works when you resize the window. Your bounds checking should not depend on a specific window size This is the java code that...

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT