| | |
Colour Cube Faces
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
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.
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){} } } }
Similar Threads
- Florida man faces trial for feeding homeless (Geeks' Lounge)
- font colour and background colour (JavaScript / DHTML / AJAX)
- Convicted phisher faces up to 101 years in prison (Geeks' Lounge)
- Java Server Faces help!!! (JSP)
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation banking binary bluetooth character chat class classes client code component constructor crashcourse database design developmenthelp draw eclipse error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide if_statement image input integer interface j2me java javaarraylist javadoc javaee javamicroeditionuseofmotionsensor javaprojects jetbrains jmf jni jpanel julia linux list loop map method methods mobile multithreading netbeans newbie number object oracle pearl print printing problem program programming project recursion remove scanner screen server set size sms socket software sort splash sql stop string swing test textfield thread threads time tree validation windows



