commit
b135fb5408
Binary file not shown.
Binary file not shown.
@ -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) {} |
||||
} |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue