Results 1 to 2 of 2
  1. #1
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Resize JFrame to show components

    Hello guys,
    I have the following project:
    Java Code:
    package boxmatrics;
    
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    
    public class BoxMatrics {
        public static void main(String[] args) {
            
            JFrame f = new JFrame("BOXMATRICS");
            BoxContainer bc = new BoxContainer();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(bc);
            f.setSize(500,500);
            f.setVisible(true);
            
        }
        
    }
    Java Code:
    package boxmatrics;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JComponent;
    
    public class BoxContainer extends JComponent implements KeyListener{
        Box[][] b;
        private Image img =Toolkit.getDefaultToolkit().createImage("/home/flopet17/Desktop/2048_images/4.png");
        
        public BoxContainer() {
            addKeyListener(this);
            setFocusable(true);
        }
        
        @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.blue);
            if(b == null)
                initB();
            for(int i=0; i<4;i++)
                for(int j=0;j<4;j++)
                {
                        b[i][j].piece(g2);
                }
        }
        private int x=0,y=0;
        private void initB() {
            b = new Box[4][4];
            for(int i=0; i<4;i++)
                for(int j=0;j<4;j++)
                {
                    
                    Rectangle2D.Double r = new Rectangle2D.Double(x+i*110,y+j*110,100,100);
                    b[i][j] = new Box(x+i*100,y+j*100,img);
                }
            System.out.println("bla");
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
        
        }
    
        @Override
        public void keyPressed(KeyEvent ke) {
            int code = ke.getKeyCode();
            if(code == KeyEvent.VK_UP){
                System.out.println("UP");
            }
            if(code == KeyEvent.VK_DOWN){
                System.out.println("DOWN");
            }
        
            if(code == KeyEvent.VK_LEFT){
                System.out.println("LEFT");
            }
        
            if(code == KeyEvent.VK_RIGHT){
                System.out.println("RIGHT");
            }
            img =Toolkit.getDefaultToolkit().createImage("/home/flopet17/Desktop/2048_images/2.png");
            initB();
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
        }
    }
    Java Code:
    package boxmatrics;
    
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JComponent;
    
    public class Box extends JComponent{
        private final int x,y;
        Rectangle2D.Double rect;
        Image img;
        public Box(int x,int y, Image img){
            this.x=x;
            this.y=y;
            this.img = img;
        }
        
        public void piece(Graphics2D g2) {
            g2.drawImage(img,x,y,this);
        }
    }
    Why I have to resize the window(JFrame window) manually, with the mouse, in order to see the components?
    Where do I need to call repaint(); ?

    Thank you

    P.S. Problem Solved
    Last edited by Flopet17; 04-09-2014 at 02:36 AM. Reason: Problem Solved

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

    Default Re: Resize JFrame to show components

    HOW did you solve it? That is useful information for people who find this thread through a search.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. FrameView doesn't show new JPanel until I resize
    By metazone in forum SWT / JFace
    Replies: 3
    Last Post: 01-05-2012, 04:52 PM
  2. Replies: 11
    Last Post: 09-18-2011, 01:01 PM
  3. Resize shell on show/hide controls.
    By spacetoha in forum SWT / JFace
    Replies: 1
    Last Post: 03-24-2009, 01:20 PM
  4. Inconsistent layout w/dynamic resize of components
    By donb2000 in forum AWT / Swing
    Replies: 3
    Last Post: 07-26-2008, 03:40 PM
  5. Replies: 1
    Last Post: 06-06-2008, 08:55 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
  •