Results 1 to 4 of 4
  1. #1
    Amanoo is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Resizing of frames doesn't work.

    Hello. I am building a GUI which is to be used for a simple game. I have some panel classes that make certain panels with certain properties. However, when I try to add a frame into another, it gets really small. I don't seem to be able to resize and move it in any possible way. Can anyone tell me how I should do it?

    A panel, quite similar in function to a button:
    Java Code:
    package GUI;
    
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.LayoutManager;
    import java.awt.Toolkit;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    
    import javax.swing.JPanel;
    
    /**
     * Simple program that loads, rotates and displays an image. Uses the file
     * Duke_Blocks.gif, which should be in the same directory.
     * 
     * @author MAG
     * @version 20Feb2009
     */
    
    @SuppressWarnings("serial")
    public class TilePanel extends JPanel {
    
    	Image image;
    	private String color;
    
    	public TilePanel() {
    		super();
    		color = "empty";
    		image = Toolkit.getDefaultToolkit().getImage("empty.png");
    		this.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent me) {
    				System.out.println("CLICKED tilepanel");
    			}
    		});
    
    	}
    
    	public TilePanel(LayoutManager layout) {
    		super(layout);
    		color = "empty";
    		image = Toolkit.getDefaultToolkit().getImage("empty.png");
    		this.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent me) {
    				System.out.println("CLICKED tilepanel");
    			}
    		});
    	}
    
    	public void paintComponent(Graphics g) {
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    	}
    
    	/**
    	 * @require color.equals("yellow") || color.equals("green") ||
    	 * color.equals("red") || color.equals("blue") || color.equals("empty")
    	 */
    	public void setColor(String color) {
    		this.color = color;
    		image = Toolkit.getDefaultToolkit().getImage(color + ".png");
    	}
    
    	public String getColor() {
    		return color;
    	}
    
    }
    This one is used by this frame, which will in turn be a big part of the playing board:
    Java Code:
    package GUI;
    
    //Import the basic graphics classes.
    import java.awt.*;
    import java.math.*;
    import javax.swing.*;
    
    public class BlockPanel extends JPanel {
    
    	Image image;
    	public BlockPanel() {
    		super();
    		image = Toolkit.getDefaultToolkit().getImage("wood.gif");
    
    	}
    
    	public void paintComponent(Graphics g) {
    		Graphics2D g2d = (Graphics2D) g;
    		// g2d.translate(170, 0); // Translate the center of our coordinates.
    		g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    	}
    
    	public void rotates() {
    
    	}
    
    	public static void main(String arg[]) {
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800, 600);
    
    		BlockPanel panel = new BlockPanel();
    		frame.setContentPane(panel);
    		frame.setVisible(true);
    
    		panel.rotates();
    		TilePanel tilePanel = new TilePanel(new FlowLayout());
    		tilePanel.setColor("green");
    		frame.add(tilePanel);
    		frame.setVisible(true);
    	}
    }
    wood.gif is just a texture of some wood, and there were empty.png, green.png, red.png,blue.png and yellow.png, which were all just differently coloured circles. Preferably I wouldn't want to bind these to specific locations, that is, use a layout to place them, and I'd like to set their coördinates and size instead. This is because the TilePanels need to move around, and by editing the coordinates in a while loop I could more easily animate this process (this is not the most important part of things, though, because only the begin positions and end positions count).

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Resizing of frames doesn't work.

    Quote Originally Posted by Amanoo View Post
    when I try to add a frame into another
    That can't be done. What are you really doing?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Amanoo is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Resizing of frames doesn't work.

    Sorry, I meant add a.JPanel into another. I always confuse the two words for some reason.
    My current code can do that, but the methods for resizing don't seem to work. I just end up with a really small panel inside of a decently sized one. To be exact, I am trying to add a TilePanel to a Blockpanel, the two of which I have defined up here.
    Last edited by Amanoo; 03-16-2012 at 01:17 PM. Reason: typo

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Resizing of frames doesn't work.

    Most layout managers respect the preferredSize. Does your TilePanel know what size it would like to be?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 06:22 PM
  2. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 09:07 AM
  3. 4 frames work toghether
    By Shima in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-09-2010, 04:14 AM
  4. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 11:33 PM
  5. will java webBrowser work with frames
    By elipford in forum New To Java
    Replies: 2
    Last Post: 06-04-2007, 04:26 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
  •