Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. package controller;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6. import java.util.ArrayList;
  7. import java.util.Deque;
  8. import java.util.Iterator;
  9.  
  10. import model.Blob;
  11. import model.Dot;
  12.  
  13. /**
  14. * Class solely used for displaying the game.
  15. */
  16.  
  17. public class MyFrame extends Frame {
  18. private Blob blob;
  19. private ArrayList<Blob> blobs;
  20. private ArrayList<Dot> dots;
  21. public boolean chatVisible;
  22. private TextArea chat;
  23. private Deque<String> chatHistory;
  24.  
  25. /**
  26. * Constructor; sets up listener handling closing the game properly.
  27. * @param s window title
  28. * @param blob player's blob
  29. * @param blobs other players' blobs
  30. * @param dots dots - food - to be displayed
  31. */
  32.  
  33. public MyFrame(String s, Blob blob, ArrayList<Blob> blobs, ArrayList<Dot> dots, Deque<String> chatHistory) {
  34. super(s);
  35. this.blob = blob;
  36. this.blobs = blobs;
  37. this.dots = dots;
  38. this.chatVisible = false;
  39. this.chatHistory = chatHistory;
  40. this.chat = new TextArea("");
  41. chat.setBounds(92, 64, 150, 21);
  42.  
  43. setBounds(0, 0, 1920, 1080);
  44. add(blob);
  45. blobs.add(blob);
  46. setVisible(true);
  47.  
  48. this.addWindowListener(new WindowAdapter() {
  49. public void windowClosing(WindowEvent e) {
  50. System.exit(0);
  51. }
  52. });
  53. }
  54.  
  55. /**
  56. * Method overriding Frame.paint(Graphics g). Paints blobs and dots.
  57. * @param g specified Graphics window
  58. */
  59.  
  60. public void paint(Graphics g) {
  61. //blob.paint(g);
  62. for (Blob b : blobs) {
  63. b.paint(g);
  64. }
  65. synchronized (dots) {
  66. Iterator i = ((ArrayList<Dot>)dots.clone()).iterator();
  67. while (i.hasNext()) {
  68. Dot d = (Dot) i.next();
  69. d.paint(g);
  70. }
  71. }
  72. if(chatVisible) {
  73. Iterator iterator = chatHistory.iterator();
  74. while (iterator.hasNext())
  75. chat.append(iterator.next() + "\n");
  76. chat.paint(g);
  77. System.out.println("widac");
  78. }
  79. }
  80.  
  81. /**
  82. * Method overriding Component.update(Graphics g). Paints a field, blobs and dots. Called by this component's repaint.
  83. * @param g specified Graphics window
  84. */
  85.  
  86. public void update(Graphics g) {
  87. Image buffer1 = createImage(getWidth(), getHeight());
  88. g = buffer1.getGraphics();
  89. paint(g);
  90. //getGraphics().drawImage(buffer1, blob.getX()/2, blob.getY()/2, this);
  91. getGraphics().drawImage(buffer1, 0, 0, this);
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement