commit b135fb5408e45dbdef413f54465a2083f666a30c Author: sigonasr2 Date: Thu Mar 17 01:01:08 2022 -0500 Setup renderer diff --git a/bin/sig/Panel.class b/bin/sig/Panel.class new file mode 100644 index 0000000..df56113 Binary files /dev/null and b/bin/sig/Panel.class differ diff --git a/bin/sig/PolygonFill.class b/bin/sig/PolygonFill.class new file mode 100644 index 0000000..de7fb2f Binary files /dev/null and b/bin/sig/PolygonFill.class differ diff --git a/src/sig/Panel.java b/src/sig/Panel.java new file mode 100644 index 0000000..b4516dc --- /dev/null +++ b/src/sig/Panel.java @@ -0,0 +1,99 @@ +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 javax.swing.JPanel; + +public class Panel extends JPanel implements Runnable { + public int pixel[]; + public int width=1280; + public int height=720; + private Thread thread; + private Image imageBuffer; + private MemoryImageSource mImageProducer; + private ColorModel cm; + + 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<1280;x++) { + for (int y=0;y<720;y++) { + p[y*1280+x]=(0<<16)+(0<<8)+0; + } + } + + + } + + @Override + public void run() { + while (true) { + // request a JPanel re-drawing + repaint(); + try {Thread.sleep(5);} catch (InterruptedException e) {} + } + } +} \ No newline at end of file diff --git a/src/sig/PolygonFill.java b/src/sig/PolygonFill.java new file mode 100644 index 0000000..0326df5 --- /dev/null +++ b/src/sig/PolygonFill.java @@ -0,0 +1,21 @@ +package sig; + +import javax.swing.JFrame; + +public class PolygonFill { + public static void main(String[] args) { + Panel p = new Panel(); + JFrame f = new JFrame("Polygon Fill Renderer"); + + p.init(); + + f.add(p); + f.setSize(1280,720); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setVisible(true); + + while (true) { + p.render(); + } + } +}