JAVA - Raspberry-pi - 应用程序即服务启动然后立即停止

时间:2015-10-16 19:56:53

标签: java service raspberry-pi2

我需要在raspberry的创业公司启动一个Java应用程序, 但我无法想象如何?!

我在main的第一行和addShutdownHook()中播放声音。 所以我可以听到每次,应用程序开始然后立即停止...

当我在CLI中启动它时,我没有这个问题! :@

1 个答案:

答案 0 :(得分:0)

问题解决了,

我发布了我发现的,如果有人有相同的内容;)

我的应用程序运行一个线程,等待键盘条目运行实时测试并在此循环中断时关闭应用程序' q',

package event;


import interfaces.ComparableShapes;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import model.ModelOne;
// import model.ModelOne.ActionEnum;
import shapes.*;
// import ui.applet.*;
// import java.awt.Graphics;

public class ShapeMouseHandler extends MouseAdapter 
{
    private ModelOne ntcModel;
    // Need Variables for the upper left corner of a shape
    private int upLtX;
    private int upLtY;
    // Create variables for the move operation
    private int moveStartX;
    private int moveStartY;
    // These were added in version 2 rewrite to replace SelectPoint objects
    private int resizeStartX;
    private int resizeStartY;

    private int currentClickCount; // This holds polygon x and y array index
    // private int selectedXcoord; // added for remove, move, change functionality
    // private int selectedYcoord; // added for remove, move, change functionality
    private Shape currentShape; // for now I will hold the current shape here

    private boolean dragInProgress; // On when a Drag is in progress


    // All SelectPoints to be decomissioned
    // private SelectPoint currentSelectPoint; // Holds the last point selected by mouse click
    // private SelectPoint currentMoveStart; // Holds the starting point of a move function
    // private SelectPoint currentMoveEnd; // Holds the ending point of a move function

    // Next is the constructor associates a model to the listener
    public ShapeMouseHandler(ModelOne mouseModel)
    {
        // Linking the passed in model to this model
        this.ntcModel = mouseModel;
    }

    // This is the MouseAdapter MousePressed Method - see if mouse is clicked
    public void mousePressed(MouseEvent mousePressEvent)
    {
        /* I am switching all mouse events to work with the switch statement based on
         *  the selected action that will give me independent functionality control for each
         */
         // Determine which shape is currently selected
         currentShape = ntcModel.getSelectedShape(mousePressEvent.getX(), mousePressEvent.getY());

          switch (ntcModel.getActionEnum())
          {
          case DRAW:  // action = Draw
             // Establish the upper left corner of the current shape
             upLtX = mousePressEvent.getX();
             upLtY = mousePressEvent.getY();
             // Ask the model to create a starter shape
             currentShape = ntcModel.createShape();
             // proceed if a shape has been created - not sure how it would not be  
             if (currentShape != null)
             {
                 // Set upper left coordinates to location of the mouse
                 currentShape.setXorigin(mousePressEvent.getX());
                 currentShape.setYorigin(mousePressEvent.getY());
                 // Now we need to set the Width and Height parameters
                 // If the user does not drag the mouse
                 if (currentShape instanceof Oval)
                 {
                     ((Oval) currentShape).setBoundBoxWidth(50);
                     ((Oval) currentShape).setBoundBoxHeight(50);
                 }
             }
             // Tell the model to repaint the applet or Application
             ntcModel.repaint();
           break; // break for case Action = 0 (Draw)

           case MOVE: // case for case Action = 1 (Move)
              // Establish location of currentShape
              // currentShape = ntcModel.getSelectedShape(mousePressEvent.getX(), mousePressEvent.getY());
              // Establish the start point for the object move
                if(currentShape == null)
                {
                    // Do nothing for now - later add a message to user "Remove What"
                    System.out.println("\nYou have asked to move a shape but there are no shapes drawn");
                }
                else
                {
                  if (dragInProgress == false)
                  {
                     // Establish a baseline for a move operation to be performed in drag event
                     moveStartX = mousePressEvent.getX();
                     moveStartY = mousePressEvent.getY();
                  } // close dragInProgress condition
                }
           break; // break for case Action = 1 (Move)

           case RESIZE: // case for case Action = 2 (Resize)
                // Establish location of currentShape
                // currentShape = ntcModel.getSelectedShape(mousePressEvent.getX(), mousePressEvent.getY());
                // Establish the start point for the object resize
                if(currentShape == null)
                {
                    // Do nothing for now - later add a message to user "Remove What"
                    System.out.println("\nYou have asked to resize a shape but there are no shapes drawn");
                }
                else
                {
                    // Establish a baseline for a resize operation to be performed in drag event
                    resizeStartX = mousePressEvent.getX();
                    resizeStartY = mousePressEvent.getY();
                }
              // currentShape = ntcModel.getSelectedShape(mousePressEvent.getX(), mousePressEvent.getY());

           break; // break for case Action = 2 (Resize)
           case REMOVE: // case for case Action = 3 (Remove)
                ntcModel.removeShapesToCompare(((ComparableShapes)currentShape)); // Added to overcome Concurrent Modification Error
                // tell the applet to redraw the shapes
                ntcModel.repaint();

           break; // break for case Action = 3 (Remove)
           case CHANGE: // case for case Action = 4 (Change)

               // System.out.println("\n***Debug - The Change Option is selected");
               /* There are 4 conditions to address:
                *  1) If there is no shape I cannot change a shape
                *  2) If we are not in Multi Shape mode, I change the only shape present, Will still use mouse to initiate change
                *  3) In Multi shape mode I need to determine what shape is selected by mouse click listener
                *  3a) only one shape identified as selected, change the shape selected
                *  3b) more than one shape selected - for now change them all
                *  The basis of the change functionality is to take the target shape and change it's 
                *  Shape definition: Triangle, Pentagon etc. , line color and fill color based on the UI
                *  Ok so it took a 5 mile run for me to realize I cannot take a class triangle and turn it
                *  into a pentagon. So we need to retrieve the shape data, delete the old shape,
                *  create a new shape and give it the shape data from the last shape.
                */

                // Establish location of currentShape
                // currentShape = ntcModel.getSelectedShape(mousePressEvent.getX(), mousePressEvent.getY());
                if(currentShape == null)
                {
                    // Do nothing for now - later add a message to user "Change What"
                    System.out.println("\nYou have asked to change a shape but there are no shapes drawn");
                }
                else if (currentShape != null)
                {
                       // get the current mouse coordinates
                       // compare current mouse coordinates with shapes in Vector
                    Vector <ComparableShapes> shapeVector = ntcModel.getShapesToCompare();
                    // Get the data for the current shape so it can be used as a baseline for the new shape 
                    int currentXOrigin = ((Shape)(currentShape)).getXorigin();
                    int currentYOrigin = ((Shape)(currentShape)).getYorigin();
                    int currentWidth = ((BoundRectShapes)(currentShape)).getBoundBoxWidth();
                    int currentHeight = ((BoundRectShapes)(currentShape)).getBoundBoxHeight();
                    // Remove the shape being changed
                    shapeVector.remove(currentShape);
                    // Create a new shape where the old shape was just sitting
                    ntcModel.createShape(); 
                    currentShape = ntcModel.getCurrentShape();
                    currentShape.setXorigin(currentXOrigin);
                    currentShape.setYorigin(currentYOrigin);
                    ((BoundRectShapes)(currentShape)).setBoundBoxWidth(currentWidth);
                    ((BoundRectShapes)(currentShape)).setBoundBoxHeight(currentHeight);
                    currentShape.setSelect(true);

                    shapeVector.add((ComparableShapes)(currentShape));

                }// end of if current shape not null
                // tell the applet to redraw the shapes
                ntcModel.repaint();

           break; // break for case Action = 4 (Change)
           default:
           break;

          } // end of switch statement
    }

    // This is the MouseAdapter mouseDragged method - see if mouse is dragged
    public void mouseDragged(MouseEvent dragEvent)
    {
        /* I am switching all mouse events to work with the switch statement based on
         *  the selected action that will give me independent functionality control for each
         */

          // This is an attempt to fix the grometric drag problem
          dragInProgress = true; // This will allow me to turn off mousePressed while drag in running

          switch (ntcModel.getActionEnum())
          {
          case DRAW:  // action = Draw
              //currentShape = ntcModel.getCurrentShape();
              // proceed if a shape has been created - not sure how it would not be 
              if (currentShape != null)
              {
                  // Check to see if we are in DRAW mode.
                  // if (ntcModel.getAction() == ModelOne.DRAW)
                  if (ntcModel.getActionEnum() == ModelOne.ActionEnum.DRAW)
                  {
                      // Set upper left coordinates to location of the mouse
                      // There is a note in the code about this allowing "rubber-banding"
                      currentShape.setXorigin(Math.min(upLtX, dragEvent.getX()));
                      currentShape.setYorigin(Math.min(upLtY, dragEvent.getY()));
                  }
                  // Now we need to set the Width and Height parameters
                  // If the user does not drag the mouse
                  if (currentShape instanceof Oval)
                  {
                      // Set ending point by mouse drag coordinates
                      // So here we are casting the variable currentShape into a OriginBasedShapes type
                      // object so we can use the OriginBasedShapes setter Methods "setXwidth and setXheight"
                      ((Oval) currentShape).setBoundBoxWidth(Math.abs(upLtX - dragEvent.getX()));
                      ((Oval) currentShape).setBoundBoxHeight(Math.abs(upLtY - dragEvent.getY()));
                  }
                  else if (currentShape instanceof BoundRectShapes)
                  {
                      // Set ending point by mouse drag coordinates
                      // So here we are casting the variable currentShape into a OriginBasedShapes type
                      // object so we can use the OriginBasedShapes setter Methods "setXwidth and setXheight"
                      // ((BoundRectShapes) currentShape).setXwidth(Math.abs(upLtX - dragEvent.getX()));
                      // ((BoundRectShapes) currentShape).setYheight(Math.abs(upLtY - dragEvent.getY()));
                      ((BoundRectShapes) currentShape).setBoundBoxWidth(dragEvent.getX());
                      ((BoundRectShapes) currentShape).setBoundBoxHeight(dragEvent.getY());
                  }
                  if (currentShape instanceof ArrayBasedShapes)
                  {
                      // Set ending point by mouse drag coordinates
                      // So here we are casting the variable currentShape into a ArrayBasedShapes type
                      // object so we can use the ArrayBasedShapes setter Methods "setXnext and setYnext"
                      // The idea here is we are loading a set of array click points one for x values
                      // and the other for y values
                      ((ArrayBasedShapes) currentShape).setXcoords(Math.abs(upLtX - dragEvent.getX()), currentClickCount );
                      ((ArrayBasedShapes) currentShape).setYcoords(Math.abs(upLtY - dragEvent.getY()), currentClickCount );
                  }
              }
              // Tell the model to repaint the applet or application
              ntcModel.repaint();
           break; // break for case Action = 0 (Draw)
           case MOVE: // case for case Action = 1 (Move)
               /* Concept - click the mouse somewhere in a shape and drag it to a new location. I need to
                * Capture the Mouse pressed event when the move box is selected
                * Capture the mouse dragged end location when the move box is selected
                * replace the starting coordinates (xStart and yStart) by adding (or subtracting) the difference
                * between the mousePressed event and the mouseDragged event
                * The solution will require logic that can handle all combinations of 3 x/y positions
                * 1) the Current starting x/y position of the object
                * 2) the Mouse pressed position (which can only be greater than 1) above
                * 3) the mouse dragged to position
                * In addition there are 2 scenarios:
                * 1) No shape present - nothing to move
                * 2) Multi shape mode - deal with shape in a vector
                *  After first attempt it looks like I have to retrieve the width and height and reset them
                *  on a move otherwise the endpoint does not move.
                */
                // System.out.println("\n***Debug - The Move Option is selected");
                if(currentShape == null)
                {
                    // Do nothing for now - later add a message to user "Remove What"
                    System.out.println("\nYou have asked to move a shape but there are no shapes drawn");
                }
                else if (currentShape != null)

                {
                    // Get the starting X and Y position of the selected shape
                    int shapeXPosition = ((Shape)(currentShape)).getXorigin();
                    int shapeYPosition = ((Shape)(currentShape)).getYorigin();
                    // Get the width and height of the current shape so we can reuse it in the new location
                    int shapeWidth = ((BoundRectShapes)(currentShape)).getBoundBoxWidth();
                    int shapeHeight = ((BoundRectShapes)(currentShape)).getBoundBoxHeight();
                    // Get the end point for the move from the drag event
                    int moveEndX = dragEvent.getX();
                    int moveEndY = dragEvent.getY();
                    // Reposition the selected shape based on the difference between the mousePressed
                    // and mouseDragged events
                    ((Shape)(currentShape)).setXorigin(shapeXPosition + (moveEndX - moveStartX)); 
                    ((Shape)(currentShape)).setYorigin(shapeYPosition + (moveEndY - moveStartY)); 
                    System.out.println("*** Debug: StartX, EndX, StartY, EndY "+ moveStartX + " " + moveEndX + " " + moveStartY + " " + moveEndY);
                    // These next 2 lines do not work for the oval
                    if (currentShape instanceof Oval)
                    {
                        ((BoundRectShapes)(currentShape)).setBoundBoxWidth(shapeWidth);
                        ((BoundRectShapes)(currentShape)).setBoundBoxHeight(shapeHeight);
                    }
                    else
                    {
                        ((BoundRectShapes)(currentShape)).setBoundBoxWidth(shapeWidth + (moveEndX - moveStartX));
                        ((BoundRectShapes)(currentShape)).setBoundBoxHeight(shapeHeight + (moveEndY - moveStartY));
                    }
                }
                // tell the applet to redraw the shapes
                ntcModel.repaint();

           break; // break for case Action = 1 (Move)

           case RESIZE: // case for case Action = 2 (Resize)

              // This is the code transplanted from GUIDemo
               /* Concept - click the mouse somewhere in a shape and drag to change it's size. I need to
                * Capture the Mouse pressed event when the resize box is selected
                * Capture the mouse dragged end location when the resize box is selected
                * replace the width and height coordinates (xSize and ySize) by adding (or subtracting) the difference
                * between the mousePressed event and the mouseDragged event
                * The solution will require logic that can handle all combinations of 3 x/y positions
                * 1) the Current starting x/y position of the object
                * 2) the Mouse pressed position (which can only be greater than 1) above
                * 3) the mouse dragged to position
                * In addition there are 2 scenarios:
                * 1) No shape present - nothing to Resize
                */
               // System.out.println("\n***Debug - The Resize Option is selected");
                if(currentShape == null)
                {
                    // Do nothing for now - later add a message to user "Remove What"
                    System.out.println("\nYou have asked to resize a shape but there are no shapes drawn");
                }
                else if (currentShape != null)
                {
                    // Look for dragpoint 
                    // If the shape is selected change it's Width and Height
                    // Get the current width and Height of the selected shape
                    int shapeXPosition = ((BoundRectShapes)(currentShape)).getBoundBoxWidth();
                    int shapeYPosition = ((BoundRectShapes)(currentShape)).getBoundBoxHeight();
                    // Get the ending x an y position after the drag
                    int resizeEndX = dragEvent.getX();
                    int resizeEndY = dragEvent.getY();
                    // Reset the selected shapes width and Height based on the difference between the 
                    // start (MousePressedEvent) and end (mouseDraggedEvent) of the drag
                    ((BoundRectShapes)(currentShape)).setBoundBoxWidth(shapeXPosition + (resizeEndX - resizeStartX)); 
                    ((BoundRectShapes)(currentShape)).setBoundBoxHeight(shapeYPosition + (resizeEndY - resizeStartY)); 

                }
                // tell the applet to redraw the shapes
                ntcModel.repaint();
           break; // break for case Action = 2 (Resize)
           case REMOVE: // case for case Action = 3 (Remove)
           break; // break for case Action = 3 (Remove)
           case CHANGE: // case for case Action = 4 (Change)
           break; // break for case Action = 4 (Change)
           default:
           break;

          } // end of switch statement
          dragInProgress = false; // Allow mouse pressed functionality to run again
    } // end mouseDragged method

    // I added this because I need to chain clicks for an abstract polygon "ArrayBasedShape" and to do
    // this I need the MouseEvent method getClickCount().
    // This is the MouseAdapter MousePressed Method - see if mouse is clicked
    //public void mouseReleased(MouseEvent releaseEvent)
    //{
     //   currentShape.getLineColor();
        // In order to implement fill I will have to test for each individual shape type
        //currentShape.getFillColor();
    //}
}

&#34;此方法可能在等待输入扫描时阻塞&#34;说JavaDoc,...可能会阻止......

如果是服务,或者您将应用程序置于后台,&#34;有下一个&#34;不要阻止,所以我的应用认为&#39; q&#39;是发送和关闭。

我刚刚添加了一个使用app asService的启动参数,它禁用或不禁用此等待循环..!

那是所有人!