Guest User

Untitled

a guest
May 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  1. import java.lang.Math;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import java.awt.geom.*;
  6. import java.io.*;
  7. import java.util.Random;
  8. import javax.imageio.*;
  9. import javax.swing.*;
  10.  
  11. public class HW04a
  12. {
  13. private static final int WIDTH = 400;
  14. private static final int HEIGHT = 400;
  15.  
  16. public static void main(String[] args)
  17. {
  18. JFrame frame = new ImageFrame (WIDTH, HEIGHT);
  19. frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  20. frame.setVisible(true);
  21.  
  22. }
  23. }
  24.  
  25. class ImageFrame extends JFrame
  26. {
  27. private final JFileChooser chooser;
  28. private BufferedImage image;
  29. private Line2D.Double line;
  30.  
  31. public ImageFrame ( int width, int height) //is sent (400, 400)
  32. {
  33. this.setTitle ("CAP3027 2008-HW04a-Yangyang Liu");
  34. this.setSize (width, height);
  35.  
  36. //add a menu to the frame
  37. addMenu();
  38.  
  39. //setup the file chooser dialog
  40.  
  41. chooser = new JFileChooser();
  42. chooser.setCurrentDirectory( new File("."));
  43. }
  44.  
  45. private void addMenu()
  46. {
  47. //setup the frame's menu bar
  48. // === File menu
  49. JMenu fileMenu = new JMenu("File");
  50.  
  51. //---open
  52.  
  53. JMenuItem option = new JMenuItem("Directed Random Walk Plant");
  54. option.addActionListener(new ActionListener()
  55. {
  56. public void actionPerformed(ActionEvent event)
  57. {
  58.  
  59. int i = 0, j = 0;
  60.  
  61. String input1 = JOptionPane.showInputDialog("Enter height: ");
  62. //take the user input as the dimension of the square
  63. int height = Integer.parseInt(input1);
  64.  
  65. String input2 = JOptionPane.showInputDialog("How many stems?");
  66. int stem = Integer.parseInt(input2);
  67.  
  68. String input3 = JOptionPane.showInputDialog("How many steps?");
  69. int step = Integer.parseInt(input3);
  70.  
  71. String input4 = JOptionPane.showInputDialog("Transmission Probability?");
  72. double alpha = Double.parseDouble(input4);
  73.  
  74. String input5 = JOptionPane.showInputDialog("Maximum Rotation Increment?");
  75. double rotation = Double.parseDouble(input5);
  76.  
  77. String input6 = JOptionPane.showInputDialog("Growth Segment Increment?");
  78. double growth = Double.parseDouble(input6);
  79.  
  80. String input7 = JOptionPane.showInputDialog("Starting Color?");
  81. int sColor = (int)Long.parseLong(input7,16);
  82.  
  83. String input8 = JOptionPane.showInputDialog("Ending Color?");
  84. int eColor = (int)Long.parseLong(input8,16);
  85.  
  86. image = new BufferedImage(height+1, height+1,
  87. BufferedImage.TYPE_INT_ARGB);
  88.  
  89. Graphics2D g2d = (Graphics2D)image.createGraphics();
  90.  
  91. //Specify antialiased lines are preferred.
  92. RenderingHints hint =
  93. new RenderingHints( RenderingHints.KEY_TEXT_ANTIALIASING,
  94. RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  95.  
  96. g2d.setRenderingHints( hint );
  97.  
  98. //display BufferedImage
  99. displayBufferedImage(image);
  100.  
  101. //set background white by setting "pen" color white
  102. g2d.setColor(Color.BLACK);
  103. g2d.fillRect(0,0, height, height);
  104.  
  105. Random rnd = new Random();
  106. double beta = 1.0- alpha; //relection probability [0.0, 1.0]
  107. double tau =0.0; //the virtual coin's bias
  108. double r = 1.0; //growth segment length
  109. int[] stemColor = new int[step-1];//for color interpolation
  110. BasicStroke[] sWidth = new BasicStroke[step-1];//for stroke width interpolation
  111.  
  112. //randomly generating each particle's starting coordinates
  113. for(i = 0; i < stem; i++)
  114. {
  115.  
  116. double theta = Math.PI/2; //start growing "upwards"
  117. double direction = -1; //direction to turn (-1 = left| +1 = right)
  118. double x = height/2; //grow from point (x, y)
  119. double y = height/2;
  120. double newX = 0.0;
  121. double newY = 0.0;
  122. //extracting colors
  123. int sAlpha = (sColor & 0xFF000000)>>24;
  124. int sRed = (sColor & 0x00FF0000)>>16;
  125. int sGreen = (sColor & 0x0000FF00)>>8;
  126. int sBlue = sColor & 0x000000FF;
  127. int eAlpha = (eColor & 0xFF000000)>>24;
  128. int eRed = (eColor & 0x00FF0000)>>16;
  129. int eGreen = (eColor & 0x0000FF00)>>8;
  130. int eBlue = eColor & 0x000000FF;
  131. //color interpolation
  132. int deltaR = (eRed-sRed)/step;
  133. int deltaG = (eGreen - sGreen)/step;
  134. int deltaB = (eBlue - sBlue)/step;
  135. //synthesize color
  136. int color;
  137. //width interpolation
  138. float width = 6.0f;
  139. float deltaWidth = (6.0f-0.5f)/step;
  140. //creating a line object
  141. line = new Line2D.Double();
  142.  
  143. //determine each stem's growth pattern
  144. for(j = 0; j < step; j++)
  145. {
  146. if(direction == -1) tau = alpha;
  147. else tau = beta;
  148.  
  149. //flip the biased coin to determine which
  150. //direction to turn next
  151. double bias = rnd.nextDouble();
  152.  
  153. if(rnd.nextDouble() > tau) direction = 1;
  154. else direction = -1;
  155.  
  156. //compute offset from end of the old growth
  157. //segment to the end of the new one
  158. r += growth;
  159. theta += (rotation*bias*direction);
  160.  
  161. newX = x+(r*Math.cos(theta));
  162. newY = y-(r*Math.sin(theta));
  163.  
  164. //set stem color by step
  165. color = sAlpha<<24|sRed<<16|sGreen<<8|sBlue;
  166. stemColor[i] = color;
  167. g2d.setColor(new Color(stemColor[i], false));
  168. sAlpha = (stemColor[i] & 0xFF000000)>>24;
  169. sRed = (stemColor[i] & 0x00FF0000)>>16;
  170. sGreen = (stemColor[i] & 0x0000FF00)>>8;
  171. sBlue = stemColor[i] & 0x000000FF;
  172. sRed += deltaR;
  173. sGreen += deltaG;
  174. sBlue += deltaB;
  175.  
  176. //draw the actual line segment
  177. line.setLine(x, y, newX, newY);
  178. BasicStroke stroke = new BasicStroke(width);
  179. sWidth[i] = stroke;
  180. g2d.setStroke(sWidth[i]);
  181. width -= deltaWidth;
  182. g2d.draw(line);
  183.  
  184. //update x & y
  185. x = newX;
  186. y = newY;
  187.  
  188. }
  189.  
  190. }
  191.  
  192.  
  193. }
  194. });
  195.  
  196.  
  197. fileMenu.add(option);
  198.  
  199. //---Exit
  200.  
  201. JMenuItem exitItem = new JMenuItem("Exit");
  202. exitItem.addActionListener( new ActionListener()
  203. {
  204. public void actionPerformed(ActionEvent event)
  205. {
  206. System.exit(0);
  207. }
  208. });
  209.  
  210. fileMenu.add(exitItem);
  211.  
  212. //===attach menu to a menu bar
  213.  
  214. JMenuBar menuBar = new JMenuBar();
  215. menuBar.add(fileMenu);
  216. this.setJMenuBar(menuBar);
  217. }
  218.  
  219. //open() - choose a file, load, and display the image.
  220. private void open()
  221. {
  222. File file = getFile();
  223. if (file != null)
  224. {
  225. displayFile(file);
  226. }
  227. }
  228.  
  229. //open a file selected by the user.
  230.  
  231. private File getFile()
  232. {
  233. File file = null;
  234.  
  235. if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
  236. {
  237. file = chooser.getSelectedFile();
  238. }
  239.  
  240. return file;
  241. }
  242.  
  243. //Display specified file in the frame
  244.  
  245. private void displayFile( File file)
  246. {
  247. try
  248. {
  249. displayBufferedImage(ImageIO.read(file));
  250.  
  251. }
  252. catch(IOException exception)
  253. {
  254. JOptionPane.showMessageDialog(this, exception);
  255. }
  256. }
  257.  
  258. //Display BufferedImage
  259.  
  260. public void displayBufferedImage(BufferedImage image)
  261. {
  262. //there are many ways to display a bufferedImage; for now we'll...
  263.  
  264. this.setContentPane( new JScrollPane( new JLabel(new ImageIcon(image))));
  265.  
  266. //JFrames are a type of container. Anytime a container's subcomponents
  267. //are modified(add to or removed from the container, or layout-related
  268. //information changed)after the container has been displayed, one
  269. //call the validate() method--which causes the container to lay out its
  270. //subcomponents again.
  271. this.validate();
  272. }
  273.  
  274. }
Add Comment
Please, Sign In to add comment