Results 1 to 3 of 3
  1. #1
    Jib
    Jib is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default set the maximum size for a window?

    Hello everyone.
    Thanks for reading my thread.

    As I'm begginer I started watching tutorials on youtube and I made a little cirle move with W,A,S,D keys.

    Here is my code:
    Game.java:
    Java Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    public class Game extends gameLoop{
    	public void init(){// san function start
    		setSize(854, 480);
    		Thread thread = new Thread(this);
    		thread.start();
    		offscreen = createImage(854, 480);
    		d = offscreen.getGraphics();
    		addKeyListener(this);
    	}
    	
    	public void paint(Graphics g){
    		d.clearRect(0, 0, 854, 480);
    		d.drawOval(x, y, 20, 20);
    		d.setColor (Color.blue);
    		g.drawImage(offscreen, 0, 0, this);
    	}
    	
    	public void update(Graphics g){//function update
    		paint(g);
    	}
    }
    gameLoop.java:

    Java Code:
    import java.applet.Applet;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    
    public class gameLoop extends Applet implements Runnable, KeyListener{
    	
    	public int x, y;
    	
    	public Image offscreen;
    	public Graphics d;
    	
    	public boolean up, down, left, right;
    	
    	public void run() {
    		x = 10;
    		y = 10;
    		while(true){
    			if(left == true){
    				x--;
    			}
    			int width = this.getWidth();
    			System.out.println(width);
    			
    			int height = this.getHeight();
    			System.out.println(height);
    			
    			if(x <= 0){
    				x = 0;
    			}
    			
    			if(x >= width){
    				x = width;
    			}
    			
    			
    			if(right == true){
    				x++;
    			}
    			
    			if(down == true){
    				y++;
    			}
    			
    			if(up == true){
    				y--;
    			}
    			
    			repaint();
    			
    			try {
    				Thread.sleep(1); //i taxitita
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	private Component getContentPane() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode() == 65 || e.getKeyCode() == 37){//left 
    			left = true;
    		}
    		
    		if(e.getKeyCode() == 68 || e.getKeyCode() == 39){//right
    			right = true;
    		}
    		
    		if(e.getKeyCode() == 87 || e.getKeyCode() == 38){//up
    			up = true;
    		}
    		
    		if(e.getKeyCode() == 83 || e.getKeyCode() == 40){//down
    			down = true;
    		}
    	}
    
    	public void keyReleased(KeyEvent e) {
    		if(e.getKeyCode() == 37 || e.getKeyCode() == 65 ){//left
    			left = false;
    		}
    		
    		if(e.getKeyCode() == 68 || e.getKeyCode() == 39){//right
    			right = false;
    		}
    		
    		if(e.getKeyCode() == 87 || e.getKeyCode() == 38){//up
    			up = false;
    		}
    		
    		if(e.getKeyCode() == 83 || e.getKeyCode() == 40){//down
    			down = false;
    		}
    	}
    
    	public void keyTyped(KeyEvent e) {}
    }
    This is not a JPanel. My problem is that I have no idea how to set the maximum size for my window.
    Is there any way of doing this without JPanel?

    Thanks in advance, any reply is appriciated!

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: set the maximum size for a window?

    If I'm not mistaken, the Applet size is usually set in the html? I haven't worked with applets in a long time though.

    Though, Applet does have this method:

    Java Code:
    Applet applet = new Applet();
    applet.setMaximumSize(new Dimension(400, 300));

  3. #3
    Jib
    Jib is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: set the maximum size for a window?

    Quote Originally Posted by AndrewM16921 View Post
    If I'm not mistaken, the Applet size is usually set in the html? I haven't worked with applets in a long time though.

    Though, Applet does have this method:

    Java Code:
    Applet applet = new Applet();
    applet.setMaximumSize(new Dimension(400, 300));
    Thanks! :)

Similar Threads

  1. Maximum Backup Size in Log4J ERROR
    By yebenes in forum New To Java
    Replies: 2
    Last Post: 10-09-2012, 09:33 AM
  2. JPanel getSize() getWidth(); also force maximum size?
    By AcousticBruce in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 04:22 AM
  3. get maximum heap size that can be allocated
    By gautam.aditya97 in forum Java 2D
    Replies: 3
    Last Post: 04-05-2010, 05:03 AM
  4. Maximum size of POST to Servlet
    By Ward in forum Java Servlet
    Replies: 0
    Last Post: 03-04-2009, 11:35 AM
  5. Maximum size of an array
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-20-2007, 11:11 AM

Posting Permissions

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