Skip to content

Example Code

sirrandalot edited this page Mar 8, 2018 · 3 revisions

Using this package is simple! Just add the jar to your java project build path and then have at it!

The Screen class extends JPanel, so you can just add it to another swing component. Here's a little example file as well as the result it gives:

import javax.swing.JFrame;

import textScreenEmu.PaletteFactory;
import textScreenEmu.PalettePredef;
import textScreenEmu.Screen;
import textScreenEmu.TilesetFactory;
import textScreenEmu.TilesetPredef;

public class TSEMUTest {
	
	public static void main(String[] args){
		
		//Create a new screen
		Screen s = new Screen(32, 16, 2, 
				TilesetFactory.createTileset(TilesetPredef.CODEPAGE473_12x12), 
				PaletteFactory.createPalette(PalettePredef.C64_16));
		
		//Set up the frame
		JFrame mainFrame = new JFrame("TextScreenEmu Test!!");
		
		mainFrame.add(s);
		mainFrame.setResizable(false);
		mainFrame.pack();
		
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
		
		
		//Colour the entire screen one colour
		s.setBackgroundColour(14);
		s.clearScreen();
		
		//Draw the colour palette
		s.drawPalette(false);
		
		//Draw a bunch of stuff!
		s.setForegroundColour(15);
		
		for(int i = 4; i < 28; i++){
			s.drawTile(i, 2, 176);
			s.drawTile(i, 3, 177);
			s.drawTile(i, 4, 178);
		}
		
		s.setColour(6, 5);
		
		for(int i = 4; i < 28; i++){
			s.drawTile(i, 12, 178);
			s.drawTile(i, 13, 177);
			s.drawTile(i, 14, 176);
		}
		
		
		s.setBackgroundColour(11);
		
		
		int offx = 6;
		int offy = 6;
		
		s.setForegroundColour(3);
		
		s.drawTile(offx + 0, offy + 0, 186);
		s.drawTile(offx + 0, offy + 1, 186);
		s.drawTile(offx + 0, offy + 2, 204);
		s.drawTile(offx + 0, offy + 3, 186);
		s.drawTile(offx + 0, offy + 4, 186);
		
		s.drawTile(offx + 1, offy + 2, 205);
		s.drawTile(offx + 2, offy + 2, 205);
		
		s.drawTile(offx + 3, offy + 0, 186);
		s.drawTile(offx + 3, offy + 1, 186);
		s.drawTile(offx + 3, offy + 2, 185);
		s.drawTile(offx + 3, offy + 3, 186);
		s.drawTile(offx + 3, offy + 4, 186);
		
		
		offx += 5;
		s.setForegroundColour(7);
		
		s.drawTile(offx + 0, offy + 0, 201);
		s.drawTile(offx + 0, offy + 1, 186);
		s.drawTile(offx + 0, offy + 2, 204);
		s.drawTile(offx + 0, offy + 3, 186);
		s.drawTile(offx + 0, offy + 4, 200);
		
		s.drawTile(offx + 1, offy + 0, 205);
		s.drawTile(offx + 1, offy + 2, 205);
		s.drawTile(offx + 1, offy + 4, 205);
		
		s.drawTile(offx + 2, offy + 0, 205);
		s.drawTile(offx + 2, offy + 2, 205);
		s.drawTile(offx + 2, offy + 4, 205);
		
		offx += 4;
		s.setForegroundColour(8);
		
		s.drawTile(offx + 0, offy + 0, 186);
		s.drawTile(offx + 0, offy + 1, 186);
		s.drawTile(offx + 0, offy + 2, 186);
		s.drawTile(offx + 0, offy + 3, 186);
		s.drawTile(offx + 0, offy + 4, 200);
		
		s.drawTile(offx + 1, offy + 4, 205);
		
		s.drawTile(offx + 2, offy + 4, 205);
		
		offx += 4;
		s.setForegroundColour(4);
		
		s.drawTile(offx + 0, offy + 0, 186);
		s.drawTile(offx + 0, offy + 1, 186);
		s.drawTile(offx + 0, offy + 2, 186);
		s.drawTile(offx + 0, offy + 3, 186);
		s.drawTile(offx + 0, offy + 4, 200);
		
		s.drawTile(offx + 1, offy + 4, 205);
		
		s.drawTile(offx + 2, offy + 4, 205);
		
		offx += 4;
		s.setForegroundColour(10);
		
		s.drawTile(offx + 0, offy + 0, 201);
		s.drawTile(offx + 0, offy + 1, 186);
		s.drawTile(offx + 0, offy + 2, 186);
		s.drawTile(offx + 0, offy + 3, 186);
		s.drawTile(offx + 0, offy + 4, 200);
		
		s.drawTile(offx + 1, offy + 0, 205);
		s.drawTile(offx + 1, offy + 4, 205);
		
		s.drawTile(offx + 2, offy + 0, 187);
		s.drawTile(offx + 2, offy + 1, 186);
		s.drawTile(offx + 2, offy + 2, 186);
		s.drawTile(offx + 2, offy + 3, 186);
		s.drawTile(offx + 2, offy + 4, 188);
		
		
		//Repaint the screen
		s.repaint();
	}
}

Here's what that gives:
Example1

What if we switch TilesetPredef.CODEPAGE473_12x12 to TilesetPredef.BASIC_8x8? Example2

Here's what happens if we switch PalettePredef.C64_16 to PalettePredef.GREY_16 without caring how we're indexing our colours! Example3

Clone this wiki locally