Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.76 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.InputEvent;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.util.Scanner;
  12.  
  13. import javax.swing.BorderFactory;
  14. import javax.swing.ButtonGroup;
  15. import javax.swing.JButton;
  16. import javax.swing.JColorChooser;
  17. import javax.swing.JFileChooser;
  18. import javax.swing.JFrame;
  19. import javax.swing.JMenu;
  20. import javax.swing.JMenuBar;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JPanel;
  23. import javax.swing.JRadioButton;
  24. import javax.swing.JSlider;
  25. import javax.swing.Timer;
  26.  
  27.  
  28. //Shift + F2 --> full api for what the cursor is on
  29.  
  30. public class LifeGUIRPatkiPeriod4 {
  31. public static void main(String[] args){
  32. LifeRPatkiPeriod4 inpLife = new LifeRPatkiPeriod4();
  33. LifeGUI gui = new LifeGUI(inpLife);
  34. }
  35. }
  36.  
  37. class LifeGUI extends MouseAdapter implements ActionListener{
  38. JFrame window;
  39. LifePanel drawingPanel;
  40. int gen;
  41. Color [][] colorArr = new Color[20][20];
  42. int[][] hasColor = new int[20][20];
  43. Color color = Color.red;
  44. boolean single = true;
  45. LifeRPatkiPeriod4 inputClass;
  46. Timer timer;
  47. JSlider slider;
  48.  
  49. LifeGUI(LifeRPatkiPeriod4 inpClass){
  50.  
  51. inputClass = inpClass;
  52. for(int row = 0; row < 20; row++){
  53. for(int col = 0; col < 20; col++){
  54. colorArr[row][col] = Color.white;
  55. }
  56. }
  57.  
  58. for(int row = 0; row < 20; row++){
  59. for(int col = 0; col < 20; col++){
  60. hasColor[row][col] = 0;
  61. }
  62. }
  63. //
  64. // int[][] grid = inpClass.grid;
  65. // for(int row = 0; row < grid.length; row++){
  66. // for(int col = 0; col < grid[0].length; col++){
  67. // if(grid[row][col] == 1){
  68. // hasColor[row][col] = 1;
  69. // }
  70. // }
  71. // }
  72.  
  73. window = new JFrame("Life - Generation " + gen);
  74. window.setBounds(100, 100, 445, 600);
  75. window.setResizable(false);
  76. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  77.  
  78. drawingPanel = new LifePanel();
  79. drawingPanel.setBounds(20, 20, 400,400);
  80. drawingPanel.setBorder(BorderFactory.createEtchedBorder());
  81.  
  82. JButton button = new JButton("Reset");
  83. button.setBounds(190, 510, 75, 20);
  84.  
  85. slider = new JSlider();
  86. slider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
  87. slider.setMajorTickSpacing(200);
  88. slider.setMinorTickSpacing(2);
  89. slider.setPaintLabels(true);
  90. slider.setPaintTrack(true);
  91. slider.setPaintTicks(true);
  92. // slider.setBounds(190, 480, slider.getWidth(), slider.getHeight());
  93. slider.setBounds(135, 450, 300, 50);
  94.  
  95. JPanel mainPanel = new JPanel(); //actual drawing life
  96. mainPanel.setLayout(null);
  97.  
  98. JButton runButton = new JButton("Run");
  99. runButton.addActionListener(this);
  100. runButton.setBounds(30, 525, 75, 20);
  101.  
  102. ButtonGroup group = new ButtonGroup();
  103. JRadioButton single = new JRadioButton("Single", true);
  104. JRadioButton continuous = new JRadioButton("Continuous");
  105. group.add(single);
  106. group.add(continuous);
  107.  
  108. single.addActionListener(this);
  109. continuous.addActionListener(this);
  110. JPanel mode = new JPanel();
  111. mode.setBorder(BorderFactory.createTitledBorder("Mode"));
  112. mode.setBounds(20, 425, 125, 100);
  113. mode.add(single);
  114. mode.add(continuous);
  115. mode.add(runButton);
  116.  
  117. mainPanel.add(runButton);
  118. mainPanel.add(mode);
  119. mainPanel.add(button);
  120. mainPanel.add(slider);
  121. window.getContentPane().add(mainPanel);
  122. window.add(mainPanel);
  123.  
  124. button.addActionListener(this);
  125. drawingPanel.addMouseListener(this);
  126. drawingPanel.addMouseMotionListener(this);
  127.  
  128. //create menuBar items
  129. JMenuBar menuBar = new JMenuBar();
  130. JMenu fileMenu = new JMenu("File");
  131. JMenu editMenu = new JMenu("Edit");
  132. JMenuItem openMenu = new JMenuItem("Open", 'o');
  133. JMenuItem saveMenu = new JMenuItem("Save", 's');
  134. JMenuItem clearMenu = new JMenuItem("Clear", 'c');
  135. JMenuItem colorMenu = new JMenuItem("Color");
  136.  
  137. //add action listeners
  138. openMenu.addActionListener(this);
  139. saveMenu.addActionListener(this);
  140. clearMenu.addActionListener(this);
  141. colorMenu.addActionListener(this);
  142.  
  143. //stack items
  144. fileMenu.add(openMenu);
  145. fileMenu.add(saveMenu);
  146. editMenu.add(clearMenu);
  147. editMenu.add(colorMenu);
  148. menuBar.add(fileMenu);
  149. menuBar.add(editMenu);
  150. window.setJMenuBar(menuBar);
  151.  
  152.  
  153.  
  154. timer = new Timer(100, this);
  155. timer.setActionCommand("timerRun");
  156. timer.addActionListener(this);
  157.  
  158.  
  159. // Let there be light
  160. mainPanel.add(drawingPanel);
  161. window.setVisible(true);
  162.  
  163. }
  164.  
  165. public void colorToInt(){
  166. for(int row = 0; row < colorArr.length; row++){
  167. for(int col = 0; col < colorArr[0].length; col++){
  168. if(colorArr[row][col] != Color.white){
  169. hasColor[row][col] = 1;
  170. }
  171. else{
  172. hasColor[row][col] = 0;
  173. }
  174. }
  175. }
  176. }
  177.  
  178. public void intToColor(){
  179. for(int row = 0; row < hasColor.length; row++){
  180. for(int col = 0; col < hasColor[0].length; col++){
  181. if(hasColor[row][col] == 1){
  182. colorArr[row][col] = Color.red;
  183. }
  184. else{
  185. colorArr[row][col] = Color.white;
  186. }
  187. }
  188. }
  189. }
  190.  
  191. public void actionPerformed(ActionEvent e) {
  192. // System.out.println("Action -> " + e.getActionCommand());
  193.  
  194. if (e.getActionCommand() != null) {
  195.  
  196. if (e.getActionCommand().equals("Red"))
  197. color = Color.RED;
  198. else if (e.getActionCommand().equals("Green"))
  199. color = Color.GREEN;
  200. else if (e.getActionCommand().equals("Blue"))
  201. color = Color.BLUE;
  202.  
  203. else if (e.getActionCommand().equals("Reset") || e.getActionCommand().equals("Clear")) {
  204. gen = 0;
  205. window.setTitle("Life - Generation " + gen);
  206. clearDraw();
  207. }
  208. else if(e.getActionCommand().equals("Open")){
  209. JFileChooser fc = new JFileChooser();
  210. fc.showOpenDialog(window);
  211. File file = fc.getSelectedFile();
  212. openFile(file, colorArr);
  213. }
  214. else if(e.getActionCommand().equals("Save")){
  215. JFileChooser fc = new JFileChooser();
  216. fc.showSaveDialog(window);
  217.  
  218. File saveMe = fc.getSelectedFile();
  219. saveFile(saveMe, colorArr);
  220. }
  221. else if(e.getActionCommand().equals("Color")){
  222. color = JColorChooser.showDialog(null, "Choose a color: ", Color.black);
  223. }
  224. else if(e.getActionCommand().equals("Single")){
  225. single = true;
  226. timer.stop();
  227. }
  228. else if(e.getActionCommand().equals("Continuous")){
  229. single = false;
  230. }
  231. else if(e.getActionCommand().equals("timerRun")){
  232. gen++;
  233. window.setTitle("Life - Generation " + gen);
  234. timer.setDelay(slider.getValue());
  235. hasColor = inputClass.nextGeneration(hasColor);
  236. intToColor();
  237. drawingPanel.repaint();
  238. }
  239. else if(e.getActionCommand().equals("Run")){
  240. if(single == true){
  241. gen++;
  242. window.setTitle("Life - Generation " + gen);
  243. hasColor = inputClass.runLife(1, hasColor);
  244. intToColor();
  245. drawingPanel.repaint();
  246. }
  247. else{
  248. timer.start();
  249. }
  250. }
  251. }
  252. }
  253.  
  254. public void clearDraw() {
  255. for(int row = 0; row < 20; row++){
  256. for(int col = 0; col < 20; col++){
  257. colorArr[row][col] = Color.white;
  258. }
  259. }
  260. colorToInt();
  261. drawingPanel.repaint();
  262. }
  263.  
  264. public void mouseClicked(MouseEvent e) {
  265. Color tempColor;
  266. if(e.getButton() == MouseEvent.BUTTON1){
  267. 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){
  268. }
  269. else{
  270. colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
  271. }
  272. drawingPanel.repaint();
  273. colorToInt();
  274.  
  275. }
  276. else if(e.getButton() == MouseEvent.BUTTON3){
  277. tempColor = color;
  278. color = Color.white;
  279. 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){
  280. }
  281. else{
  282. colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
  283. }
  284. drawingPanel.repaint();
  285. colorToInt();
  286. color = tempColor;
  287. }
  288. }
  289.  
  290.  
  291. public void mouseDragged(MouseEvent e) {
  292. Color tempColor = color;
  293. //update array
  294. int mouseInfo = e.getModifiersEx();
  295. if((mouseInfo & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK){
  296. //make sure in bounds of drawingPanel
  297. 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){
  298. }
  299. else{
  300. colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
  301. }
  302. drawingPanel.repaint();
  303. colorToInt();
  304. }
  305. else if((mouseInfo & InputEvent.BUTTON3_DOWN_MASK) == InputEvent.BUTTON3_DOWN_MASK){
  306.  
  307. tempColor = color;
  308. color = Color.white;
  309. 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){
  310. }
  311. else{
  312. colorArr[e.getX()/20*20/20][e.getY()/20*20/20] = color;
  313. }
  314. drawingPanel.repaint();
  315. colorToInt();
  316. color = tempColor;
  317.  
  318. }
  319.  
  320. }
  321.  
  322. public void saveFile(File f, Color[][] arr){
  323. //write each individual component into the new file
  324. String ans = "P3\n20 20\n255\n"; //type (ppm), width height, max color val
  325. try{
  326. FileWriter fw = new FileWriter(f);
  327. fw.write(ans);
  328. for(int row = 0; row < 20; row++){
  329. for(int col = 0; col < 20; col++){
  330. String redVal = "" + arr[row][col].getRed();
  331. String greenVal = "" + arr[row][col].getGreen();
  332. String blueVal = "" + arr[row][col].getBlue();
  333. fw.write(redVal + " ");
  334. fw.write(greenVal + " ");
  335. fw.write(blueVal + " ");
  336. }
  337. }
  338. fw.close();
  339. }
  340. catch(IOException e){
  341. System.out.println("Error: " + e.getMessage());
  342. }
  343. }
  344.  
  345. public void openFile(File f, Color[][] arr){
  346. Scanner in;
  347. try{
  348. in = new Scanner(f);
  349. if(in.next().equals("P3") == false){
  350. throw new IOException("The file you are attempting to open" +
  351. "is not a valid PPM file.");
  352. }
  353. else{
  354. String next;
  355. while(true){
  356. next = in.next();
  357. if(next.charAt(0) == '#'){
  358. in.nextLine();
  359. }
  360. else{
  361. break;
  362. }
  363. }
  364. int width = Integer.valueOf(next); //not used, but need to advance scanner
  365. int height = in.nextInt();
  366. while(true){
  367. next = in.next();
  368. if(next.charAt(0) == '#'){
  369. in.nextLine();
  370. }
  371. else{
  372. break;
  373. }
  374. }
  375. int colorMax = Integer.valueOf(next); //in.nextInt();
  376. while(true){
  377. next = in.next();
  378. if(next.charAt(0) == '#'){
  379. in.nextLine();
  380. }
  381. else{
  382. break;
  383. }
  384. }
  385. int xCount = 0;
  386. int yCount = 0;
  387. while(in.hasNext()){
  388. int red;
  389. if(xCount == 0 && yCount == 0){
  390. red = Integer.valueOf(next);
  391. }
  392. else{
  393. red = in.nextInt();
  394. }
  395. int green;
  396. int blue;
  397. if(in.hasNext()){
  398. green = in.nextInt();
  399. }
  400. else{
  401. break;
  402. }
  403. if(in.hasNext()){
  404. blue = in.nextInt();
  405. }
  406. else{
  407. break;
  408. }
  409. Color inputColor = new Color(red, green, blue);
  410. arr[yCount][xCount] = inputColor;
  411. if(xCount == 19){
  412. xCount = -1;
  413. }
  414. if(xCount == -1){
  415. yCount++;
  416. }
  417. xCount++;
  418. }
  419. }
  420.  
  421. }
  422. catch(IOException e){
  423. System.out.println(e.getMessage());
  424. }
  425. }
  426.  
  427.  
  428.  
  429.  
  430.  
  431. class LifePanel extends JPanel{
  432. public void paintComponent(Graphics g) {
  433. super.paintComponent(g);
  434. g.setColor(Color.white);
  435. g.fillRect(2, 2, this.getWidth()-2, this.getHeight()-2);
  436.  
  437. g.setColor(Color.lightGray);
  438. for (int x = 0; x < this.getWidth(); x += 20)
  439. g.drawLine(x, 0, x, this.getHeight());
  440.  
  441. for (int y = 0; y < this.getHeight(); y += 20)
  442. g.drawLine(0, y, this.getWidth(), y);
  443.  
  444. for(int row = 0; row < 20; row++){
  445. for(int col = 0; col < 20; col++){
  446. g.setColor(colorArr[row][col]);
  447. g.fillRect(row*20, col*20, drawingPanel.getWidth()/20, drawingPanel.getHeight()/20);
  448. //outline:
  449. g.setColor(Color.lightGray);
  450. g.drawRect(row*20, col*20, drawingPanel.getWidth()/20, drawingPanel.getHeight()/20);
  451. }
  452. }
  453.  
  454. }
  455. }
  456.  
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement