Results 1 to 5 of 5
  1. #1
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default JFrame from another class

    Hello,
    I'm trying to add a jpanel to my program.
    However i created another class to set my program to fullscreen. I'm trying to access my created my JFrame but nothing happens.
    Java Code:
    //ScreenManger Class
    public void setFullscreen(DisplayMode dm){
    		f = new JFrame();
    		f.setUndecorated(true);
    		f.setIgnoreRepaint(false);
    		f.setResizable(false);
    		graphicsDevice.setFullScreenWindow(f);
    // a graphics device
               }
    public JFrame myFrame(){
    		return f;
    	}
    
    //gamev1 Class
    Window w;
    public void run(){
     w = screenManager.getFullScreeenWindow();
    				w.addMouseListener(this);
    				w.addMouseMotionListener(this);      
    				w.setFocusTraversalKeysEnabled(false);
    				w.addKeyListener(this);
    //more code
    }
    
    public void selection(){
                            JFrame f = screenManager.myFrame();
    			f.setVisible(true);
    			
    			GridLayout grid = new GridLayout(2, 4);
    			main.setVisible(true);
    			main.setLayout(grid);
    			JButton button = new JButton();
    			button.setVisible(true);
    			main.add(button);
    			f.add(main);}
    How do i access the Jframe that im working with from my main class so i can add the JPanel?
    Thank you

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: JFrame from another class

    Did you get any error message?

  3. #3
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: JFrame from another class

    No error messages just nothing is happening, im trying to draw stuff but nothing

  4. #4
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: JFrame from another class

    can you post complete code here? because i couldn't understand main
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  5. #5
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: JFrame from another class

    Its rather long,
    Java Code:
    //gamev1
    package Com.dd;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.DisplayMode;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.awt.image.BufferedImage;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import org.apache.commons.io.FileUtils;
    import org.imgscalr.Scalr;
    
    public class gamev1 implements KeyListener, Runnable, MouseMotionListener,MouseListener	 {
    
    	
    	public static void main(String[] args) {
    		 new gamev1();
    	}
    	 public gamev1() {
    			      animator = new Thread(this);
    				 animator.start();  
    	}
    	
    int x,y;
    boolean loaded;
    	 long pauseTracker = 0;
    	private Thread animator;
    	long distanceTraveledX;
    	long lives = 3;
    	long distanceTraveledY;
    	int RandomArray = 0;
    	private ScreenManager screenManager;
    	private BufferedImage backGroundImage;
    	private BufferedImage resizedBackgroundImage;
    	JButton[] slimeButtons = new JButton[9];
    	private Image enemy;
    	private BufferedImage resizedSlimeImage;
    	private BufferedImage drackyImage;
    	private BufferedImage resizedDrackyImage;
    	private BufferedImage slimeImage;
    	
    	private ImageIcon drakSlimeImage;
    	private ImageIcon darkSlimeImage;
    	private ImageIcon kingSlimeImage;
    	private ImageIcon bubbleSlimeImage;
    	private ImageIcon liquidMetalSlimeImage;
    	private ImageIcon healSlimeImage;
    	
    	
    	
    	private Image face1;
    	private Image face2;
    	JFrame f;
    long startTime;
    long EndTime;
    	  // used by the pause 'button'
    	  private volatile boolean OverDracky = false;
    	  private volatile boolean OverSlime = false;
    	  private Rectangle Slime;
    	  private Rectangle Dracky;
    	  private volatile boolean gameOver;
    	  
    	  private volatile boolean isPaused = false;
    	 private int MAX_FRAME_SKIPS = 5;
    	Random rand = new Random();
    	private int max = 0;
    	Window w;
    	private int min=0;
    	private int r1= 200;
    	private int r2 = 200;
    	SpriteV2 s2 = new SpriteV2();	
    	private int FPSIndex = 0;
    	float FPS_100= 10000000;
    	float FPS_60 = 16666667 ;
    	float FPS_50=  20000000 ;
    	float FPS_40=  25000000;
    	float FPS_30 = 33333334 ;
    	
    	ArrayList<Float> FPS = new ArrayList<Float>();
    	long animationTime; 
    	String hit = "";	
    	JPanel[] jpanels = new JPanel[3];
    	//before the loop starts program boot
    	long afterTime;
    	//after a cycle
    	long TimeReq;
    	// Time Req for a cycle
    	long precycleTime;
    	// The time before a cycle
    	long sleepTime;
    	//How long the thread shoulds sleep , Nano;
    	double fps;
    	double fpsStored;
    	boolean running;
    	boolean password = false;
    	CardLayout menus = new CardLayout();
    	
    	//float VelocityY;
    	
    	private final DisplayMode modes1[] = {
    		new DisplayMode(1920, 1080, 32,0),
    		new DisplayMode(1920, 1080, 16,0),
    		new DisplayMode(1600, 900, 32,0),
    		new DisplayMode(1600, 900, 16,0),
    		new DisplayMode(1366, 768, 32,60),
    		new DisplayMode(1366, 768, 16,0),
    		new DisplayMode(1200, 720, 32,0),
    		new DisplayMode(1200, 720, 16,0),
    		new DisplayMode(800, 600, 32,0),
    		new DisplayMode(800, 600, 16,0),
    		new DisplayMode(640, 480, 32,0),
    		new DisplayMode(640, 480, 24,0),
    		new DisplayMode(640, 480, 16,0),
    	};
    	//load Images
    	public void loadImages(){
    		try {
    			slimeImage = ImageIO.read(new File("C:\\Users\\dq8-slime-blue.jpg"));
    			 drackyImage = ImageIO.read(new File("C:\\Users\\dracky.png"));
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		 resizedBackgroundImage = Scalr.resize(backGroundImage, Scalr.Method.ULTRA_QUALITY,screenManager.getWidth(),screenManager.getHeight(), Scalr.OP_ANTIALIAS);
    		 resizedSlimeImage = Scalr.resize(slimeImage, Scalr.Method.ULTRA_QUALITY,screenManager.getWidth()/4,screenManager.getHeight()/6, Scalr.OP_ANTIALIAS);
    		 resizedDrackyImage = Scalr.resize(drackyImage, Scalr.Method.ULTRA_QUALITY,screenManager.getWidth()/4,screenManager.getHeight()/6, Scalr.OP_ANTIALIAS);
    		
    	
    		//more imges
    //			
    		}
    	
    	private void gameOver(){
    		if (gameOver){
    		System.exit(0);
    		animator = null;
    		}
    	}
    	
    	public void loadingScreen(){
    		 startTime = System.currentTimeMillis();
    		screenManager = new ScreenManager();
    		DisplayMode dm = screenManager.findFirstCompatiblemode(modes1);
    		
    				screenManager.setFullscreen(dm);
    		try {
    			backGroundImage  = ImageIO.read(new File("C:/Users/bOb/workspace/Game tutorial/Dragon quest game/Dragon quest background.jpg"));
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		Graphics2D g =  screenManager.getGraphics();
    		g.drawImage(backGroundImage, 0, 0, null);
    		screenManager.update();
    		g.dispose();
    
    			
    		}
    		
    	
    	@Override
    	public void run(){
    		
    		loadingScreen();
    		running = true;
    	
    		try {
    			 	FPS.add(0, FPS_60);
    				FPS.add(1, FPS_50);
    				FPS.add(2, FPS_40);
    				FPS.add(3, FPS_30);
    				FPS.add(4,FPS_100);
    				
    				 //slimeIcons[1].setImage(image)
    				for (int i = 0; i < jpanels.length; i++) {
    					jpanels[i] = new JPanel();
    					jpanels[i].setVisible(true);
    					
    				}
    				 w = screenManager.getFullScreeenWindow();
    				w.addMouseListener(this);
    				w.addMouseMotionListener(this);      
    				w.setFocusTraversalKeysEnabled(false);
    				w.addKeyListener(this);
    			Graphics2D	g = screenManager.getGraphics();
    			loadImages();
    			
    			 ArrayList<ImageIcon> slimeIcons = new ArrayList<ImageIcon>();
    			 slimeIcons.add(0,healSlimeImage);
    			 slimeIcons.add(1,bubbleSlimeImage);
    			 slimeIcons.add(2,darkSlimeImage);
    			 slimeIcons.add(3,drakSlimeImage);
    			 slimeIcons.add(4,liquidMetalSlimeImage);
    			 slimeIcons.add(5,kingSlimeImage);
    			 slimeIcons.add(6,kingSlimeImage);
    			 slimeIcons.add(7,kingSlimeImage);
    			 slimeIcons.add(8,kingSlimeImage);
    			 
    			 for (int i = 0; i < slimeButtons.length; i++) {
    				slimeButtons[i] = new JButton();	
    				slimeButtons[i].setIcon(slimeIcons.get(i));
    				slimeButtons[i].setVisible(true);
    				slimeButtons[i].setBorderPainted(false);
    				slimeButtons[i].addActionListener(new ActionListener() {
    						
    						@Override
    						public void actionPerformed(ActionEvent e) {
    							System.out.println("hi");
    							password = true;
    							
    						}
    					});
    				}
    			loadData();
    			loaded = true ;
    			EndTime = System.currentTimeMillis();
    			long loadingTime = EndTime - startTime;
    			System.out.print(loadingTime);
    			IntroScreen(g);
    			
    			movieLoop();
    			
    			
    		} finally {
    			screenManager.restoreScreen();
    		}
    	}
    		private void IntroScreen(Graphics2D g) {			
    			 g =  screenManager.getGraphics();
    			 int counter =0;
    			 Slime = new Rectangle(0,0,resizedSlimeImage.getWidth(),resizedSlimeImage.getHeight());
    				Dracky = new Rectangle(500,500,resizedDrackyImage.getWidth(),resizedDrackyImage.getHeight());
    			while (!password){
    				if(!OverSlime &&!OverDracky){
    				
    				g.drawImage(resizedBackgroundImage, 0, 0,null);
    				g.drawImage(resizedSlimeImage,0,0,null);
    				g.drawImage(resizedDrackyImage,500,500,null);
    				System.out.println("bye");
    				}
    				
    				
    				if(OverDracky){
    					password = true;
    					s2.spriteAnimation(resizedDrackyImage, 100);
    					s2.spriteAnimation(resizedDrackyImage, 100);
    				}
    				if(OverSlime&& counter ==0 ){
    					
    					System.out.println("bob");
    					slimeScreen();
    					counter++;
    				}
    				
    				g.dispose();
    				screenManager.update();
    				}
    		}
    
    
    		public void slimeScreen() {
    			Graphics2D g = screenManager.getGraphics();
    			//Dracky = null;
    			g.drawImage(resizedBackgroundImage,0,0,null);
    			JPanel main = new JPanel(){
    				 public void paint(Graphics2D g) {
    					g = screenManager.getGraphics();
    				g.drawImage(resizedBackgroundImage,0,0,null);
    				}
    			};
    		JFrame f = screenManager.myFrame();
    			f.setVisible(true);
    			
    			GridLayout grid = new GridLayout(2, 4);
    			main.setVisible(true);
    			main.setLayout(grid);
    			JButton button = new JButton();
    			button.setVisible(true);
    			button.setIcon(healSlimeImage);
    			main.add(button);
    			f.add(main);
    			
    			
    			
    		}
    		public void loadData(){
    		
    			 File file =new File("C:/Users/Bob/GameDevelopment/Bob Game/Lib/Dragon quest game/saveData.txt");
    			
    		
    			 if(!file.exists()){
    	    	    	try {
    						file.createNewFile();
    					} catch (IOException e){} 										
    	    	    }
    			 try {
    				 String livesString =	FileUtils.readFileToString(file);
    			 System.out.println(livesString);
    			 lives = Long.parseLong(livesString);
    			 
    			} catch (IOException e) {
    				
    			}
    			
    		}
    			 
    		
    		public void saveData(){
    					
    			    	BufferedWriter bw = null;
    			 
    			    	try{
    			 
    			    	    String content = ""+lives;
    			    	    
    			 
    			    	    File file =new File("C:/Users/saveData.txt");
    			 
    			    	    if(!file.exists()){
    			    	    	file.createNewFile();
    			    	    }
    			    	    FileUtils.write(file, content);
    			    	 System.out.println(content);
    			 
    			    	}catch(IOException e){
    			    		e.printStackTrace();
    			    }	}
    			    
    
    		
    			@Override
    			public void keyPressed(KeyEvent e){
    			
    				int  keyCode  = e.getKeyCode();
    				if(keyCode == KeyEvent.VK_P){
    					saveData();
    					
    				}
    				if(keyCode == KeyEvent.VK_ESCAPE){
    					 gameOver = true;
    				}
    							
    				if(keyCode == KeyEvent.VK_W){
    					moveUp();
    						
    					}
    				if(keyCode == KeyEvent.VK_A){
    					moveLeft();
    				
    					}
    				if(keyCode == KeyEvent.VK_D){
    					moveRight();
    										}
    				if(keyCode == KeyEvent.VK_L){
    				loadData();
    										}
    				
    				if(keyCode == KeyEvent.VK_SPACE){
    					//animator.suspend();
    					isPaused =! isPaused;
    					standStill();
    					}
    				if(keyCode == KeyEvent.VK_S){
    				moveDown();
    					}
    				if(keyCode == KeyEvent.VK_ENTER){
    					
    					FPSIndex ++;
    					
    					if(FPSIndex >= 5){
    						FPSIndex = 0;
    					}
    						standStill();
    						
    						}
    				}
    			
    			
    			private void moveDown() {
    				//sprite.setVelocityX(0);
    				s2.setVelocityY(5*(FPS.get(FPSIndex))/FPS_60);
    			}
    
    			private void moveRight() {
    				s2.setVelocityX(5*(FPS.get(FPSIndex))/FPS_60);
    				//sprite.setVelocityY(0);
    				
    			}
    
    			private void moveLeft() {
    				s2.setVelocityX(-5*(FPS.get(FPSIndex))/FPS_60);
    				//sprite.setVelocityY(0);
    				
    			}
    
    			private void moveUp() {			
    				//sprite.setVelocityX(0);
    				s2.setVelocityY (-5*(FPS.get(FPSIndex))/FPS_60);
    				
    			}
    			
    			public void projectile(){
    				
    			}
    
    			// key release
    			@Override
    			public void keyReleased(KeyEvent e){
    				int  keyCode  = e.getKeyCode();
    				if(keyCode == KeyEvent.VK_W){
    					standStill();				
    				}
    				if(keyCode == KeyEvent.VK_A){
    				standStill();
    					}
    				if(keyCode == KeyEvent.VK_D){
    				standStill();
    					}
    				if(keyCode == KeyEvent.VK_S){
    					standStill();
    					}
    
    			}
    	
    			private void standStill() {
    			//sprite.setVelocityX(0);
    			//sprite.setVelocityY(0);
    				s2.setVelocityX(0);
    				s2.setVelocityY(0);
    //			
    				
    			}
    
    			@Override
    			public void keyTyped(KeyEvent e) {
    				
    				e.consume();
    				
    			}
    		// 
    	
    	public void movieLoop(){
    	
    		s2.setVelocityX(0);
    		s2.setVelocityY(0);
    		while(running){
    			Graphics2D	g = screenManager.getGraphics();		
    			precycleTime = System.nanoTime();
    			RandomArray++;
    			if(RandomArray==50){
    			r2 = rand.nextInt(screenManager.getHeight()-enemy.getHeight(null) - min + 1) + min;
    			r1 = rand.nextInt(screenManager.getWidth()-enemy.getWidth(null) - min + 1) + min;
    			RandomArray=0;}
    			gameOver();	
    			 wrapeEffect(1);
    			 update();	
    			
    			collision();
    			screenManager.update();
    			draw(g);
    			g.dispose();
    			
    			afterTime = System.nanoTime();
    			
    			
    			TimeReq = (afterTime - precycleTime);
    			sleepTime = (long) ((FPS.get(FPSIndex) - TimeReq)/1000000);
    			
    			
    			// How long thread sleeps, seconds
    			
    			if (sleepTime > 0 ){
    				//System.out.println("hi");
    			try {
    					Thread.sleep(sleepTime);
    					
    				} catch (InterruptedException e) 
    					{
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    					}
    			 int skips = 0;
    			 int excess = 0;
    			while((excess > FPS.get(FPSIndex)/1000000) && (skips < MAX_FRAME_SKIPS)) {
    		        excess -= FPS.get(FPSIndex)/1000000;
    		        wrapeEffect(1);
    		        update();
    			   // screenManager.update();
    			    // update state but don't render
    		        skips++;
    		        
    		        fps = 	1000000000/(System.nanoTime() - precycleTime);
    		      }
    	}
    			fps = 	1000000000/(System.nanoTime() - precycleTime);
    
    						
    			}}
    	private void update() {
    			s2.update();
    			
    		}
    	public Rectangle getSlimeButtonRectangle(){
    		return new Rectangle(0,0, resizedSlimeImage.getWidth(), resizedSlimeImage.getWidth());
    	}
    	
    	public Rectangle getMySpriteRectangle()
    	
    	{
    		return new Rectangle((int)s2.getX(), (int) s2.getY(), s2.getHeight(), s2.getWidth());
    		//return new Rectangle((int)sprite.getX(), (int) sprite.getY(), sprite.getHeight(), sprite.getWidth());
    	}
    	public Rectangle getMyEnemyRectangle()
    	{
    		return new Rectangle(r1,r2, enemy.getHeight(null), enemy.getWidth(null));
    	}
    
    
    	private void collision() {
    		 Rectangle s = getMySpriteRectangle();
    		 Rectangle e = getMyEnemyRectangle();
    		
    		 if (s.intersects(e)){
    
    			 hit = ("Hit");
    			lives -=1;	
    		 }
    		 else{
    			 hit = "Missed";
    		 } 
    		
    		}
    	//draws graphics
    	public void draw(Graphics g){	
    		
    		if (!isPaused && !gameOver){ 	
    		g.drawImage(resizedBackgroundImage,0,0,null);		
    		g.drawImage(enemy,r1,r2,null);
    		g.drawString(s2.getX()+"",660,600);
    		g.drawImage(s2.getImage(),(int) s2.getX(), (int) s2.getY(),null);
    	    g.drawString(""+fps, 20, 20);
    	    g.drawString(hit, 200, 30);
    	    g.drawString(""+lives, 400, 20);
    	    g.drawString(""+sleepTime, 500, 20);
    	    g.drawString(""+(-5*(FPS.get(FPSIndex))/FPS_60), 700, 20);
    	    g.drawString(""+TimeReq, 420, 50);
    		}
    		else{
    			g.drawString("PAUSED", 500, 500);
    		}
    	}
    
    	
    	//update sprite
    	public void wrapeEffect(long TimeReq){
    		if(!isPaused&& !gameOver){
    			if(s2.getX() < 0 - s2.getWidth()){
    				s2.setX(screenManager.getWidth());
    				
    			}
    			if(s2.getX() > screenManager.getWidth()){
    				s2.setX(screenManager.getWidth()-screenManager.getWidth() - s2.getWidth());
    				
    			}
    				s2.changeLocation(TimeReq);
    	//Wrap effect for x axis	
    				if(s2.getY() < 0 - s2.getHeight() ){
    					s2.setY(screenManager.getHeight());
    					
    				}
    				if(s2.getY() > screenManager.getHeight()){
    					s2.setY(screenManager.getHeight()-screenManager.getHeight() - s2.getHeight());
    					
    				}
    			
    	s2.changeLocation(TimeReq);
    			
    	}
    	}
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		x = e.getX();
    		y = e.getY();
    		OverDracky = Dracky.contains(x,y) ? true : false;
    		OverSlime = Slime.contains(x,y) ? true : false;
    		
    	}
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	
    	
    		}
    //sprite class
    package Com.dd;
    import java.util.ArrayList;
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    
    public class SpriteV2 {
    	private long TotalTime = 0;
    	Image OpenMouth = new ImageIcon("C:/Users/Bob/workspace/Game tutorial/Dragon quest game/Face1.png").getImage();
    	Image ClosedMouth = new ImageIcon("C:/Users/Bob/workspace/Game tutorial/Dragon quest game/face2.png").getImage();
    	private int x ;
    	//x coordinates
    	private int y ;
    	//y coordinates
    	private int w;
    	private int h;
    	private float vx ;
    	//Horizontal Velocity 
    	private float vy ;
    	//Vertical Velocity 
    	private ArrayList<Image> images;
    	private ArrayList<Long> animationTimes;
    	private int ImageIndex;
    	private int animationTimesIndex;
    	
    	//long updateTime;
    	
    	public SpriteV2(){
    		images = new ArrayList<Image>();
    		animationTimes = new ArrayList<Long>();
    		//images.add(0, OpenMouth);
    		//images.add(1, ClosedMouth);
    		//images.add(2, OpenRightFace);
    		//images.add(3, ClosedRightFace);
    		//images.add(4, DownFace );
    		//images.add(5, UpFace);
    		//images.add(5, DownFace);
    		//x = OpenMouth.getWidth(null);
    		//y = OpenMouth.getHeight(null);
    	}
    	public void changeLocation(long updateTime){
    	x += vx*updateTime;
    	y += vy*updateTime;
    	}
    	
    	public int getWidth(){
    		w= (images.get(ImageIndex)).getWidth(null);
    		return w;
    	} 
    	public int getHeight(){
    		h= (images.get(ImageIndex)).getHeight(null);
    		return h;
    	} 
    	
    	//get  horizontal velocity
    	public float getVelocityX(){
    		return vx;
    	}
    	//get  Vertical velocity
    		public float getVelocityY(){
    			return vy;
    		}
    		//set horizontal velocity
    		public void setVelocityX(float vx){
    			this.vx = vx;
    		}
    		//set Vertical velocity
    				public void setVelocityY(float vy){
    					this.vy = vy;
    				}
    				
    				//get sprites image
    		long TimeBetween = 0;
    	
    		public void spriteAnimation(Image i,long t){
    			images.add(i);
    			animationTimes.add(t);
    		}
    		public void upAnimation(){
    		ImageIndex = 5;
    		while(ImageIndex<6){
    		ImageIndex++;	
    		}
    		
    		}
    		public void downAnimation(){
    		ImageIndex = 3;
    		while(ImageIndex<4){
    			ImageIndex++;
    	
    		}
    		}
    		public void rightAnimation(){
    			ImageIndex = 1;
    			while(ImageIndex<2){
    				ImageIndex++;
    		
    			}
    	
    		}
    		public void leftAnimation(){
    			ImageIndex = 7;
    			while(ImageIndex<8){
    				ImageIndex++;
    		
    			}
    		}
    		
    		public synchronized void update(){
    			
    			TimeBetween = animationTimes.get(animationTimesIndex);
    		
    			ImageIndex++;
    			if(ImageIndex>1){
    				ImageIndex =0;
    			}
    					
    				
    		}
    		public Image getImage(){
    			Image pic;
    			pic =(images.get(ImageIndex));
    			return pic;
    		}
    	public int getX(){
    		return x;
    	}
    	public int getY(){
    		return y;
    	}
    	//set sprite position
    	public void setX(int x){
    		this.x = x;
    	}
    	public void setY(int y){
    		this.y = y;
    	}}
    
    
    //screen Manager
    package Com.dd;
    import java.awt.DisplayMode;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Window;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class ScreenManager {
    	gamev1 game = new gamev1();
    	private GraphicsDevice graphicsDevice;
    	
    	public ScreenManager(){
    		//give access to moniter
    		GraphicsEnvironment e  = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		graphicsDevice = e.getDefaultScreenDevice();
    	}
    //get all dms compatible 
    	public DisplayMode[] getCompatibleDisplayModes(){
    		return graphicsDevice.getDisplayModes();
    		
    	}
    	//compares Dmpassed  in to vcDMsee if they match
    	public DisplayMode findFirstCompatiblemode(DisplayMode modes[]){
    		DisplayMode goodModes[] =graphicsDevice.getDisplayModes();
    		for(int  x= 0;x<modes.length; x++){
    			for( int y =0; y<goodModes.length;y++){
    				if(displayModesMatch(modes[x], goodModes[y])){
    					return modes[x];
    				}
    			}
    		}
    		return null;
    	}
    	//get current dm
    	public DisplayMode getCurrentDisplayMode(){
    		return graphicsDevice.getDisplayMode();
    	}
    	//checks if two modes match each other
    	public boolean displayModesMatch( DisplayMode m1, DisplayMode m2){
    		if(m1.getWidth()!= m2.getWidth()|| m1.getHeight() != m2.getHeight()){
    			return false;
    		}
    		if(m1.getBitDepth()!= DisplayMode.BIT_DEPTH_MULTI&& m2.getBitDepth()!=DisplayMode.BIT_DEPTH_MULTI&& m1.getBitDepth() != m2.getBitDepth()){
    			return false;
    		} 
    		if (m1.getRefreshRate() !=DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate()!=DisplayMode.REFRESH_RATE_UNKNOWN&& m1.getRefreshRate()!= m2.getRefreshRate()){
    			
    		}
    		return true;
    		
    		
    }
    	JFrame f;
    	
    	public void setFullscreen(DisplayMode dm){
    		f = new JFrame();
    		f.setIconImage(game.slimeIcon());
    		f.setUndecorated(true);
    		f.setIgnoreRepaint(false);
    		f.setResizable(false);
    		JPanel main = new JPanel();
    		f.add(main);
    		graphicsDevice.setFullScreenWindow(f);
    		if(dm != null && graphicsDevice.isDisplayChangeSupported()){
    			try {
    				graphicsDevice.setDisplayMode(dm);
    			} catch (Exception e) {
    				// TODO: handle exception
    			}
    		}
    		f.createBufferStrategy(2);
    		
    	}
    	public JFrame myFrame(){
    		return f;
    	}
    	
    	//we will set Graphics object = to method returning
    	public Graphics2D getGraphics(){
    		Window w = graphicsDevice.getFullScreenWindow();
    	if(w!=null){
    		BufferStrategy s = w.getBufferStrategy();
    		return(Graphics2D)s.getDrawGraphics();
    		
    	}	
    	else {
    		return null;
    	}
    	}
    	public void update(){
    		Window w = graphicsDevice.getFullScreenWindow();
    		if(w != null){
    			BufferStrategy s =  w.getBufferStrategy();
    			if(!s.contentsLost()){
    				s.show();
    			}
    		}
    	}
    	//return full screen window
    	public Window getFullScreeenWindow(){
    			return graphicsDevice.getFullScreenWindow();
    			}
    	public int getWidth(){
    		Window w = graphicsDevice.getFullScreenWindow();
    		if (w!= null){
    			return w.getWidth();
    		}
    		else{
    			return 0;
    		}
    	}
    	public int getHeight(){
    		Window w = graphicsDevice.getFullScreenWindow();
    		if (w!= null){
    			return w.getHeight();
    		}
    		else{
    			return 0;
    		}
    	}
    	//get out of fullscreen
    	
    	public void restoreScreen(){
    		Window w = graphicsDevice.getFullScreenWindow();
    		if(w!= null){
    			w.dispose();
    		}
    		graphicsDevice.setFullScreenWindow(null);
    	}
    	//create compatible image
    	public BufferedImage createCompatibleImage(int w, int h, int t){
    		Window win = graphicsDevice.getFullScreenWindow();
    		if (win != null){
    			GraphicsConfiguration gc =win.getGraphicsConfiguration();
    			return gc.createCompatibleImage(w, h,t);
    		}
    		return null;
    	}
    	
    	
    	}

Similar Threads

  1. How to call JDialog class from JFrame class?
    By garnachito in forum New To Java
    Replies: 4
    Last Post: 07-24-2012, 04:29 AM
  2. JTextField on JPanel-class within JFrame-class
    By floris in forum AWT / Swing
    Replies: 5
    Last Post: 06-25-2012, 01:54 PM
  3. Dispose Jframe from another class.
    By vahshir in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2011, 01:33 PM
  4. Class that extends JFrame help
    By javaman1 in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 03:29 AM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •