Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Math;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import java.awt.geom.*;
- import java.io.*;
- import java.util.Random;
- import javax.imageio.*;
- import javax.swing.*;
- public class HW04a
- {
- private static final int WIDTH = 400;
- private static final int HEIGHT = 400;
- public static void main(String[] args)
- {
- JFrame frame = new ImageFrame (WIDTH, HEIGHT);
- frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
- class ImageFrame extends JFrame
- {
- private final JFileChooser chooser;
- private BufferedImage image;
- private Line2D.Double line;
- public ImageFrame ( int width, int height) //is sent (400, 400)
- {
- this.setTitle ("CAP3027 2008-HW04a-Yangyang Liu");
- this.setSize (width, height);
- //add a menu to the frame
- addMenu();
- //setup the file chooser dialog
- chooser = new JFileChooser();
- chooser.setCurrentDirectory( new File("."));
- }
- private void addMenu()
- {
- //setup the frame's menu bar
- // === File menu
- JMenu fileMenu = new JMenu("File");
- //---open
- JMenuItem option = new JMenuItem("Directed Random Walk Plant");
- option.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- int i = 0, j = 0;
- String input1 = JOptionPane.showInputDialog("Enter height: ");
- //take the user input as the dimension of the square
- int height = Integer.parseInt(input1);
- String input2 = JOptionPane.showInputDialog("How many stems?");
- int stem = Integer.parseInt(input2);
- String input3 = JOptionPane.showInputDialog("How many steps?");
- int step = Integer.parseInt(input3);
- String input4 = JOptionPane.showInputDialog("Transmission Probability?");
- double alpha = Double.parseDouble(input4);
- String input5 = JOptionPane.showInputDialog("Maximum Rotation Increment?");
- double rotation = Double.parseDouble(input5);
- String input6 = JOptionPane.showInputDialog("Growth Segment Increment?");
- double growth = Double.parseDouble(input6);
- String input7 = JOptionPane.showInputDialog("Starting Color?");
- int sColor = (int)Long.parseLong(input7,16);
- String input8 = JOptionPane.showInputDialog("Ending Color?");
- int eColor = (int)Long.parseLong(input8,16);
- image = new BufferedImage(height+1, height+1,
- BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2d = (Graphics2D)image.createGraphics();
- //Specify antialiased lines are preferred.
- RenderingHints hint =
- new RenderingHints( RenderingHints.KEY_TEXT_ANTIALIASING,
- RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- g2d.setRenderingHints( hint );
- //display BufferedImage
- displayBufferedImage(image);
- //set background white by setting "pen" color white
- g2d.setColor(Color.BLACK);
- g2d.fillRect(0,0, height, height);
- Random rnd = new Random();
- double beta = 1.0- alpha; //relection probability [0.0, 1.0]
- double tau =0.0; //the virtual coin's bias
- double r = 1.0; //growth segment length
- int[] stemColor = new int[step-1];//for color interpolation
- BasicStroke[] sWidth = new BasicStroke[step-1];//for stroke width interpolation
- //randomly generating each particle's starting coordinates
- for(i = 0; i < stem; i++)
- {
- double theta = Math.PI/2; //start growing "upwards"
- double direction = -1; //direction to turn (-1 = left| +1 = right)
- double x = height/2; //grow from point (x, y)
- double y = height/2;
- double newX = 0.0;
- double newY = 0.0;
- //extracting colors
- int sAlpha = (sColor & 0xFF000000)>>24;
- int sRed = (sColor & 0x00FF0000)>>16;
- int sGreen = (sColor & 0x0000FF00)>>8;
- int sBlue = sColor & 0x000000FF;
- int eAlpha = (eColor & 0xFF000000)>>24;
- int eRed = (eColor & 0x00FF0000)>>16;
- int eGreen = (eColor & 0x0000FF00)>>8;
- int eBlue = eColor & 0x000000FF;
- //color interpolation
- int deltaR = (eRed-sRed)/step;
- int deltaG = (eGreen - sGreen)/step;
- int deltaB = (eBlue - sBlue)/step;
- //synthesize color
- int color;
- //width interpolation
- float width = 6.0f;
- float deltaWidth = (6.0f-0.5f)/step;
- //creating a line object
- line = new Line2D.Double();
- //determine each stem's growth pattern
- for(j = 0; j < step; j++)
- {
- if(direction == -1) tau = alpha;
- else tau = beta;
- //flip the biased coin to determine which
- //direction to turn next
- double bias = rnd.nextDouble();
- if(rnd.nextDouble() > tau) direction = 1;
- else direction = -1;
- //compute offset from end of the old growth
- //segment to the end of the new one
- r += growth;
- theta += (rotation*bias*direction);
- newX = x+(r*Math.cos(theta));
- newY = y-(r*Math.sin(theta));
- //set stem color by step
- color = sAlpha<<24|sRed<<16|sGreen<<8|sBlue;
- stemColor[i] = color;
- g2d.setColor(new Color(stemColor[i], false));
- sAlpha = (stemColor[i] & 0xFF000000)>>24;
- sRed = (stemColor[i] & 0x00FF0000)>>16;
- sGreen = (stemColor[i] & 0x0000FF00)>>8;
- sBlue = stemColor[i] & 0x000000FF;
- sRed += deltaR;
- sGreen += deltaG;
- sBlue += deltaB;
- //draw the actual line segment
- line.setLine(x, y, newX, newY);
- BasicStroke stroke = new BasicStroke(width);
- sWidth[i] = stroke;
- g2d.setStroke(sWidth[i]);
- width -= deltaWidth;
- g2d.draw(line);
- //update x & y
- x = newX;
- y = newY;
- }
- }
- }
- });
- fileMenu.add(option);
- //---Exit
- JMenuItem exitItem = new JMenuItem("Exit");
- exitItem.addActionListener( new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- System.exit(0);
- }
- });
- fileMenu.add(exitItem);
- //===attach menu to a menu bar
- JMenuBar menuBar = new JMenuBar();
- menuBar.add(fileMenu);
- this.setJMenuBar(menuBar);
- }
- //open() - choose a file, load, and display the image.
- private void open()
- {
- File file = getFile();
- if (file != null)
- {
- displayFile(file);
- }
- }
- //open a file selected by the user.
- private File getFile()
- {
- File file = null;
- if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
- {
- file = chooser.getSelectedFile();
- }
- return file;
- }
- //Display specified file in the frame
- private void displayFile( File file)
- {
- try
- {
- displayBufferedImage(ImageIO.read(file));
- }
- catch(IOException exception)
- {
- JOptionPane.showMessageDialog(this, exception);
- }
- }
- //Display BufferedImage
- public void displayBufferedImage(BufferedImage image)
- {
- //there are many ways to display a bufferedImage; for now we'll...
- this.setContentPane( new JScrollPane( new JLabel(new ImageIcon(image))));
- //JFrames are a type of container. Anytime a container's subcomponents
- //are modified(add to or removed from the container, or layout-related
- //information changed)after the container has been displayed, one
- //call the validate() method--which causes the container to lay out its
- //subcomponents again.
- this.validate();
- }
- }
Add Comment
Please, Sign In to add comment