2013-04-04 22 views
1

我需要将来自IP摄像机的视频流传输到Java小程序窗体上,然后在其上绘制矩形并获取四个坐标。我可以通过小应用程序流式传输视频,并且可以绘制多边形,但是可以单独绘制。我想要做的是我需要绘制多边形,而视频流时,多边形应该是半透明的。流式传输视频并在其上绘制多边形Java

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

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

} 

回答

0

哦,孩子......我有很多问题,当我已经混有逻辑油漆...我的老板/客户端必须明确要求在paint方法,这样做的逻辑。

我想建议至少一个LayeredPanel。在层1中使用IP摄像头做东西,并做矩形,无论你想在layer2中做什么。

+0

感谢您的即时回复,我希望第一层和第二层的坐标匹配。因为基于我需要处理视频的矩形。 – Tattu 2013-04-04 10:55:25

+0

@ user865439是的,它们匹配,只需使用LayeredPanel, – 2013-04-04 11:42:03

+0

Thnx ...我会尝试并发布结果... – Tattu 2013-04-04 11:47:28