Results 1 to 3 of 3
  1. #1
    SparklyLlama is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default paintComponent(g) Not Scaling BufferedImage?

    In my main class for my Game, I have the following code:

    Java Code:
        // dimensions
        public static final int WIDTH = 266;
        public static final int HEIGHT = 200;
        public static final int SCALE = 3;
    
        // game loop
    	private Thread thread;
    	private boolean running = true;
    	public static int count = 0;
    	
    	// rendering
    	private BufferedImage image;
    
        public Panel() {
    		
    		setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    		
    		image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    		
    	}
    
        public void run() {
    		
    		long start = System.nanoTime();
    		final double numUpdates = 30.0;
    		double ns = 1000000000 / numUpdates;
    		double delta = 0;
    		
    		while(running) {
    			
    			long current = System.nanoTime();
    			delta += (current - start) / ns;
    			start = current;
    			if(delta >= 1) {
    				update();
    				delta--;
    			}
    			repaint();
    			
    		}
    		
    	}
    
        public void paintComponent(Graphics g) {
    		
    		super.paintComponent(g);
    		
    		g.setColor(new Color(230, 100, 100));
    		g.fillRect(0, 0, 200, 100);
    		
    		g.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
    		
    	}
    
        public static void main(String[] args) {
    		
    		JFrame frame = new JFrame("Rage Mage");
    		
    		frame.add(new Panel());
    		frame.setResizable(false);
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    	}
    The `image` isn't being scaled though, it's drawn at it's actual dimensions, rather than having it's dimensions multiplied by 3 (`SCALE`).

    Also, if I remove the `super.paintComponent(g);` line and the `g.drawImage(...);` line, the rectangle is still being rendered.

    Thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: paintComponent(g) Not Scaling BufferedImage?

    Please submit a Short, Self Contained, Correct Example. Make sure it can compile and run and don't include the game stuff.
    Just demonstrate how an image is not scaling as you think it should.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: paintComponent(g) Not Scaling BufferedImage?

    I don't get the code. You're creating an empty image and the rectangle you speak of comes from this line of code:

    Java Code:
    g.fillRect(0, 0, 200, 100);
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. movement during scaling
    By PRW56 in forum New To Java
    Replies: 4
    Last Post: 07-12-2012, 02:10 PM
  2. Scaling VS. Clipping
    By JojoDiaz in forum New To Java
    Replies: 0
    Last Post: 11-14-2011, 05:10 AM
  3. scaling image
    By OmerHalit in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2010, 02:30 AM
  4. Scaling a JPEG
    By ScottVal in forum Advanced Java
    Replies: 5
    Last Post: 03-21-2009, 09:47 PM
  5. scaling images
    By willemjav in forum Java Applets
    Replies: 7
    Last Post: 06-19-2008, 10:54 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
  •