1

我需要最好通过 java applet 形式从 IP cam 流式传输视频,然后在其上绘制一个矩形并获取四个坐标。我可以通过小程序流式传输视频,也可以单独绘制多边形。我想要做的是我需要在视频流式传输时绘制多边形并且多边形应该是半透明的。

这是我用来绘制多边形的代码。

封装 IntelligentCameraApp;

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 int polygonColor = Color.TRANSLUCENT;  
                        // 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();
   }


   private void putPolygon() {
          // Draw the polygon described by the arrays xCoord and yCoord
          // 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(xCoord[0], yCoord[0], xCoord[1], yCoord[1]);
       }
       else {
          //g.setColor(Color.red);
          g.fillPolygon(xCoord, yCoord, pointCt);

          g.drawPolygon(xCoord, yCoord, pointCt);
       }
       g.dispose();
   }


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

      if (evt.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(xCoord[0] - evt.getX()) <= 2)
                          && (Math.abs(yCoord[0] - evt.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 (evt.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.
         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) { }

} 
4

1 回答 1

0

哦,男孩......当我将油漆与逻辑混合时,我遇到了很多问题......我的老板/客户必须明确要求在油漆方法中做逻辑。

我想建议至少一个LayeredPanel。并在第 1 层使用 IP 摄像头做一些事情,并在第 2 层做任何你想要的矩形。

于 2013-04-04T10:52:20.170 回答