Code:
/*
This applet lets the user draw colored polygons.
The user inputs a polygon by clicking a series of points.
Clicking near the starting point (within 2 pixels) or
right-clicking (or 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. This applet does not keep a copy of the image
on the screen, so it will not reappear if the applet is
covered and then uncovered.
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class SimplePolygons extends Applet implements MouseListener {
/* Variables for implementing polygon input. */
private int[] xCoord, yCoord; // 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;
// Color that is used to draw the polygons.
public void init() {
// Initialize the applet. The applet listens for mouse events.
// Arrays are created to hold the points.
setBackground(Color.white);
addMouseListener(this);
xCoord = new int[500];
yCoord = new int[500];
pointCt = 0;
}
public void paint(Graphics g) {
// The paint() routine does nothing but draw a 1-pixel black
// border around the applet. Polygons drawn on the applet
// are not permanent.
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
} // end paint()
private void putLine(int x1, int y1, int x2, int y2) {
// Draw a line from (x1,y1) to (x2,y2) directly onto the
// applet, without going through the paint() method.
Graphics g = getGraphics();
g.drawLine(x1,y1,x2,y2);
g.dispose();
}
public void mousePressed(MouseEvent evt) {
// Process a user mouse-click.
if (pointCt < 2)
return;
Graphics g = getGraphics();
if (pointCt == 2) {
g.drawLine(xCoord[0], yCoord[0], xCoord[1], yCoord[1]);
}
if (evt.isMetaDown() || pointCt == 500) {
g.setColor(Color.red);
g.fillPolygon(xCoord, yCoord, pointCt);
g.setColor(Color.black);
g.drawPolygon(xCoord, yCoord, pointCt);
}
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.
xCoord[pointCt] = evt.getX();
yCoord[pointCt] = evt.getY();
pointCt++;
if (pointCt >= 2) {
putLine(xCoord[pointCt-2], yCoord[pointCt-2],
xCoord[pointCt-1], yCoord[pointCt-1]);
}
}
} // end mousePressed()
public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
} // end class SimplePolygons
/*
This applet lets the user draw colored polygons.
The user inputs a polygon by clicking a series of points.
Clicking near the starting point (within 2 pixels) or
right-clicking (or 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. This applet does not keep a copy of the image
on the screen, so it will not reappear if the applet is
covered and then uncovered.
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class SimplePolygons extends Applet implements MouseListener {
/* Variables for implementing polygon input. */
private int[] xCoord, yCoord; // 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;
// Color that is used to draw the polygons.
public void init() {
// Initialize the applet. The applet listens for mouse events.
// Arrays are created to hold the points.
setBackground(Color.white);
addMouseListener(this);
xCoord = new int[500];
yCoord = new int[500];
pointCt = 0;
}
public void paint(Graphics g) {
// The paint() routine does nothing but draw a 1-pixel black
// border around the applet. Polygons drawn on the applet
// are not permanent.
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
} // end paint()
private void putLine(int x1, int y1, int x2, int y2) {
// Draw a line from (x1,y1) to (x2,y2) directly onto the
// applet, without going through the paint() method.
Graphics g = getGraphics();
g.drawLine(x1,y1,x2,y2);
g.dispose();
}
public void mousePressed(MouseEvent evt) {
// Process a user mouse-click.
if (pointCt < 2)
return;
Graphics g = getGraphics();
if (pointCt == 2) {
g.drawLine(xCoord[0], yCoord[0], xCoord[1], yCoord[1]);
}
if (evt.isMetaDown() || pointCt == 500) {
g.setColor(Color.red);
g.fillPolygon(xCoord, yCoord, pointCt);
g.setColor(Color.black);
g.drawPolygon(xCoord, yCoord, pointCt);
}
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.
xCoord[pointCt] = evt.getX();
yCoord[pointCt] = evt.getY();
pointCt++;
if (pointCt >= 2) {
putLine(xCoord[pointCt-2], yCoord[pointCt-2],
xCoord[pointCt-1], yCoord[pointCt-1]);
}
}
} // end mousePressed()
public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
} // end class SimplePolygons
Zanima me prvo kako da napravim da program iscrta samo dva poligona.
Benzin vaš - ideje naše