package sig; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.ColorModel; import java.awt.image.MemoryImageSource; import java.util.ArrayList; import java.util.List; import javax.swing.JPanel; public class Panel extends JPanel implements Runnable { public int pixel[]; public int width=1280; public int height=720; final int CIRCLE_PRECISION=32; private Thread thread; private Image imageBuffer; private MemoryImageSource mImageProducer; private ColorModel cm; int scanLine=0; int nextScanLine=0; double x_offset=0; double y_offset=0; int frameCount=0; public Panel() { super(true); thread = new Thread(this, "MyPanel Thread"); } /** * Get Best Color model available for current screen. * @return color model */ protected static ColorModel getCompatibleColorModel(){ GraphicsConfiguration gfx_config = GraphicsEnvironment. getLocalGraphicsEnvironment().getDefaultScreenDevice(). getDefaultConfiguration(); return gfx_config.getColorModel(); } /** * Call it after been visible and after resizes. */ public void init(){ cm = getCompatibleColorModel(); int screenSize = width * height; if(pixel == null || pixel.length < screenSize){ pixel = new int[screenSize]; } if(thread.isInterrupted() || !thread.isAlive()){ thread.start(); } mImageProducer = new MemoryImageSource(width, height, cm, pixel,0, width); mImageProducer.setAnimated(true); mImageProducer.setFullBufferUpdates(true); imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); // perform draws on pixels render(); // ask ImageProducer to update image mImageProducer.newPixels(); // draw it on panel g.drawImage(this.imageBuffer, 0, 0, this); } /** * Overrides ImageObserver.imageUpdate. * Always return true, assuming that imageBuffer is ready to go when called */ @Override public boolean imageUpdate(Image image, int a, int b, int c, int d, int e) { return true; } /** * Do your draws in here !! * pixel is your canvas! */ public /* abstract */ void render(){ int[] p = pixel; // this avoid crash when resizing //a=h/w for (int x=0;x edges_sorted = new ArrayList(); for (int i=0;i=edges[i].min_y) { edges_sorted.add(j,edges[i]); inserted=true; break; } } if (!inserted) { edges_sorted.add(edges[i]); } } } } //System.out.println(edges_sorted); List active_edges = new ArrayList(); scanLine = edges_sorted.get(0).min_y-1; nextScanLine = scanLine+1; do { for (int i=0;i=0) { Draw(p,index,col.getColor()); } } } List new_active_edges = new ArrayList(); for (int i=0;ie.x_of_min_y) { new_active_edges.add(j,e); inserted=true; break; } } if (!inserted) { new_active_edges.add(e); } } active_edges=new_active_edges; GetNextScanLineEdges(edges_sorted, active_edges); } while (active_edges.size()>0); } private void GetNextScanLineEdges(List edges_sorted, List active_edges) { if (scanLine==nextScanLine) { for (int i=0;iscanLine) { nextScanLine=e.min_y; break; } } } } public void Draw(int[] canvas,int index, int col) { int alpha = col>>>24; if (alpha==0) { return;} else if (alpha==255) { canvas[index]=col; } else { float ratio=alpha/255f; int prev_col=canvas[index]; int prev_r=(prev_col&0xFF); int prev_g=(prev_col&0xFF00)>>>8; int prev_b=(prev_col&0xFF0000)>>>16; int r=(col&0xFF); int g=(col&0xFF00)>>>8; int b=(col&0xFF0000)>>>16; int new_r=(int)(ratio*r+(1-ratio)*prev_r); int new_g=(int)(ratio*g+(1-ratio)*prev_g); int new_b=(int)(ratio*b+(1-ratio)*prev_b); canvas[index]=new_r+(new_g<<8)+(new_b<<16)+(col&0xFF000000); } } @Override public void run() { while (true) { // request a JPanel re-drawing repaint(); //System.out.println("Repaint "+frameCount++); //try {Thread.sleep(1);} catch (InterruptedException e) {} } } }