Answers for "java 2d rendering"

1

java 2d rendering example

//Java 2D rendering technique

//i know there is too much to explain but i am too lazy to do that atm xd

//enjoy!

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

class Main extends Canvas implements Runnable{
	
	private static final long serialVersionUID = 1L;
	public static final int WIDTH=240;
	public static final int HEIGHT=WIDTH/16*9;
	
	private BufferedImage image = new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_RGB);
	private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
	private int TickRate;
	private static final String NAME = "TEST";
	private boolean running = true;
	private static final int SCALE = 3;
	public void start(){
		running = true;
		new Thread(this).start();
	}
	
	public void stop(){
		
		running = false;
		
	}
	
	public void run(){
		
		while(running){
			engine();
			
		}
		
	}	
		
		
	private void engine() {
	        tick();
		    render();
			
	}
		
		
	public void tick() {
	TickRate++;
	}
	
	
	
    public void render() {
    
    BufferStrategy bs = getBufferStrategy();
    if(bs==null) {
    	createBufferStrategy(3);
    	return;
    }
    
    
    for(int i=0; i<pixels.length; i++) {
    	pixels[i]= i + TickRate;
    	
    
    }
    
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0,getWidth(),getHeight(),null);
    g.dispose();
    bs.show();
		
	}
	
	public static void main(String[] args) {
	Main main = new Main();
	main.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
	main.setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
	main.setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
	
	JFrame frame = new JFrame(main.NAME);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setLayout(new BorderLayout());
	frame.setResizable(false);
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);
	frame.add(main);
	frame.pack();
	main.start();
	
	}
	
	
	}
Posted by: Guest on July-29-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language