package designer.components.screen;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JPanel;
import java.awt.Graphics;
import designer.components.Palette;

@SuppressWarnings("serial")
public class EditorScreen extends JPanel
{
	private volatile int imgheight = 100, imgwidth = 100;
	protected volatile boolean mousedown = false;
	public final Palette palette = new Palette(this);
	private int imgtype = BufferedImage.TYPE_4BYTE_ABGR;
	private GridLayout layout;
	private volatile int[] data;
	
	public EditorScreen()
	{
		resizeCanvas();
		this.setGaps(1, Color.GRAY);
	}
	public void repaintCanvas()
	{
		this.getParent().repaint();
		this.setGaps(1, Color.GRAY);
		// TODO?
	}
	
	private final EditorScreen editor = this;
	
	private void resizeCanvas()
	{
		this.removeAll();
		layout = new GridLayout(imgwidth, imgheight);
		this.setLayout(layout);
		System.out.println(imgheight + " " + imgwidth);
		data = new int[imgwidth * imgheight];
		int i = 0;
		for(int ix = 0; ix < imgheight; ++ix)
		{
			for(int iy = 0; iy < imgwidth; ++iy)
			{
				System.out.println(
						ix + " " + iy + " " + imgheight + " " + imgwidth);
				final int ti = i;
				this.add(new ScaleablePixelBox(new Thread()
				{
					public void run()
					{
						java.awt.Component c = editor.getComponent(ti);
						c.setBackground(palette.getSelectedColor());
						editor.repaint();
						data[ti] = palette.getSelectedColor().getRGB();
					}
				}, Color.WHITE, this, false));
				data[i++] = Color.WHITE.getRGB();
			}
		}
		this.revalidate();
	}
	public void newImg(int[] wh)
	{
		imgwidth = wh[0];
		imgheight = wh[1];
		Thread.yield();
		this.resizeCanvas();
	}
	public BufferedImage getData()
	{
		BufferedImage img = new BufferedImage(imgwidth, imgheight, imgtype);
		int i = 0;
		for(int x = 0; x < imgheight; ++x)
		{
			for(int y = 0; y < imgwidth; ++y)
			{
				img.setRGB(x, y, data[i++]);
			}
		}
		return img;
	}
	public void setData(BufferedImage img) throws IOException
	{
		imgwidth = img.getWidth();
		imgheight = img.getHeight();
		data = new int[imgwidth * imgheight];
		int i = 0;
		for(int x = 0; x < imgheight; ++x)
		{
			for(int y = 0; y < imgwidth; ++y)
			{
				data[i++] = img.getRGB(x, y);
			}
		}
		this.resizeCanvas();
		this.repaintCanvas();
	}
	public void setGaps(int gap, Color color)
	{
		layout.setHgap(gap);
		layout.setVgap(gap);
		this.setLayout(layout);
		this.setBackground(color);
		this.revalidate();
	}
}

@@ -177,34 +177,8 @@
 el;%0A
-import java.awt.Graphics;%0A
 impo
@@ -209,16 +209,16 @@
 alette;%0A
+
 %0A@Suppre
@@ -315,19 +315,18 @@
 eight = 
-100
+32
 , imgwid
@@ -334,11 +334,10 @@
 h = 
-100
+32
 ;%0A%09p
@@ -594,146 +594,8 @@
 ();%0A
-%09%09this.setGaps(1, Color.GRAY);%0A%09%7D%0A%09public void repaintCanvas()%0A%09%7B%0A%09%09this.getParent().repaint();%0A%09%09this.setGaps(1, Color.GRAY);%0A%09%09// TODO?%0A
 %09%7D%0A%09
@@ -1409,32 +1409,63 @@
 RGB();%0A%09%09%09%7D%0A%09%09%7D%0A
+%09%09this.setGaps(1, Color.GRAY);%0A
 %09%09this.revalidat
@@ -2148,24 +2148,24 @@
 );%0A%09%09%09%7D%0A%09%09%7D%0A
+
 %09%09this.resiz
@@ -2179,32 +2179,8 @@
 ();%0A
-%09%09this.repaintCanvas();%0A
 %09%7D%0A%09

Fix disappearing grid