Package com.tail

Source Code of com.tail.TextPanel2

package com.tail;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;

public class TextPanel2 extends JComponent {

  private static final long serialVersionUID = 6001973725994078371L;

  private Color BACKGROUND_1 = new Color(226,223,239);
  private Color BACKGROUND_2 = new Color(209,220,231);

  private static final int LINE_HEIGHT=18;
  private static final int SEPARATION_LEFT = 4;
  private static final int SEPARATION_RIGHT = 4;
  private static final int SEPARATION_LINE_SIZE = 1;

  //private Color LINE_NUMBER_TEXT_COLOR = new Color(88,190,91);
  //private Color TEXT_COLOR = new Color(88,190,91).brighter();
  private Color LINE_NUMBER_TEXT_COLOR = Color.GRAY;
  private Color TEXT_COLOR = Color.BLACK;

  private Font font = new Font("Consolas",Font.BOLD, 12);
  private Font fontPlain = new Font("Consolas",Font.PLAIN, 12);

  ArrayList<String> document;

  FileReader reader;

  private Dimension textBounds = new Dimension(0, 0);
  private Point viewPortPoint = new Point(0,0);

  private BufferedImage mainBuffer;
  private BufferedImage workBuffer;



  public TextPanel2() {
    super();
    document = new ArrayList<String>();

    setOpaque(true);
    setBackground(BACKGROUND_1);
    initComponents();
  }

  private BufferedImage getCompatibleImage()
  {
    BufferedImage image = null;
    GraphicsConfiguration gc = getGraphicsConfiguration();
    if (gc != null)
    {
      image = gc.createCompatibleImage(getWidth(), getHeight());
    }
    else
    {
      image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    }

    return image;
  }

  public void addLine(String line){
    document.add(line);
    FontMetrics fm = this.getFontMetrics(font); // or another font
    int strw = fm.stringWidth(String.valueOf(document.size()));
    int newWidth = strw+SEPARATION_RIGHT+SEPARATION_LEFT+SEPARATION_LINE_SIZE + fm.stringWidth(line);
    textBounds = new Dimension(Math.max(newWidth, textBounds.width), document.size()*LINE_HEIGHT);
    this.repaint();
  }


  public void updateLines(int firsLine, List<String> lines){

  }


  public void addLines(ArrayList<String> lines){
    document.addAll(lines);
    FontMetrics fm = this.getFontMetrics(font);
    int strw = fm.stringWidth(String.valueOf(document.size()));
    int finalWidth = 0;
    for (String line : lines){
      int newWidth = strw+SEPARATION_RIGHT+SEPARATION_LEFT+SEPARATION_LINE_SIZE+ fm.stringWidth(line);
      finalWidth = Math.max(finalWidth, newWidth);
    }
    textBounds = new Dimension(Math.max(finalWidth, textBounds.width), document.size()*LINE_HEIGHT);
    this.repaint();
  }

  @Override
  protected void paintComponent(Graphics g) {
    if (mainBuffer != null) {
      Graphics scratchGraphics = (g == null) ? null : g.create();
      scratchGraphics.drawImage(mainBuffer, 0, 0, null);
      scratchGraphics.dispose();
    }
  }

  public void refresh(){
    paintModel();
  }

  private void paintModel(){
    if (workBuffer == null ||
      workBuffer.getWidth() != getWidth() ||
          workBuffer.getHeight() != getHeight()) {
      workBuffer = getCompatibleImage();
    }

    Graphics2D g2 = workBuffer.createGraphics();

    Rectangle rect=g2.getClipBounds();
    g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setRenderingHint(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

    //Fondo blanco
    g2.setColor(Color.WHITE);
    g2.fillRect(rect.x,rect.y,rect.width,rect.height);

    if (document.isEmpty()){
      return;
    }

    int separation = (int)font.getStringBounds(String.valueOf(document.size()), g2.getFontRenderContext()).getWidth();


    int cont=viewPortPoint.y/LINE_HEIGHT;
    int posy=0;

    int tope;
    if (textBounds.height > (viewPortPoint.y+this.getSize().height)){
      tope = this.getSize().height;
    }else{
      tope = textBounds.height;
    }

    //pintamos el pijama
    while(posy <  tope){

      posy+=LINE_HEIGHT;
      g2.setColor(LINE_NUMBER_TEXT_COLOR);
      g2.setFont(font);
      g2.drawString(String.valueOf(cont+1), 0,posy-4);
      //changeColor=!changeColor;

      g2.setFont(fontPlain);

      g2.setColor(TEXT_COLOR);
      int stringSeparation = separation+SEPARATION_RIGHT+SEPARATION_LEFT+SEPARATION_LINE_SIZE;
      g2.drawString(document.get(cont),stringSeparation,posy-4);
      cont++;

    }

    g2.setColor(Color.GRAY);
    g2.fillRect(separation+SEPARATION_LEFT,rect.y,SEPARATION_LINE_SIZE,rect.height);

    g2.dispose();

        // copiamos el buffer de trabajo en el de frente
        if (mainBuffer == null ||
          mainBuffer.getWidth() != getWidth() ||
          mainBuffer.getHeight() != getHeight()) {
          mainBuffer = getCompatibleImage();
        }
        g2 = mainBuffer.createGraphics();
        g2.drawImage(workBuffer, 0, 0, null);

        g2.dispose();

        repaint();
  }

  private void initComponents(){
    this.addMouseWheelListener(new MouseAdapter() {

      @Override
      public void mouseWheelMoved(MouseWheelEvent arg0) {
        System.out.println("qu� pasa t�o");
        Dimension dim = TextPanel2.this.getSize();
        if ((dim.height+viewPortPoint.y)<textBounds.height){
          viewPortPoint.translate(0, arg0.getWheelRotation()*LINE_HEIGHT);
          TextPanel2.this.repaint();
        }

      }



    });

    this.addMouseMotionListener(new MouseMotionListener() {

      @Override
      public void mouseMoved(MouseEvent arg0) {
        System.out.println("me muevo");
        if (!TextPanel2.this.isFocusOwner()){
          TextPanel2.this.requestFocusInWindow();
          System.out.println("me HAGO due�o Y SE�OR DEL FOCO");
        }
      }

      @Override
      public void mouseDragged(MouseEvent arg0) {
        System.out.println("dragueando"+System.currentTimeMillis());

      }
    });

    this.addKeyListener(new KeyListener() {

      @Override
      public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

      }

      @Override
      public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

      }

      @Override
      public void keyPressed(KeyEvent arg0) {
        if (arg0.getKeyCode()==KeyEvent.VK_PAGE_DOWN){
          int y = viewPortPoint.y+TextPanel2.this.getSize().height;
          if (y>textBounds.height){
            y = textBounds.height- TextPanel2.this.getSize().height;
          }
          viewPortPoint.setLocation(0, y);
          TextPanel2.this.repaint();
        }else if(arg0.getKeyCode()==KeyEvent.VK_PAGE_UP){
          int y = viewPortPoint.y-TextPanel2.this.getSize().height;
          if (y<0){
            y = 0;
          }
          viewPortPoint.setLocation(0, y);
          TextPanel2.this.repaint();

        }

      }
    });
}
}
TOP

Related Classes of com.tail.TextPanel2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.