流式传输视频并在其上绘制多边形Java

时间:2013-04-04 10:48:05

标签: java video graphics streaming drawing

我需要优先通过Java applet表单从IP cam流式传输视频,然后在其上绘制一个矩形并获得四个坐标。我可以在applet上传输视频,我可以绘制多边形,但是可以单独绘制。我想要做的是我需要在视频流式传输时绘制多边形,多边形应该是半透明的。

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

包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) { }

} 

1 个答案:

答案 0 :(得分:0)

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

我想建议至少LayeredPanel。并在第1层中使用IP摄像头进行操作,并在第2层中执行矩形操作。