Colour Cube Faces

cms271828 0 Tallied Votes 171 Views Share

This program generates the 6 faces of a colour cube.
The faces are displayed in a JPanel, and they are also saved to C:/Faces as PNG files.
So non-windows users might have to alter code slightly.

import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Main extends JFrame
{
	private static final long serialVersionUID = 1L;

	public Main()
	{
		Drawer drawer=new Drawer();
		drawer.setPreferredSize(new Dimension(3*256+2,2*256+1));
		
		this.setTitle("Colour Cube Faces - Images saved to C:/Faces folder");
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().add(drawer);
		this.pack();
		this.setVisible(true);
	}
	
	
	public static void main(String[] args)
	{
		new Main();
	}
}





class Drawer extends JPanel
{
	private static final long serialVersionUID = 1L;

	BufferedImage[] buffs=new BufferedImage[6];
	
	public Drawer()
	{
		createBuffs();
		
		saveBuffs(new File("C:/Faces"));
	}
	

	public void paintComponent(Graphics g)
	{
	    super.paintComponent(g);
	    Graphics2D g2=(Graphics2D)g;
	    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
	           	            RenderingHints.VALUE_STROKE_PURE);	
        
	    g2.drawImage(buffs[0],0,0,null);
	    g2.drawImage(buffs[1],257,0,null);
	    g2.drawImage(buffs[2],514,0,null);
	    
	    g2.drawImage(buffs[3],0,257,null);
	    g2.drawImage(buffs[4],257,257,null);
	    g2.drawImage(buffs[5],514,257,null);
	}


	private void createBuffs()
	{
		int[][] data=new int[][]{
				{0,0,255,   1,0,0,   0,-1,255},
				{0,0,0,   -1,0,255,   0,-1,255},
				{1,0,0,   0,0,0,      0,-1,255},
				{-1,0,255,   0,0,255,      0,-1,255},
				{1,0,0,   0,-1,255,      0,0,255},
				{1,0,0,   0,1,0,      0,0,0}
		};
		
		for(int lp=0;lp<6;lp++)
		{
		    buffs[lp]=new BufferedImage(256,256,BufferedImage.TYPE_INT_RGB);
		    Graphics2D g2=buffs[lp].createGraphics();
		    drawFace(data[lp],g2);
		    g2.dispose();
		}
		
	}
	
	
	private void drawFace(int[] d,Graphics2D g2)
	{
		for(int x=0;x<=255;x++) for(int y=0;y<=255;y++)
		{
			g2.setColor(new Color(d[0]*x+d[1]*y+d[2],
					              d[3]*x+d[4]*y+d[5],
					              d[6]*x+d[7]*y+d[8]));
			g2.drawLine(x,y,x,y);
		}
	}

	
	private void saveBuffs(File dir)
	{
		// MAKE FOLDER IF DOESNT EXIST
		if(!dir.exists()) dir.mkdir();
	
		File output;
		for(int lp=0;lp<6;lp++)
		{
			String fileName="pic"+lp+".png";
			output=new File(dir.toString()+"/"+fileName);
			try
			{
				ImageIO.write(buffs[lp],"PNG",output);
			}
			catch(IOException e){}
		}
	}
}
patelmiteshb -3 Newbie Poster

i use this but i have problem in this if you have any help please reply me.

Thanks.

cms271828 2 Junior Poster

Whats the problem?

It should work fine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.