Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2009, 03:48 AM
Member
 
Join Date: Jul 2009
Posts: 6
Rep Power: 0
roaan is on a distinguished road
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.

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 + "]";
    }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-05-2009, 05:22 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-05-2009, 10:23 AM
Member
 
Join Date: Jul 2009
Posts: 6
Rep Power: 0
roaan is on a distinguished road
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";
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-05-2009, 02:35 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-05-2009, 04:30 PM
Member
 
Join Date: Jul 2009
Posts: 6
Rep Power: 0
roaan is on a distinguished road
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

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 + "]";
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding multiple images to frame 435.mahesh AWT / Swing 1 04-24-2009 09:31 PM
adding image in a frame(urgent) arunkumarinfo NetBeans 0 02-23-2009 11:49 AM
Read/Write of components of frame window Harish kumara M AWT / Swing 0 09-17-2008 09:14 AM
Problem in adding Multiple Panels at the Specific positon on frame SANDY_INDIA AWT / Swing 7 07-09-2008 01:06 AM
Adding checkboxes on frame Java Tip Java Tips 0 12-21-2007 09:40 AM


All times are GMT +2. The time now is 04:36 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org