Question
In java
PP 8.21 Write a program that creates a polyline shape dynamically using mouse clicks. Each mouse click adds a new line segment from the previous point. Include a button below the drawing area to clear the current polyline and begin another.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
This applet lets the user draw colored polygons.

The user inputs a polygon by clicking a series of points.
Clicking near the starting point i.e. within 2 pixels /
right-clicking/Command-clicking will complete the
polygon, so the user can begin a new one.

Shift-clicking
will clear the screen. Up to 500 points are allowed in a
polygon.
*/


import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class SimplePolygons extends Applet implements MouseListener {
  
  
/* Variables for implementing polygon input. */

private int[] xCoordin, yCoordin; // Arrays containing the points of
// the polygon. Up to 500 points
// are allowed.
  
private int pointCt; // The number of points that have been input.

private final static Color polygonColor = Color.red;

public void init() {
  
setBackground(Color.white);
addMouseListener(this);
xCoordin = new int[500];
yCoordin = new int[500];
pointCt = 0;
}


public void paint(Graphics gs) {


gs.setColor(Color.black);
gs.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() {
// Draw the polygon described by the arrays xCoordin and yCoordin
// and the integer pointCt. A filled polygon with a black
// outline is drawn. If pointCt is 0 or 1, nothing is drawn.
// If pointCt is 2, only a black line is drawn.
if (pointCt < 2)
return;
Graphics g = getGraphics();
if (pointCt == 2) {
g.drawLine(xCoordin[0], yCoordin[0], xCoordin[1], yCoordin[1]);
}
else {
g.setColor(Color.red);
g.fillPolygon(xCoordin, yCoordin, pointCt);
g.setColor(Color.black);
g.drawPolygon(xCoordin, yCoordin, pointCt);
}
g.dispose();
}

public void mousePressed(MouseEvent evts) {
// Process a user mouse-click.

if (evts.isShiftDown()) {
// Clear the applet. (This only requires a repaint.)
// Also, set pointCt to zero to start a new polygon.
pointCt = 0;
repaint();
}
else if ( pointCt > 0 && (Math.abs(xCoordin[0] - evts.getX()) <= 2)
&& (Math.abs(yCoordin[0] - evts.getY()) <= 2) ) {
// User has clicked near the starting point.
// Draw the polygon and reset pointCt so that the
// user can start a new polygon.
putPolygon();
pointCt = 0;
}
else if (evts.isMetaDown() || pointCt == 500) {
// Draw the polygon and reset pointCt so that the
// user can start a new polygon.
putPolygon();
pointCt = 0;
}
else {
// Add the point where the user clicked to the list of
// points in the polygon, and draw a line between the
// previous point and the current point.
xCoordin[pointCt] = evts.getX();
yCoordin[pointCt] = evts.getY();
pointCt++;
if (pointCt >= 2) {
putLine(xCoordin[pointCt-2], yCoordin[pointCt-2],
xCoordin[pointCt-1], yCoordin[pointCt-1]);
}
}
  
} // end mousePressed()

public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }

}

Add a comment
Know the answer?
Add Answer to:
In java Write a program that creates a polyline shape dynamically using mouse clicks. Each mouse...
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
  • This is in Java: A JavaFX application that creates polyline shapes dynamically using mouse clicks. Each...

    This is in Java: A JavaFX application that creates polyline shapes dynamically using mouse clicks. Each mouse click adds a new line segment to the current polyline from the previous point to the current mouse position. Allow the user to end the current polyline with a double click. Also provide a button that clears the window and allows the user to begin again. Please no Jpanel thank you!

  • ##JAVA## about a GUI program (see screenshot below) that permits the user to create, save, and load polygons that are displayed (both hollow and filled) in a GUI. The features and operations required...

    ##JAVA## about a GUI program (see screenshot below) that permits the user to create, save, and load polygons that are displayed (both hollow and filled) in a GUI. The features and operations required by the GUI program include: 1. The program starts with an empty (no vertices) polygon set with a randomly generated color. The drawing area is set to be initially square and should have a black background. ------------------------------------------------------------------------------------------------ 2. The left mouse button lets you begin building a...

  • Write a Java program that does the following: 1. Creates a string array called firstNames that...

    Write a Java program that does the following: 1. Creates a string array called firstNames that holds these values "John", "Frank", "Nick", "Amanda", "Brittany", "Amy", "Deborah", and "Stirling". 2. Creates another string array called lastNames that holds these values "Thompson", "Smith", "Jones", "Keribarian", "Lu", "Banafsheh", "Spielberg", "Jordan", "Castle" and "Simpson". 3. Now add a method that creates an array list called randomNames which picks a random first name from the array in step 1 and a random last name in...

  • Write Java program that takes an integer N from the command line and creates N by...

    Write Java program that takes an integer N from the command line and creates N by N boolean array a[][] such that a[i][j] is true if i and j are relatively prime (have no common factors) and false otherwise. print the output using * to represent true and a space to represent false and include row and column numbers. (use sieving)

  • Write a program in Java that converts the following do-while loop block to: for loop Write,...

    Write a program in Java that converts the following do-while loop block to: for loop Write, compile and run below program segment. Make sure to click on the Output Window to input the number. Evaluate the program. Take notes and comment of below program segments of your observations. int x; Scanner input = new Scanner(System.in);       x = input.nextInt();       do{             System.out.printf(“%d\n”, x * 2);             x++;       } while(x < 100);

  • Write a GUI-based Java application that implements the "Math Game" shown below. The program asks the...

    Write a GUI-based Java application that implements the "Math Game" shown below. The program asks the user to enter the answer of multiplying two one-digit random integers.  If the user enters a correct answer, a message will be displayed at the bottom of the frame, and the text field will be cleared.  If the user enters a wrong answer, a message will be displayed at the bottom of the frame asking for another answer.  If the user...

  • Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks! Write...

    Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate of his/her choice. Display the current number of votes above each button. Include a Finished button that erases the images of the losers and displays only the winner's image with a message of...

  • Java Programing. Using NetBean Write a program to draw a line connecting two circles. These two circles can be dragged by mouse. And when circles are dragged, the line should be adjusted so that it st...

    Java Programing. Using NetBean Write a program to draw a line connecting two circles. These two circles can be dragged by mouse. And when circles are dragged, the line should be adjusted so that it still connect these two circles (binding property)

  • This task is a program framework that you should complete. The program should allow the user...

    This task is a program framework that you should complete. The program should allow the user to move a circular figure with the mouse over a drawing area. The current position of the figure is displayed continuously: Given is the main program: import javafx.scene.Scene; import javafx.application.Application; import javafx.beans.value.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage; public class Main extends Application { private DraggableCircle dc; private Text text; private void updateText() { text.setText("("+dc.getCenterX()+", "+dc.getCenterY()+")"); } @Override public void start(final Stage...

  • Write a Java program which implements both the while and for loop, as needed, to draw...

    Write a Java program which implements both the while and for loop, as needed, to draw the illustrations given below. Shape 1 123456789 12345678 1234567 123456 12345 1234 123 12 1 Shape 2 ************* ************* ************* ************* ************* Shape 3 ============ * * * * * * * * * * ============ Hint: To draw the shapes above, you can use nested for loops to accomplish the task. The outer for loop iterates on each row, and the inner for...

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