Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. class GameMaster
  2. {
  3.  
  4. public static void main(String args[])
  5. {
  6.  
  7. // Doing some stuff here, like building rooms, etc...
  8.  
  9. // Here I start using images
  10. DrawRoom drawRoom = new DrawRoom();
  11. Thread myThread = new Thread(drawRoom);
  12. myThread.start(); // The first image appears as expected.
  13.  
  14. // Then in a while loop, I get user input from the console and process it.
  15. // According to which room the user is in, I want to draw the corresponding
  16. //image.
  17.  
  18. drawRoom.changeImage("Images/SOME-OTHER-IMAGE.JPG");
  19. // This however, does not change the shown image!
  20. }
  21. }
  22.  
  23. public class DrawRoom extends JPanel implements Runnable{
  24.  
  25. Image image;
  26. JFrame frame;
  27.  
  28. public DrawRoom(){
  29.  
  30. this.image = Toolkit.getDefaultToolkit().getImage("Images/GAME-START.JPG");
  31. this.frame = new JFrame("The Current Image");
  32. this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. this.frame.setSize(640,510);
  34. }
  35.  
  36. public void paintComponent(Graphics g){
  37.  
  38. g.drawImage(image,0,0,640,480, this);
  39. }
  40.  
  41. public static void main(String arg[]){
  42. // Left empty.
  43. }
  44.  
  45. public void run(){
  46.  
  47. DrawRoom panel = new DrawRoom();
  48. this.frame.setContentPane(panel);
  49. this.frame.setVisible(true);
  50. }
  51.  
  52. public void changeImage(String whichImage){
  53.  
  54. this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
  55. this.frame.revalidate();
  56. this.frame.repaint();
  57. }
  58. }
  59.  
  60. import javax.swing.*;
  61.  
  62. public class SimpleThreeTierMain {
  63. /**
  64. * @param args
  65. */
  66. public static void main(String[] args) {
  67. // TODO Auto-generated method stub
  68.  
  69. // Doing some stuff here, like building rooms, etc...
  70.  
  71. // Here I start using images
  72. DrawRoom drawRoom = new DrawRoom();
  73. JFrame frame;
  74.  
  75. frame = new JFrame("The Current Image");
  76. frame.setContentPane(drawRoom);
  77. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  78. frame.setSize(640,510);
  79. frame.setVisible(true);
  80.  
  81. // Then in a while loop, I get user input from the console and process it.
  82. // According to which room the user is in, I want to draw the corresponding
  83. //image.
  84.  
  85. long lTime = 2050;
  86. int iChange = 0;
  87. try {
  88. while (true) {
  89. Thread.sleep (lTime);
  90. if (iChange == 1)
  91. drawRoom.changeImage("0112091252a.jpg");
  92. else
  93. drawRoom.changeImage("0112091251.jpg");
  94. iChange = 1 - iChange;
  95. }
  96. } catch (InterruptedException iex) {}
  97. }
  98. }
  99.  
  100. import javax.swing.*;
  101. import java.awt.*;
  102.  
  103. public class DrawRoom extends JPanel {
  104.  
  105. Image image;
  106.  
  107. public DrawRoom() {
  108. this.image = Toolkit.getDefaultToolkit().getImage("0112091251.jpg");
  109. }
  110.  
  111. public void paintComponent(Graphics g){
  112. g.drawImage(image,0,0,640,480, this);
  113. }
  114.  
  115. public void changeImage(String whichImage){
  116. this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
  117. this.repaint();
  118. }
  119. }
  120.  
  121. public void changeImage(String whichImage){
  122.  
  123. this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
  124. this.repaint(); // not this.frame.repaint()!
  125.  
  126. }
  127.  
  128. public DrawRoom(){
  129.  
  130. this.image = Toolkit.getDefaultToolkit().getImage("Images/GAME-START.JPG");
  131. this.frame = new JFrame("The Current Image");
  132. this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  133. this.frame.setSize(640,510);
  134.  
  135. this.frame.setContentPane(this);
  136. this.frame.setVisible(true);
  137.  
  138. }
  139.  
  140. ...
  141.  
  142. public void run(){
  143.  
  144. // do not need to create any DrawRoom instances!
  145.  
  146. }
  147.  
  148. public void changeImage(String whichImage){
  149.  
  150. this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
  151. this.repaint();
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement