Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class GameMaster
- {
- public static void main(String args[])
- {
- // Doing some stuff here, like building rooms, etc...
- // Here I start using images
- DrawRoom drawRoom = new DrawRoom();
- Thread myThread = new Thread(drawRoom);
- myThread.start(); // The first image appears as expected.
- // Then in a while loop, I get user input from the console and process it.
- // According to which room the user is in, I want to draw the corresponding
- //image.
- drawRoom.changeImage("Images/SOME-OTHER-IMAGE.JPG");
- // This however, does not change the shown image!
- }
- }
- public class DrawRoom extends JPanel implements Runnable{
- Image image;
- JFrame frame;
- public DrawRoom(){
- this.image = Toolkit.getDefaultToolkit().getImage("Images/GAME-START.JPG");
- this.frame = new JFrame("The Current Image");
- this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.frame.setSize(640,510);
- }
- public void paintComponent(Graphics g){
- g.drawImage(image,0,0,640,480, this);
- }
- public static void main(String arg[]){
- // Left empty.
- }
- public void run(){
- DrawRoom panel = new DrawRoom();
- this.frame.setContentPane(panel);
- this.frame.setVisible(true);
- }
- public void changeImage(String whichImage){
- this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
- this.frame.revalidate();
- this.frame.repaint();
- }
- }
- import javax.swing.*;
- public class SimpleThreeTierMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // Doing some stuff here, like building rooms, etc...
- // Here I start using images
- DrawRoom drawRoom = new DrawRoom();
- JFrame frame;
- frame = new JFrame("The Current Image");
- frame.setContentPane(drawRoom);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(640,510);
- frame.setVisible(true);
- // Then in a while loop, I get user input from the console and process it.
- // According to which room the user is in, I want to draw the corresponding
- //image.
- long lTime = 2050;
- int iChange = 0;
- try {
- while (true) {
- Thread.sleep (lTime);
- if (iChange == 1)
- drawRoom.changeImage("0112091252a.jpg");
- else
- drawRoom.changeImage("0112091251.jpg");
- iChange = 1 - iChange;
- }
- } catch (InterruptedException iex) {}
- }
- }
- import javax.swing.*;
- import java.awt.*;
- public class DrawRoom extends JPanel {
- Image image;
- public DrawRoom() {
- this.image = Toolkit.getDefaultToolkit().getImage("0112091251.jpg");
- }
- public void paintComponent(Graphics g){
- g.drawImage(image,0,0,640,480, this);
- }
- public void changeImage(String whichImage){
- this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
- this.repaint();
- }
- }
- public void changeImage(String whichImage){
- this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
- this.repaint(); // not this.frame.repaint()!
- }
- public DrawRoom(){
- this.image = Toolkit.getDefaultToolkit().getImage("Images/GAME-START.JPG");
- this.frame = new JFrame("The Current Image");
- this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.frame.setSize(640,510);
- this.frame.setContentPane(this);
- this.frame.setVisible(true);
- }
- ...
- public void run(){
- // do not need to create any DrawRoom instances!
- }
- public void changeImage(String whichImage){
- this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
- this.repaint();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement