1 package org.objectweb.jac.aspects.gui.swing; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.image.*; 6 7 9 10 public class NumberPanel extends JComponent implements Runnable 11 { 12 BufferedImage buffer; 13 Graphics2D gc; 14 String text; 15 FontMetrics fm; 16 17 boolean painting = false; 18 boolean needRepaint = false; 19 20 int lineToMark = -1; 21 22 JScrollPane sp; 23 24 Thread paint = null; 25 26 NumberPanel() 27 { 28 setDoubleBuffered(false); 29 setFont(new Font("MonoSpaced", Font.PLAIN, 12)); 30 31 } 32 33 public void setJScrollPane(JScrollPane s) 34 { 35 sp = s; 36 } 37 38 public void run() 39 { 40 repaint(); 41 } 42 43 public void paint(Graphics g) 44 { 45 47 48 if (buffer == null || buffer.getWidth(null) < getSize().width || buffer.getHeight(null) < getSize().height) 49 { 50 buffer = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_3BYTE_BGR); 51 gc = buffer.createGraphics(); 52 53 gc.setFont(new Font("MonoSpaced", Font.PLAIN, 12)); 54 fm = gc.getFontMetrics(); 55 } 56 57 gc.setColor(getBackground()); 59 gc.fillRect(0, 0, getSize().width, getSize().height); 60 gc.setColor(new Color(200, 0, 0)); 61 62 fm = g.getFontMetrics(); 63 92 93 int posY = fm.getHeight(); 94 95 for(int i = 0; posY < getSize().height; i ++) 96 { 97 gc.drawString("" + i, 55 - fm.charsWidth(("" + i).toCharArray(), 0, ("" + i).length()), posY); 98 if (lineToMark == i) 99 { 100 gc.setColor(Color.green); 101 gc.fillPolygon(new int[] 102 { 103 58, 63, 58 104 }, new int[] 105 { 106 posY - (int)(fm.getHeight() / 1.5d), posY - (int)(fm.getHeight() / 3d), posY 107 }, 108 3); 109 110 gc.setColor(new Color(200, 0, 0)); 111 } 112 posY += fm.getHeight(); 113 } 114 ((Graphics2D)g).drawImage(buffer, 0, 0, null); 117 } 118 119 public void scrollToLine(int l) 120 { 121 lineToMark = l; 122 if(fm != null) 123 { 124 int lines = 1; 125 for(int i = 0; i < text.length(); i ++) 126 { 127 if(text.charAt(i) == '\n')lines ++; 128 } 129 130 int posY = fm.getHeight(); 131 132 for(int i = 1; i <= lines && i != l; i ++) 133 { 134 posY += fm.getHeight(); 135 } 136 137 if (sp != null) 138 { 139 System.out.println("Line: " + l); 140 141 if(posY - fm.getHeight() < sp.getVerticalScrollBar().getValue() || posY - fm.getHeight() > sp.getVerticalScrollBar().getValue() + sp.getVerticalScrollBar().getVisibleAmount()) sp.getVerticalScrollBar().setValue(posY - fm.getHeight()); 142 } 143 } 144 repaint(); 145 } 146 147 public void update(String t, FontMetrics f) 148 { 149 if (!text.equals(t)) 151 { 152 text = t; 153 needRepaint = true; 154 repaint(); 155 } 156 161 } 162 163 public void update(Graphics g) 164 { 165 paint(g); 166 } 167 } 168 | Popular Tags |