Results 1 to 2 of 2
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default My program runs fine for me, but others need to resize to see it

    I wrote a tiny game (if you can even call it that!) and before it was not working for me, and I needed to resize it to see my images. Now, I no longer need to resize, but I packaged it into a jar and other people say they cannot see it unless they resize (they are running off other computers). Why will it work for me and not for others? How can I make it work for everyone :D?

    Image class:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class FeedPeter extends GameM {
    	private BufferedImage image;
    	private BufferedImage image2;
    	private BufferedImage image3;
    
    	public FeedPeter() {
    		try {
    			image = ImageIO.read(new File(
    					"/Users/MWaldman/Downloads/petergriffin2.png"));
    			image2 = ImageIO.read(new File(
    					"/Users/MWaldman/Downloads/cheeseburger2.png"));
    			image3 = ImageIO.read(new File(
    					"/Users/MWaldman/Downloads/petericon.png"));
    			repaint();
    		} catch (Exception e) {
    			System.out.println("Error");
    		}
    	}
    
    	public void paintComponent(Graphics g) {
    		if (image != null) { // added to prevent null pointer exception
    			super.paintComponent(g);
    			g.drawImage(image, x, y, null);
    			g.drawImage(image2, ex, why, null);
    		}
    
    	}
    
    	public static void main(String[] args) {
    
    		new FeedPeter();
    
    	}
    }
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Timer;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.util.*;
    
    class GameM extends JComponent {
    	Random ar = new Random();
    	public int x = ar.nextInt(400);
    	Random har = new Random();
    	public int y = har.nextInt(400);
    	Random r = new Random();
    	Random rand = new Random();
    	Random newx;
    	Random newy;
    	int resetTimes = 0;
    	int ex = r.nextInt(400);
    	int why = rand.nextInt(400);
    	Random ra;
    	Random ra2;
    	
    
    	public void ResetIt() {
    		if (x <= ex + 25 && y <= why + 25 && x >= ex - 25 && y >= why - 25) {
    
    			newx = new Random();
    			newy = new Random();
    			int newex = newx.nextInt(450);
    			int newwhy = newy.nextInt(450);
    			x = newex;
    			y = newwhy;
    			ra = new Random();
    			ra2 = new Random();
    			ex = ra.nextInt(400);
    			why = ra2.nextInt(400);
    			resetTimes++;
    			repaint();
    
    		}
    	}
    
    	KeyListener kw = new KeyListener() {
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			
    
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
    			int k = e.getKeyCode();
    			if (k == KeyEvent.VK_W) {
    				y = y - 10;
    				repaint();
    				ResetIt();
    
    			}
    			if (k == KeyEvent.VK_S) {
    				y = y + 10;
    				repaint();
    				ResetIt();
    
    			}
    			if (k == KeyEvent.VK_A) {
    				x = x - 10;
    				repaint();
    				ResetIt();
    
    			}
    			if (k == KeyEvent.VK_D) {
    				x = x + 10;
    				repaint();
    				ResetIt();
    			}
    			if (k == KeyEvent.VK_Q) {
    				x = x - 10;
    				y = y - 10;
    				repaint();
    				ResetIt();
    			}
    			if (k == KeyEvent.VK_E) {
    				x = x + 10;
    				y = y - 10;
    				repaint();
    				ResetIt();
    			}
    			if (k == KeyEvent.VK_RIGHT) {
    				x = x + 10;
    				repaint();
    				ResetIt();
    			}
    			if (k == KeyEvent.VK_LEFT) {
    				x = x - 10;
    				repaint();
    				ResetIt();
    			}
    			if (k == KeyEvent.VK_DOWN) {
    				y = y + 10;
    				repaint();
    				ResetIt();
    
    			}
    			if (k == KeyEvent.VK_UP) {
    				y = y - 10;
    				repaint();
    				ResetIt();
    
    			}
    		};
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    
    		}
    
    	};
    
    	GameM() {
    		JFrame f = new JFrame("Feed Peter");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(500,500);
    		f.validate();
    		JPanel p = new JPanel();
    		p.setLayout(new BorderLayout());
    		f.add(p);
    		p.add(this);
    		f.addKeyListener(kw);
    		p.setBackground(Color.blue);
    		f.setVisible(true);
    		try{
    			f.setIconImage(Toolkit.getDefaultToolkit().getImage("/Users/MW/Downloads/petericon.png"));
    		} catch(Exception e){System.out.println("Did not receive Icon");}
    	}
    
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.white);
    		g.drawString("MW Games 2014", 350, 15);
    		g.drawString("Score:" + resetTimes, 20, 20);
    		g.drawString("Use WASD or arrow keys", 150, 20);
    		g.drawString("Level "+Math.round(Math.sqrt(resetTimes)*1)/1 * .5, 20, 50);
    		g.setColor(Color.blue);
    		g.fillRect(65,30,20,20);
    	}
    
    }
    So, yeah. Adding repaint to the image class (feedpeter) constructor is what fixed it for me, but apparently not for other people lol.


    On a super small side note, for thread economy, on line 152 I try to change the icon, but it is still a coffee cup. The exception is not handled so I don't think that's whats wrong.

    Thank you !!
    Last edited by MW130; 01-21-2014 at 06:02 PM.

  2. #2
    thechazm is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: My program runs fine for me, but others need to resize to see it

    I'm no expert but I had ran across this problem myself. The issue I was up against was screen resolution differences. I made a simple Scaling class that does the math for me:

    Java Code:
    package manning;
    
    import java.awt.Dimension;
    
    public class Scaling {
    	public static Dimension getScaledDimension(Dimension imgSize) {
    		WindowFunctions W = new WindowFunctions();
    		Dimension window = new Dimension(W.GetWidth(), W.GetHeight());
    		Dimension prefScreenSize = new Dimension(1920, 1040);
    	    
    	    if (prefScreenSize.width > window.width ){
    	    	
    	    	double aspect = (double) window.width / (double) prefScreenSize.width;
    	    	int new_width = (int) (aspect * imgSize.width);
    	    	return new Dimension(new_width, getNewHeight(imgSize));
    	    	
    	    }else if (prefScreenSize.width < window.width){
    	    	
    	    	double aspect = (double) prefScreenSize.width / (double) window.width;
    	    	int new_width = (int) (aspect * imgSize.width);
    	    	return new Dimension(new_width, getNewHeight(imgSize));
    	    	
    	    }else if (prefScreenSize.height != window.height){
    	    	
    	    	return new Dimension(imgSize.width, getNewHeight(imgSize));
    	    	
    	    }else{
    	    	
    	    	return imgSize;
    	    	
    	    }
    	    
    	}
    	
    	public static int getNewHeight(Dimension imgSize){
    		WindowFunctions W = new WindowFunctions();
    		Dimension window = new Dimension(W.GetWidth(), W.GetHeight());
    		Dimension prefScreenSize = new Dimension(1920, 1080);
    		
    		if (prefScreenSize.height > window.width){
    			
    			double aspect = (double) window.height / (double) prefScreenSize.height;
    	    	int new_height = (int) (aspect * imgSize.height);
    	    	return new_height;
    	    	
    		}else if (prefScreenSize.height < window.height){
    			
    			double aspect = (double) prefScreenSize.height / (double) window.height;
    	    	int new_height = (int) (aspect * imgSize.height);
    			return new_height;
    			
    		}else{
    			return imgSize.height;
    		}
    		
    	}
    }
    Then all I had to do was call this Scaling.getScaledDimension(prefSize); during any of my getPreferredSize() calls. Dunno if this will help in this situation but just throwing it out there.

Similar Threads

  1. Replies: 12
    Last Post: 02-03-2013, 07:53 PM
  2. Replies: 7
    Last Post: 09-06-2012, 09:21 PM
  3. Replies: 6
    Last Post: 02-08-2011, 02:29 AM
  4. Program working fine until I add methods....
    By LadyMonsterFace in forum New To Java
    Replies: 3
    Last Post: 12-04-2010, 03:22 AM
  5. Code runs fine but get compile error
    By BillyB in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 02:18 PM

Posting Permissions

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