Colour Cube Faces

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
cms271828 cms271828 is offline Offline Oct 23rd, 2007, 1:13 pm |
0
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.
Quick reply to this message  
Java Syntax
  1. import java.awt.Dimension;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import javax.imageio.ImageIO;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13.  
  14. public class Main extends JFrame
  15. {
  16. private static final long serialVersionUID = 1L;
  17.  
  18. public Main()
  19. {
  20. Drawer drawer=new Drawer();
  21. drawer.setPreferredSize(new Dimension(3*256+2,2*256+1));
  22.  
  23. this.setTitle("Colour Cube Faces - Images saved to C:/Faces folder");
  24. this.setResizable(false);
  25. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. this.getContentPane().add(drawer);
  27. this.pack();
  28. this.setVisible(true);
  29. }
  30.  
  31.  
  32. public static void main(String[] args)
  33. {
  34. new Main();
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. class Drawer extends JPanel
  43. {
  44. private static final long serialVersionUID = 1L;
  45.  
  46. BufferedImage[] buffs=new BufferedImage[6];
  47.  
  48. public Drawer()
  49. {
  50. createBuffs();
  51.  
  52. saveBuffs(new File("C:/Faces"));
  53. }
  54.  
  55.  
  56. public void paintComponent(Graphics g)
  57. {
  58. super.paintComponent(g);
  59. Graphics2D g2=(Graphics2D)g;
  60. g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
  61. RenderingHints.VALUE_STROKE_PURE);
  62.  
  63. g2.drawImage(buffs[0],0,0,null);
  64. g2.drawImage(buffs[1],257,0,null);
  65. g2.drawImage(buffs[2],514,0,null);
  66.  
  67. g2.drawImage(buffs[3],0,257,null);
  68. g2.drawImage(buffs[4],257,257,null);
  69. g2.drawImage(buffs[5],514,257,null);
  70. }
  71.  
  72.  
  73. private void createBuffs()
  74. {
  75. int[][] data=new int[][]{
  76. {0,0,255, 1,0,0, 0,-1,255},
  77. {0,0,0, -1,0,255, 0,-1,255},
  78. {1,0,0, 0,0,0, 0,-1,255},
  79. {-1,0,255, 0,0,255, 0,-1,255},
  80. {1,0,0, 0,-1,255, 0,0,255},
  81. {1,0,0, 0,1,0, 0,0,0}
  82. };
  83.  
  84. for(int lp=0;lp<6;lp++)
  85. {
  86. buffs[lp]=new BufferedImage(256,256,BufferedImage.TYPE_INT_RGB);
  87. Graphics2D g2=buffs[lp].createGraphics();
  88. drawFace(data[lp],g2);
  89. g2.dispose();
  90. }
  91.  
  92. }
  93.  
  94.  
  95. private void drawFace(int[] d,Graphics2D g2)
  96. {
  97. for(int x=0;x<=255;x++) for(int y=0;y<=255;y++)
  98. {
  99. g2.setColor(new Color(d[0]*x+d[1]*y+d[2],
  100. d[3]*x+d[4]*y+d[5],
  101. d[6]*x+d[7]*y+d[8]));
  102. g2.drawLine(x,y,x,y);
  103. }
  104. }
  105.  
  106.  
  107. private void saveBuffs(File dir)
  108. {
  109. // MAKE FOLDER IF DOESNT EXIST
  110. if(!dir.exists()) dir.mkdir();
  111.  
  112. File output;
  113. for(int lp=0;lp<6;lp++)
  114. {
  115. String fileName="pic"+lp+".png";
  116. output=new File(dir.toString()+"/"+fileName);
  117. try
  118. {
  119. ImageIO.write(buffs[lp],"PNG",output);
  120. }
  121. catch(IOException e){}
  122. }
  123. }
  124. }
0
patelmiteshb patelmiteshb is offline Offline | May 20th, 2008
i use this but i have problem in this if you have any help please reply me.

Thanks.
 
0
cms271828 cms271828 is offline Offline | May 20th, 2008
Whats the problem?

It should work fine
 
 

Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC