Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.InputEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- import javax.swing.BorderFactory;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JColorChooser;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JRadioButton;
- import javax.swing.JSlider;
- import javax.swing.Timer;
- //Shift + F2 --> full api for what the cursor is on
- public class LifeGUIRPatkiPeriod4 {
- public static void main(String[] args){
- LifeRPatkiPeriod4 inpLife = new LifeRPatkiPeriod4();
- LifeGUI gui = new LifeGUI(inpLife);
- }
- }
- class LifeGUI extends MouseAdapter implements ActionListener{
- JFrame window;
- LifePanel drawingPanel;
- int gen;
- Color [][] colorArr = new Color[20][20];
- int[][] hasColor = new int[20][20];
- Color color = Color.red;
- boolean single = true;
- LifeRPatkiPeriod4 inputClass;
- Timer timer;
- JSlider slider;
- LifeGUI(LifeRPatkiPeriod4 inpClass){
- inputClass = inpClass;
- for(int row = 0; row < 20; row++){
- for(int col = 0; col < 20; col++){
- colorArr[row][col] = Color.white;
- }
- }
- for(int row = 0; row < 20; row++){
- for(int col = 0; col < 20; col++){
- hasColor[row][col] = 0;
- }
- }
- //
- // int[][] grid = inpClass.grid;
- // for(int row = 0; row < grid.length; row++){
- // for(int col = 0; col < grid[0].length; col++){
- // if(grid[row][col] == 1){
- // hasColor[row][col] = 1;
- // }
- // }
- // }
- window = new JFrame("Life - Generation " + gen);
- window.setBounds(100, 100, 445, 600);
- window.setResizable(false);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- drawingPanel = new LifePanel();
- drawingPanel.setBounds(20, 20, 400,400);
- drawingPanel.setBorder(BorderFactory.createEtchedBorder());
- JButton button = new JButton("Reset");
- button.setBounds(190, 510, 75, 20);
- slider = new JSlider();
- slider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
- slider.setMajorTickSpacing(200);
- slider.setMinorTickSpacing(2);
- slider.setPaintLabels(true);
- slider.setPaintTrack(true);
- slider.setPaintTicks(true);
- // slider.setBounds(190, 480, slider.getWidth(), slider.getHeight());
- slider.setBounds(135, 450, 300, 50);
- JPanel mainPanel = new JPanel(); //actual drawing life
- mainPanel.setLayout(null);
- JButton runButton = new JButton("Run");
- runButton.addActionListener(this);
- runButton.setBounds(30, 525, 75, 20);
- ButtonGroup group = new ButtonGroup();
- JRadioButton single = new JRadioButton("Single", true);
- JRadioButton continuous = new JRadioButton("Continuous");
- group.add(single);
- group.add(continuous);
- single.addActionListener(this);
- continuous.addActionListener(this);
- JPanel mode = new JPanel();
- mode.setBorder(BorderFactory.createTitledBorder("Mode"));
- mode.setBounds(20, 425, 125, 100);
- mode.add(single);
- mode.add(continuous);
- mode.add(runButton);
- mainPanel.add(runButton);
- mainPanel.add(mode);
- mainPanel.add(button);
- mainPanel.add(slider);
- window.getContentPane().add(mainPanel);
- window.add(mainPanel);
- button.addActionListener(this);
- drawingPanel.addMouseListener(this);
- drawingPanel.addMouseMotionListener(this);
- //create menuBar items
- JMenuBar menuBar = new JMenuBar();
- JMenu fileMenu = new JMenu("File");
- JMenu editMenu = new JMenu("Edit");
- JMenuItem openMenu = new JMenuItem("Open", 'o');
- JMenuItem saveMenu = new JMenuItem("Save", 's');
- JMenuItem clearMenu = new JMenuItem("Clear", 'c');
- JMenuItem colorMenu = new JMenuItem("Color");
- //add action listeners
- openMenu.addActionListener(this);
- saveMenu.addActionListener(this);
- clearMenu.addActionListener(this);
- colorMenu.addActionListener(this);
- //stack items
- fileMenu.add(openMenu);
- fileMenu.add(saveMenu);
- editMenu.add(clearMenu);
- editMenu.add(colorMenu);
- menuBar.add(fileMenu);
- menuBar.add(editMenu);
- window.setJMenuBar(menuBar);
- timer = new Timer(100, this);
- timer.setActionCommand("timerRun");
- timer.addActionListener(this);
- // Let there be light
- mainPanel.add(drawingPanel);
- window.setVisible(true);
- }
- public void colorToInt(){
- for(int row = 0; row < colorArr.length; row++){
- for(int col = 0; col < colorArr[0].length; col++){
- if(colorArr[row][col] != Color.white){
- hasColor[row][col] = 1;
- }
- else{
- hasColor[row][col] = 0;
- }
- }
- }
- }
- public void intToColor(){
- for(int row = 0; row < hasColor.length; row++){
- for(int col = 0; col < hasColor[0].length; col++){
- if(hasColor[row][col] == 1){
- colorArr[row][col] = Color.red;
- }
- else{
- colorArr[row][col] = Color.white;
- }
- }
- }
- }
- public void actionPerformed(ActionEvent e) {
- // System.out.println("Action -> " + e.getActionCommand());
- if (e.getActionCommand() != null) {
- if (e.getActionCommand().equals("Red"))
- color = Color.RED;
- else if (e.getActionCommand().equals("Green"))
- color = Color.GREEN;
- else if (e.getActionCommand().equals("Blue"))
- color = Color.BLUE;
- else if (e.getActionCommand().equals("Reset") || e.getActionCommand().equals("Clear")) {
- gen = 0;
- window.setTitle("Life - Generation " + gen);
- clearDraw();
- }
- else if(e.getActionCommand().equals("Open")){
- JFileChooser fc = new JFileChooser();
- fc.showOpenDialog(window);
- File file = fc.getSelectedFile();
- openFile(file, colorArr);
- }
- else if(e.getActionCommand().equals("Save")){
- JFileChooser fc = new JFileChooser();
- fc.showSaveDialog(window);
- File saveMe = fc.getSelectedFile();
- saveFile(saveMe, colorArr);
- }
- else if(e.getActionCommand().equals("Color")){
- color = JColorChooser.showDialog(null, "Choose a color: ", Color.black);
- }
- else if(e.getActionCommand().equals("Single")){
- single = true;
- timer.stop();
- }
- else if(e.getActionCommand().equals("Continuous")){
- single = false;
- }
- else if(e.getActionCommand().equals("timerRun")){
- gen++;
- window.setTitle("Life - Generation " + gen);
- timer.setDelay(slider.getValue());
- hasColor = inputClass.nextGeneration(hasColor);
- intToColor();
- drawingPanel.repaint();
- }
- else if(e.getActionCommand().equals("Run")){
- if(single == true){
- gen++;
- window.setTitle("Life - Generation " + gen);
- hasColor = inputClass.runLife(1, hasColor);
- intToColor();
- drawingPanel.repaint();
- }
- else{
- timer.start();
- }
- }
- }
- }
- public void clearDraw() {
- for(int row = 0; row < 20; row++){
- for(int col = 0; col < 20; col++){
- colorArr[row][col] = Color.white;
- }
- }
- colorToInt();
- drawingPanel.repaint();
- }
- public void mouseClicked(MouseEvent e) {
- Color tempColor;
- if(e.getButton() == MouseEvent.BUTTON1){
- if(e.getX()/20*20/20 >= 20 || e.getX()/20*20/20 < 0 || e.getY()/20*20/20 >= 20 || e.getY()/20*20/20 < 0){
- }
- else{
- colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
- }
- drawingPanel.repaint();
- colorToInt();
- }
- else if(e.getButton() == MouseEvent.BUTTON3){
- tempColor = color;
- color = Color.white;
- if(e.getX()/20*20/20 >= 20 || e.getX()/20*20/20 < 0 || e.getY()/20*20/20 >= 20 || e.getY()/20*20/20 < 0){
- }
- else{
- colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
- }
- drawingPanel.repaint();
- colorToInt();
- color = tempColor;
- }
- }
- public void mouseDragged(MouseEvent e) {
- Color tempColor = color;
- //update array
- int mouseInfo = e.getModifiersEx();
- if((mouseInfo & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK){
- //make sure in bounds of drawingPanel
- if(e.getX()/20*20/20 >= 20 || e.getX()/20*20/20 < 0 || e.getY()/20*20/20 >= 20 || e.getY()/20*20/20 < 0){
- }
- else{
- colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
- }
- drawingPanel.repaint();
- colorToInt();
- }
- else if((mouseInfo & InputEvent.BUTTON3_DOWN_MASK) == InputEvent.BUTTON3_DOWN_MASK){
- tempColor = color;
- color = Color.white;
- if(e.getX()/20*20/20 >= 20 || e.getX()/20*20/20 < 0 || e.getY()/20*20/20 >= 20 || e.getY()/20*20/20 < 0){
- }
- else{
- colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
- }
- drawingPanel.repaint();
- colorToInt();
- color = tempColor;
- }
- }
- public void saveFile(File f, Color[][] arr){
- //write each individual component into the new file
- String ans = "P3\n20 20\n255\n"; //type (ppm), width height, max color val
- try{
- FileWriter fw = new FileWriter(f);
- fw.write(ans);
- for(int row = 0; row < 20; row++){
- for(int col = 0; col < 20; col++){
- String redVal = "" + arr[row][col].getRed();
- String greenVal = "" + arr[row][col].getGreen();
- String blueVal = "" + arr[row][col].getBlue();
- fw.write(redVal + " ");
- fw.write(greenVal + " ");
- fw.write(blueVal + " ");
- }
- }
- fw.close();
- }
- catch(IOException e){
- System.out.println("Error: " + e.getMessage());
- }
- }
- public void openFile(File f, Color[][] arr){
- Scanner in;
- try{
- in = new Scanner(f);
- if(in.next().equals("P3") == false){
- throw new IOException("The file you are attempting to open" +
- "is not a valid PPM file.");
- }
- else{
- String next;
- while(true){
- next = in.next();
- if(next.charAt(0) == '#'){
- in.nextLine();
- }
- else{
- break;
- }
- }
- int width = Integer.valueOf(next); //not used, but need to advance scanner
- int height = in.nextInt();
- while(true){
- next = in.next();
- if(next.charAt(0) == '#'){
- in.nextLine();
- }
- else{
- break;
- }
- }
- int colorMax = Integer.valueOf(next); //in.nextInt();
- while(true){
- next = in.next();
- if(next.charAt(0) == '#'){
- in.nextLine();
- }
- else{
- break;
- }
- }
- int xCount = 0;
- int yCount = 0;
- while(in.hasNext()){
- int red;
- if(xCount == 0 && yCount == 0){
- red = Integer.valueOf(next);
- }
- else{
- red = in.nextInt();
- }
- int green;
- int blue;
- if(in.hasNext()){
- green = in.nextInt();
- }
- else{
- break;
- }
- if(in.hasNext()){
- blue = in.nextInt();
- }
- else{
- break;
- }
- Color inputColor = new Color(red, green, blue);
- arr[yCount][xCount] = inputColor;
- if(xCount == 19){
- xCount = -1;
- }
- if(xCount == -1){
- yCount++;
- }
- xCount++;
- }
- }
- }
- catch(IOException e){
- System.out.println(e.getMessage());
- }
- }
- class LifePanel extends JPanel{
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.white);
- g.fillRect(2, 2, this.getWidth()-2, this.getHeight()-2);
- g.setColor(Color.lightGray);
- for (int x = 0; x < this.getWidth(); x += 20)
- g.drawLine(x, 0, x, this.getHeight());
- for (int y = 0; y < this.getHeight(); y += 20)
- g.drawLine(0, y, this.getWidth(), y);
- for(int row = 0; row < 20; row++){
- for(int col = 0; col < 20; col++){
- g.setColor(colorArr[row][col]);
- g.fillRect(row*20, col*20, drawingPanel.getWidth()/20, drawingPanel.getHeight()/20);
- //outline:
- g.setColor(Color.lightGray);
- g.drawRect(row*20, col*20, drawingPanel.getWidth()/20, drawingPanel.getHeight()/20);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement