Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Getter from other class and border problem.

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getter from other class and border problem.

    Hi, I'm new to Java and I just can't find out how to do this properly.

    I have a main class that hold the frame and paints, a class for drawing my game, and an image that is drawn too.
    I want to count the amount of times I click on this "android" (right now it counts if i click the screen) but the text from the class Game doesn't update.

    And I don't understand why
    androidX = Main.width - android.getWidth();
    doesn't work. In my head the image should be inside the borders but it isn't. Have I calculated the pixels wrong or am I missing something?


    public class Main extends JPanel{
     
    	public static final int width = 600;
    	public static final int height = 600;
     
    	Game game = new Game();
    	Android android = new Android();
     
    	public void paint(Graphics g) {
    		super.paint(g);
    		game.render(g);
    		android.render(g);
    	}
     
    	public Main() {
    		addMouseListener(new MouseListener() {
     
    			public void mouseClicked(MouseEvent e) {
    			}
     
    			public void mouseEntered(MouseEvent e) {
    			}
     
    			public void mouseExited(MouseEvent e) {
    			}
     
    			public void mousePressed(MouseEvent e) {
    					System.out.println("Hit: " + android.getTimesHit()); // This works
    					android.mousePressed(e);
    			}
     
    			public void mouseReleased(MouseEvent e) {
    			}
     
    		});
     
    		setFocusable(true);
     
    	}
     
    	public static void main(String[] args) throws InterruptedException {
     
    		Main main = new Main();
     
    		JFrame frame = new JFrame("Main");
    		frame.add(main);
    		frame.setSize(width, height);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
     
    		while (true) {
    			main.repaint();
    			Thread.sleep(10);
     
    		}
    	}
     
    }

    public class Game {
     
    	Android android = new Android();
     
    	public void render (Graphics g) {
    		Font fnt0 = new Font("arial", Font.BOLD, 10);
    		g.setFont(fnt0);
    		g.setColor(Color.BLACK);
    		g.drawString("Hit: " + android.getTimesHit(), 50, 70);	// This doesn't work, it always says zero
    	}
     
    }

    public class Android {
     
    	public static BufferedImage android;
     
    	private int timesHit = 0;
    	private int androidX = 0;
    	private int androidY = 0;
     
    	public int getTimesHit() {
    		return timesHit;
    	}
     
    	public void setTimesHit(int timesHit) {
    		this.timesHit = timesHit;
    	}
     
     
    	public Android() {
    		try {
    			android = ImageIO.read(getClass().getResource("android.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}		
    	}
     
    	public void mousePressed(MouseEvent e) {
    		move();
    	}
     
    	void move() {
     
    		androidX = Main.width - android.getWidth();
    		androidY = Main.height - android.getHeight(); //The image gets outside the borders, what is wrong?
    		setTimesHit(getTimesHit() + 1);
    	}
     
    	public void render(Graphics g) {
    		g.drawImage(android, androidX, androidY, null);
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Getter from other class and border problem.

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Your technique for drawing (or painting) in a Swing app is not correct. Please review the lesson "Performing Custom Painting" for proper technique.

    After you've completed that lesson, if you still can't correct your code, please come back with your updated code, preferably something that we can compile and run to demonstrate any problems you're having. For example, use images from the Internet or tell us where we can find the same images you're using.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getter from other class and border problem.

    I have followed the tutorial but I am not sure how repaint() works. This is the code I have written:

    public class Main {
     
        public static void main(String[] args) {
     
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
     
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
            SwingUtilities.isEventDispatchThread());
            JFrame f = new JFrame("Swing Paint Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.add(new Panel());
            f.setSize(600, 600);
            f.setVisible(true);
        } 
     
    }

    class Panel extends JPanel {
     
        RedSquare redSquare = new RedSquare();
        ImageTest image = new ImageTest();
        Random random = new Random();
        Game game = new Game();
     
        public Panel() {
     
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    moveImage();
                }
            });      
        }
     
        private void moveImage(){
        	int nextX = random.nextInt(600 - image.getWidth());
        	int nextY = random.nextInt(600 - image.getHeight());
     
        	image.setxPos(nextX);
        	image.setyPos(nextY);
     
        	image.repaint();
     
        	System.out.println("Moved");
        }
     
        public void paintComponent(Graphics g) {
        	super.paintComponent(g);  
        	game.paintGame(g);
            image.paintImage(g);
        }  
    }

    class Game {
     
    	public void paintGame(Graphics g) {
    		Font fnt0 = new Font("arial", Font.BOLD, 10);
    		g.setFont(fnt0);
    		g.setColor(Color.BLACK);
    		g.drawString("Hit: ", 50, 70);
    	}
    }

    class ImageTest extends JPanel{
     
    	public static BufferedImage testImage;
     
    	public ImageTest() {
    		try {
    			testImage = ImageIO.read(getClass().getResource("image.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}		
     
    		width = testImage.getWidth();
    		height = testImage.getHeight();
    	}
     
        private int xPos = 0;
        private int yPos = 0;
        private int width = 0;
        private int height = 0;
     
        public int getxPos() {
    		return xPos;
    	}
     
    	public void setxPos(int xPos) {
    		this.xPos = xPos;
    	}
     
    	public int getyPos() {
    		return yPos;
    	}
     
    	public void setyPos(int yPos) {
    		this.yPos = yPos;
    	}
     
    	public int getWidth() {
    		return width;
    	}
     
    	public int getHeight() {
    		return height;
    	}
     
    	public void paintImage(Graphics g){
        	g.drawImage(testImage, xPos, yPos, null);
        }
     
    }

    I assume that I have to repaint game and image in order for it to work but how? Right now nothing is being redrawn.
    I want the image to move to a random position and the counter of hits to go up each time I click the screen.

    I have attached the image if you want to try the code.
    image.png

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getter from other class and border problem.

    I wrote repaint(); inside moveImage() so it appears to be working now.

        private void moveImage(){
        	int nextX = random.nextInt(600 - image.getWidth());
        	int nextY = random.nextInt(600 - image.getHeight());
     
            System.out.println("X: " + nextX + "  Y: " + nextY);
     
        	image.setxPos(nextX);
        	image.setyPos(nextY);
     
        	repaint();
     
        	System.out.println("Moved");
        }

    But I still have the initial problem with the image getting outside the borders.

    I set the size to be 600, 600 but in this picture the y position of the image is only 559. (The image is now a red square, 32x32)
    Last edited by suumpmolk; May 7th, 2014 at 01:32 PM.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Getter from other class and border problem.

    The coordinates (x, y) define the upper left corner of the square, so what you show is completely consistent with how graphics are drawn. If you want to limit the graphic to only appear as a whole object within the frame, then you'll have to modify the code so that no part of the square moves outside the frame. For example, y in the (x, y) coordinate cannot be any larger than the height of the frame - height of the square. In the case you've shown, y can be no larger than 600 - 32. Make sense?

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getter from other class and border problem.

    That is exactly what I have done. But as I show in the picture, it is still outside.
    600 - 32 = 568. If y or x is below 568 it should be inside the frame, correct? In the picture in my post above the Y is 559 which is less than 568 but it is still outside the border.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Getter from other class and border problem.

    Show updated code that provides the results you've shown.

Similar Threads

  1. Needing some help with array getter and setter methods!!
    By BlackShadow in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 5th, 2012, 07:11 PM
  2. Problem with border in program
    By kc120us in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 06:57 PM
  3. [SOLVED] Project help...confusion with changing a getter/setter for a driver class
    By coolidge in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2011, 11:08 PM
  4. What's wrong with my date getter?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 8th, 2010, 07:39 PM