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
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