Results 1 to 5 of 5
  1. #1
    roaan is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default adding components to frame

    Hi ,

    I found an application for my project on the net and want to tweak it in that i want to add an image and a graph to a frame. But what is happening currently is that whenever i add image the graph disappears. How do i use the layout manager for frames in java.

    Java Code:
    import java.awt.*;
    import java.awt.Rectangle;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class SquareGrid extends JPanel {
        SquareRx[][] squares;
        final int PAD = 20;
        final int ROWS = 5;
        final int COLS = 7;
        BufferedImage image;
    
        public SquareGrid() {
            addMouseListener(ml);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            if(squares == null) {
                initSquares();
            }
            // Draw squares.
            g2.setPaint(Color.blue);
            for(int i = 0; i < ROWS; i++) {
                for(int j = 0; j < COLS; j++) {
                    squares[i][j].draw(g2);
                }
            }
        }
    
        private void initSquares() {
            squares = new SquareRx[ROWS][COLS];
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/COLS;
            double yInc = (double)(h - 2*PAD)/ROWS;
            for(int i = 0; i < ROWS; i++) {
                double y = PAD + i*yInc;
                for(int j = 0; j < COLS; j++) {
                    double x = PAD + j*xInc;
                    Rectangle2D.Double r =
                        new Rectangle2D.Double(x, y, xInc, yInc);
                    squares[i][j] = new SquareRx(i, j, r);
                }
            }
        }
    
        private MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                if(!isInGrid(p)) return;
                double xInc = (double)(getWidth() - 2*PAD)/COLS;
                double yInc = (double)(getHeight() - 2*PAD)/ROWS;
                int row = (int)((p.y-PAD)/yInc);
                int col = (int)((p.x-PAD)/xInc);
                System.out.println(squares[row][col]);
                boolean isSelected = squares[row][col].isSelected();
                squares[row][col].setSelected(!isSelected);
                repaint();
            }
        };
    
        private boolean isInGrid(Point p) {
            Rectangle r = getBounds();
            r.grow(-PAD, -PAD);
            return r.contains(p);
        }
        
        public void ShowImage() {
            try {
          String imageName="C:/Documents and Settings/ROHAN/Desktop/RG.jpg";
          File input = new File(imageName);
              image = ImageIO.read(input);
            } catch (IOException ie) {
              System.out.println("Error:"+ie.getMessage());
            }
          }
    
    
        public static void main(String[] args) {
            SquareGrid test = new SquareGrid();
            JFrame f = new JFrame("RoboGuard RoutePlanner");
            Panel panel = new ShowImage();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            //f.getContentPane().add(panel);
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
            test.addComponentListener(test.cl);
        }
    
        private ComponentListener cl = new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                squares = null;
                repaint();
            }
        };
    }
    
    class SquareRx {
        private final int row;
        private final int col;
        Rectangle2D.Double rect;
        Color color = new Color(140,200,160);
        Color bgColor = Color.orange;
        Color selColor = Color.red;
        private boolean selected = false;
    
        public SquareRx(int r, int c, Rectangle2D.Double rect) {
            row = r;
            col = c;
            this.rect = rect;
        }
    
        public void draw(Graphics2D g2) {
            g2.setPaint(selected ? selColor : bgColor);
            g2.fill(rect);
            g2.setPaint(color);
            g2.draw(rect);
        }
    
        public void setSelected(boolean selected) {
            this.selected = selected;
        }
    
        public boolean isSelected() { return selected; }
    
        public String toString() {
            return "SQUARE[row:" + row + ", col:" + col +
                        ", selected:" + selected + "]";
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by roaan View Post
    How do i use the layout manager for frames in java.
    Please have a look here: How to use Layout Managers

    edit: the current code as posted does not compile for us, so it is hard to test.
    Last edited by Fubarable; 07-05-2009 at 05:32 AM.

  3. #3
    roaan is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    Oh i think its because it needs an image location when you try to compile at the point

    String imageName="C:/Documents and Settings/ROHAN/Desktop/alien.jpeg";

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No, one reason is because you don't post the code for the ShowImage class which appears to extend Panel (which suggests you are doing something else bad -- mixing AWT and Swing components).

    Also, please be upfront when cross-posting the same question in multiple fora. Many can get upset if they put time and effort trying to answer a question here (all as volunteers mind you) only to find that the same answer has been given hours ago in a cross-post. Please help prevent that from happening by providing links to all of your cross-posts. Thanks.

  5. #5
    roaan is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    Okay this is the original code. It just displays a grid. I want to add an image to it as well. The layout should be such that the grid is at top and image below it.

    Also posted at:
    forums.sun.com/thread.jspa?threadID=5395747&tstart=0

    Java Code:
    import java.awt.*;
    import java.awt.Rectangle;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class SquareGrid extends JPanel {
        SquareRx[][] squares;
        final int PAD = 10;
        final int ROWS = 5;
        final int COLS = 7;
        BufferedImage image;
    
        public SquareGrid() {
            addMouseListener(ml);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            if(squares == null) {
                initSquares();
            }
            // Draw squares.
            g2.setPaint(Color.blue);
            for(int i = 0; i < ROWS; i++) {
                for(int j = 0; j < COLS; j++) {
                    squares[i][j].draw(g2);
                }
            }
        }
    
        private void initSquares() {
            squares = new SquareRx[ROWS][COLS];
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/COLS;
            double yInc = (double)(h - 2*PAD)/ROWS;
            for(int i = 0; i < ROWS; i++) {
                double y = PAD + i*yInc;
                for(int j = 0; j < COLS; j++) {
                    double x = PAD + j*xInc;
                    Rectangle2D.Double r =
                        new Rectangle2D.Double(x, y, xInc, yInc);
                    squares[i][j] = new SquareRx(i, j, r);
                }
            }
        }
    
        private MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                if(!isInGrid(p)) return;
                double xInc = (double)(getWidth() - 2*PAD)/COLS;
                double yInc = (double)(getHeight() - 2*PAD)/ROWS;
                int row = (int)((p.y-PAD)/yInc);
                int col = (int)((p.x-PAD)/xInc);
                System.out.println(squares[row][col]);
                boolean isSelected = squares[row][col].isSelected();
                squares[row][col].setSelected(!isSelected);
                repaint();
            }
        };
    
        private boolean isInGrid(Point p) {
            Rectangle r = getBounds();
            r.grow(-PAD, -PAD);
            return r.contains(p);
        }
        
    
        public static void main(String[] args) {
            SquareGrid test = new SquareGrid();
            JFrame f = new JFrame("RoboGuard RoutePlanner");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
            test.addComponentListener(test.cl);
        }
    
        private ComponentListener cl = new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                squares = null;
                repaint();
            }
        };
    }
    
    class SquareRx {
        private final int row;
        private final int col;
        Rectangle2D.Double rect;
        Color color = new Color(140,200,160);
        Color bgColor = Color.black;
        Color selColor = Color.red;
        private boolean selected = false;
    
        public SquareRx(int r, int c, Rectangle2D.Double rect) {
            row = r;
            col = c;
            this.rect = rect;
        }
    
        public void draw(Graphics2D g2) {
            g2.setPaint(selected ? selColor : bgColor);
            g2.fill(rect);
            g2.setPaint(color);
            g2.draw(rect);
        }
    
        public void setSelected(boolean selected) {
            this.selected = selected;
        }
    
        public boolean isSelected() { return selected; }
    
        public String toString() {
            return "SQUARE[row:" + row + ", col:" + col +
                        ", selected:" + selected + "]";
        }
    }

Similar Threads

  1. Adding multiple images to frame
    By 435.mahesh in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2009, 09:31 PM
  2. adding image in a frame(urgent)
    By arunkumarinfo in forum NetBeans
    Replies: 0
    Last Post: 02-23-2009, 11:49 AM
  3. Read/Write of components of frame window
    By Harish kumara M in forum AWT / Swing
    Replies: 0
    Last Post: 09-17-2008, 09:14 AM
  4. Replies: 7
    Last Post: 07-09-2008, 01:06 AM
  5. Adding checkboxes on frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 09:40 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
  •