Ejemplo n.º 1
0
  /** Initializes the painting algorithm in the DataPanel */
  private void initPainter() {
    getContentPane().remove(DataPanel);

    topx1 = 0;
    topx2 = 120;
    bottomx1 = 150;
    bottomx2 = 0;
    timer = new javax.swing.Timer(10, this);
    timer.start();

    DataPanel =
        new JPanel() {
          public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;

            panelLength = getSize().width;
            panelHeight = getSize().height;

            g2d.setStroke(new BasicStroke(10));

            GradientPaint topBarPaint =
                new GradientPaint(topx1, topy1, Color.black, topx2, topy2, Color.blue, true);

            g2d.setPaint(topBarPaint);
            g2d.drawLine(0, 0, panelLength, 0);

            GradientPaint bottomBarPaint =
                new GradientPaint(
                    bottomx1, bottomy1, Color.black, bottomx2, bottomy2, Color.blue, true);

            g2d.setPaint(bottomBarPaint);
            g2d.drawLine(0, panelHeight, panelLength, panelHeight);

            if (drawVerticalLines) {
              GradientPaint verticalBarPaint =
                  new GradientPaint(0, 0, Color.black, 0, panelHeight, Color.lightGray, true);

              g2d.setStroke(new BasicStroke(2));
              g2d.setPaint(verticalBarPaint);
              g2d.drawLine(vertx1, 6, vertx2, panelHeight - 6);
              g2d.drawLine(vert2x1, 6, vert2x2, panelHeight - 6);
            }

            g2d.setPaint(new GradientPaint(50, 0, Color.darkGray, 450, 0, Color.black));
            g2d.setFont(new Font("Verdana", 0, 18));

            double strWidth = g2d.getFontMetrics().getStringBounds(upperMessage, g2d).getWidth();
            g2d.drawString(
                upperMessage,
                (float) (panelLength / 2) - (float) (strWidth / 2),
                (float) panelHeight / 3);

            strWidth = g2d.getFontMetrics().getStringBounds(lowerMessage, g2d).getWidth();
            g2d.drawString(
                lowerMessage,
                (float) (panelLength / 2) - (float) (strWidth / 2),
                (float) panelHeight / (1.5f));
          }
        };

    DataPanel.setMinimumSize(new Dimension(450, 100));
    DataPanel.setPreferredSize(new Dimension(450, 100));
    DataPanel.setMaximumSize(new Dimension(450, 100));

    getContentPane().add(DataPanel);
    this.pack();
    centerOnScreen();
  }