Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Help in creating grid points using java swing

    Hi all,

    I am new to java swing programming.I want to develop an application which has a panel with an array of grid points(not grid cells). The number of columns and rows of the grid points must be user specific.
    Once such an environment is created, the user's mouse click on the diff grid points must be captured and those points alone must be highlighted in a different color. As of now, i've only created the Jpanel.
    I know it is possible to get the coordinates of the mouse click use mouse events, but i do not know how to place the grid points in the environment and assign them each a co-ordinate so that i catch them back on user's mouse click.
    Since i am completely new into this, kindly elaborate me on how i proceed on with this.
    since the code ive developed so far, only creates the panel and the text box, i've not included it here.
    Forgive me if i didn't come out with the clear picture.
    Thanks in advance

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

    Default

    I would break the big problem down into little problems and try to solve each one in isolation. For instance, first I would solve the issue of user input by trying to get user input and create a two-dimensional array if int with the input and then print it to the console. Next I would try to use a two-dimensional array of Point to draw your grid (I suppose you will be drawing filled circles of a certain size, correct)? Then I would worry about adding a MouseListener that iterates through the array seeing if any clicks are reasonably close to a Point on the grid to suggest that the mouse clicked on it. Then I would try to change the grid point color on mouse click.

    So I suggest that you do just this -- one step at a time, and then if you get stuck on any specific step along the way, post your code and a detailed description of your problem and we'll probably be much better able to help you.

    Much luck!

  3. #3
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    Hey thanks a lot for you immediate response, thank you for guiding me through this.
    I somehow managed to do this.Ive posted my code, kindly post in your valuable suggestions if you feel i need to include any change.
    Short desc....
    Ive created the Jpanel in the main file env.java. Then for the purpose of creating grid points (small circles), ive called for the draww function in another class file and generated the total number of grid points each as an object of the class Circle.Ive assigned the center x and y coordinate for each circle adn incremented it by 50 for the next circle. The radius is taken as 5.
    Then for each click, i loop through the circle objects to find whether that click is within the radius of any circle and fill that circle accordingly.
    the file env.java is
    Java Code:
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.util.*;
    import java.math.*;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.image.*;
    import javax.imageio.ImageIO;
    
    public class env extends JFrame implements MouseListener,MouseMotionListener
    {
     JFrame f = new JFrame("dynamic environment");
    JPanel panel;
    JLabel rc1,cr1,sr1,ob1;
    JButton  rc,cr,sr,ob,tr;
    JTextField t1,t2,t3,t4,t5;
    int xco=0,yco=0;
    public static int m,n;
    public static int[][] recor = new int[100][2];
    Graphics g;
    paint p;
    public env()
    {
    p=new paint();
    f.setDefaultLookAndFeelDecorated(true);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setBounds(0,0,950,700);
    GUI();
    System.out.println("Before the mouse event");
    f.addMouseListener(this);
    f.setVisible(true);
    g=f.getGraphics();
    }
    
       public void mouseEntered(MouseEvent e) { }
    	public void mouseExited   ( MouseEvent e ){}
    	public void mousePressed  ( MouseEvent e ) {}
    	public void mouseDragged(MouseEvent e) {}
    	public void mouseMoved(MouseEvent e) { }
    	public void mouseReleased ( MouseEvent e ) {}
    
    
    
    
    public void mouseClicked(MouseEvent e) {
    System.out.println("Into the mouse event");
    xco=e.getX();
    yco=e.getY();
    System.out.println("The mouse events for x and y are "+xco+" "+yco);
    p.fillCircle(xco, yco,g);
    {
    
    }
    
    }
    
    
    
    
    
    public void GUI()
    {
    panel=new JPanel();
    panel.setLayout(new GridLayout(20, 1));
    panel.setSize(100,350);
    rc1 = new JLabel("Enter cols & rows");
    t1=new JTextField("");
    t2=new JTextField("");
    cr = new JButton("Create Environment");
    sr = new JButton("Locate Source");
    tr = new JButton("Locate Target");
    ob = new JButton("locate Obstacles");
    panel.add(rc1);
    panel.add(t1);
    panel.add(t2);
    panel.add(cr);
    panel.add(sr);
    panel.add(tr);
    panel.add(ob);
    f.getContentPane().add(panel,BorderLayout.EAST);
    f.getContentPane().add(new paint(),BorderLayout.WEST);
    cr.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
       m=Integer.parseInt(t1.getText());
       System.out.println("The rows are "+m);
     n=Integer.parseInt(t2.getText());
        System.out.println("The cols are "+n);
    p.draww(g,m,n);
    }
    });
    }
    public static void main(String args[])
        {
       new env();
        }
    }
    Then the other class file paint.java which includes the draww function is

    Java Code:
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.util.*;
    import java.math.*;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.image.*;
    import java.awt.geom.*;
    import javax.imageio.ImageIO;
    public class paint extends JPanel
    {
    public  Circle[] circle;
    static final int radius=5;
    public int previous_width=0,previous_height=0;
    paint()
        {
         }
    
    public void fillCircle(int x,int y,Graphics g){
    g.setColor(Color.red);
    System.out.println("Points received "+x+" "+y);
        for(Circle c:circle){
            if(c.containPoint(x, y)){
                System.out.println("Colouring");
                g.fillOval(c.getCenter_x()-radius, c.getCenter_y()-radius, radius*2, radius*2);
            }
        }
    
    }
    
    public void draww(Graphics g,int w,int h)
    {
    g.clearRect(20, 40,previous_width ,previous_height );
    circle=new Circle[h*w];
    g.setColor(Color.red);
    int center_x=25,center_y=45;
    int circle_counter=0;
    for(int i=0;i<h;i++){
        center_x=25;
        for(int j=0;j<w;j++,circle_counter++){
            g.drawOval(center_x-radius, center_y-radius, radius*2, radius*2);
            circle[circle_counter]=new Circle(center_x,center_y);
            center_x+=50;
        }
    center_y+=50;
    }
    }
    }
    The class file for the circle used is

    Java Code:
    public class Circle {
    static final int radius=5;
    private int center_x,center_y;
    Circle(int x,int y){
        this.center_x=x;
        this.center_y=y;
    
    }
    
        public int getCenter_x() {
            return center_x;
        }
    
        public void setCenter_x(int center_x) {
            this.center_x = center_x;
        }
    
        public int getCenter_y() {
            return center_y;
        }
    
        public void setCenter_y(int center_y) {
            this.center_y = center_y;
        }
    
        public boolean containPoint(int x,int y){
            int difference_x= (x-center_x)*(x-center_x);
            int difference_y=(y-center_y)*(y-center_y);
            int distance=difference_x+difference_y;
            int rad_squ=radius*radius;
            System.out.println("formula checker :"+distance);
            return (distance-rad_squ)<=0;
            
        }
    }
    Kindly suggest if i need to do something else.
    The next step is that i need to click on the grid point and then select the button "locate source". Once i do that, all the grid points which are colored in red needs to be joined using a line.It must be joined such that, each colored circle, joins to two other colored circles which are nearer to it out of the total colored circles,irrespective of the order we color select those circles. So that on the whole it gives some shape.
    To proceed on with this, i thought, for each colored circle, ill calculate its distance from all the colored circles and then join the two which are least colored.
    If there is something better or efficient than this kindly suggest.
    Thanks for your help.

Similar Threads

  1. Creating An Interactive Grid
    By Pnev in forum AWT / Swing
    Replies: 1
    Last Post: 11-22-2010, 09:58 AM
  2. creating a make grid method
    By Matt198525 in forum New To Java
    Replies: 13
    Last Post: 10-29-2010, 03:47 AM
  3. creating a Jtable using swing
    By hariza in forum AWT / Swing
    Replies: 6
    Last Post: 08-25-2010, 04:14 AM
  4. Replies: 2
    Last Post: 08-24-2008, 01:30 AM
  5. Help creating a changing grid
    By adlb1300 in forum New To Java
    Replies: 11
    Last Post: 10-24-2007, 02:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •