Results 1 to 13 of 13
  1. #1
    iamchoilan is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default Two problem for my rssi calculation program

    I have downloaded a program, which can calculate rssi signal on a grid sheet.

    1. I want to build a button, when I press the button, it pop up another window which show a graph on the data the program just calculated.

    Can this be done ?

    2. I want to insert a image to be the background of the grids, instead of that piece of white sheet, can this be done ?


    Here is my codes in the attachment.
    Attached Files Attached Files

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    1. I want to build a button, when I press the button, it pop up another window which show a graph on the data the program just calculated.

    Can this be done ?
    Yes. You can find out about buttons, listeners and dialogs here: Trail: Creating a GUI with JFC/Swing. For the grid and plotting data use a JPanel, override the/its paintComponent method and do your custom drawing in it. For more about drawing see the last link (Performing Custom Painting) on the page (link above) and also the first link under the Other UI-Related Trails header, viz, "2D Graphics".

    2. I want to insert a image to be the background of the grids, instead of that piece of white sheet, can this be done ?

    Yes. Draw the background image first and then draw your grid.

  3. #3
    iamchoilan is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    Yes. You can find out about buttons, listeners and dialogs here: Trail: Creating a GUI with JFC/Swing. For the grid and plotting data use a JPanel, override the/its paintComponent method and do your custom drawing in it. For more about drawing see the last link (Performing Custom Painting) on the page (link above) and also the first link under the Other UI-Related Trails header, viz, "2D Graphics".

    Yes. Draw the background image first and then draw your grid.
    I want that graph to pop up in another window though :)


    How do I draw that background image into the program ?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I want that graph to pop up in another window though
    Launch a JDialog and add the graphic component (JPanel) to it.
    Somewhere in_or_called_from_your_event_code:
    Java Code:
    GraphicPanel graphicPanel = new GraphicPanel(image, data);
    JDialog dialog = new JDialog(desired_args);
    dialog.add(graphicPanel);
    // configure dialog as desired
    dialog.setVisible(true);
    How do I draw that background image into the program ?
    Java Code:
    class GraphicPanel extends JPanel {
        BufferedImage bgImage;
        int[][] data;
    
        GraphicPanel(BufferedImage image, int[] data) {
            bgImage = image;
            this.data = data;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = 10;
            int y = 10;
            g.drawImage(bgImage, x, y, this);
            // draw your grid lines...
            // draw your data points/lines as desired...
        }
    }

  5. #5
    iamchoilan is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    Oh there is still error, can you please have a look in the program ?
    Sorry I am new to java hope someone can solve my problem here...

    Q1. Here is where I want to plot graph.

    Java Code:
    jButton3.setBounds(new Rectangle(8, 5, 70, 22));
    		jButton3.setMargin(new Insets(0, 0, 0, 0));
    		jButton3.setText("Plot");
    		jButton3.addActionListener(new MainSim_jButton3_actionAdapter(this));
    I build a button here, but,

    Java Code:
    class MainSim_jButton3_actionAdapter
        implements ActionListener {
        
    }
    This class is still empty I do not know how to implement the plotting thing...


    Q2. Here is where I want to insert the image.

    Java Code:
    public void paint(Graphics g) {
            g.setColor(Color.white);
            g.fillRect(0, 0, getWidth(), getHeight());
    
    //Here it draw the white part.
    
            g.setColor(Color.gray);
            if (fpPoints != null && fpPoints.length > 0) {
                for (int i = 0; i < fpPoints.length; i++) {
                    g.drawLine(0, pix_grid * i, getWidth(), pix_grid * i);
                }
                for (int j = 0; j < fpPoints[0].length; j++) {
                    g.drawLine(pix_grid * j, 0, pix_grid * j, getHeight());
                }
            }
    
    //And here it draw the gray lines.
    
            super.paint(g);
            String p = "";
            Color cp = Color.black;
            java.util.Random r = new java.util.Random(10);
            if (highlight != null) {
                for (int i = 0; i < highlight.size(); i++) {
                    FpPoint fpp1 = (FpPoint) highlight.get(i);
                    if (!p.equalsIgnoreCase(fpp1.getFPRss())) {
                        //g.setColor(new Color((int)(Math.random()*2)*255, (int)(Math.random()*2)*255, (int)(Math.random()*2)*255));
                        g.setColor(new Color( (int) (r.nextFloat() * 2) * 255, (int) (r.nextFloat() * 2) * 255, (int) (r.nextFloat() * 2) * 255));
                        if (g.getColor().equals(Color.white)) {
                            g.setColor(Color.gray);
                        }
                        p = fpp1.getFPRss();
                    }
                    //g.fillOval(MainSim.m2pixel(fpp1.getPointYm()) - 4, MainSim.m2pixel(fpp1.getPointXm()) - 4, 8, 8);
                    g.fillOval(MainSim.m2pixel(fpp1.getPointYm()) - pix_grid / 2, MainSim.m2pixel(fpp1.getPointXm()) - pix_grid / 2, pix_grid, pix_grid);
                }
            }
    
            if ( apsdata != null ){
                for ( int i = 0 ; i < apsdata.size() ; i ++){
                    ApsData ad = (ApsData) apsdata.get(i);
                    g.setColor(Color.black);
                    g.fillRect(MainSim.m2pixel(ad.getXm()), MainSim.m2pixel(ad.getYm()), 26, 16);
                    g.setColor(Color.white);
                    g.drawString(String.valueOf(ad.getRssiAvg()), MainSim.m2pixel(ad.getXm())+3, MainSim.m2pixel(ad.getYm()) + 13);
                }
            }
        }
    Last edited by iamchoilan; 04-24-2008 at 09:15 PM.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class SimTest implements ActionListener {
        Random seed = new Random();
        JDialog dialog;
    
        public void actionPerformed(ActionEvent e) {
            int[] data = getData();
            DataPanel dataPanel = new DataPanel(data);
            if(dialog == null) {
                dialog = new JDialog(new Frame(), "data plot", false);
                dialog.setSize(400,400);
                dialog.setLocationRelativeTo(null);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            }
            dialog.add(dataPanel);
            dialog.setVisible(true);
        }
    
        private int[] getData() {
            int[] data = new int[8];
            for(int i = 0; i < data.length; i++) {
                data[i] = seed.nextInt(101);
            }
            return data;
        }
    
        private JPanel getContent() {
            JButton button = new JButton("test");
            button.addActionListener(this);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(button, new GridBagConstraints());
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new SimTest().getContent());
            f.setSize(200,75);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class DataPanel extends JPanel {
        int[] data;
        final int PAD = 20;
    
        public DataPanel(int[] data) {
            this.data = data;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            // Draw ordinate.
            g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
            // Draw abcissa.
            g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
            double xInc = (double)(w - 2*PAD)/(data.length-1);
            double scale = (double)(h - 2*PAD)/getMax();
            // Draw lines.
            g2.setPaint(Color.blue);
            for(int i = 0; i < data.length-1; i++) {
                double x1 = PAD + i*xInc;
                double y1 = h - PAD - scale*data[i];
                double x2 = PAD + (i+1)*xInc;
                double y2 = h - PAD - scale*data[i+1];
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            }
            // Mark data points.
            g2.setPaint(Color.red);
            for(int i = 0; i < data.length; i++) {
                double x = PAD + i*xInc;
                double y = h - PAD - scale*data[i];
                g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
            }
        }
    
        private int getMax() {
            int max = -Integer.MAX_VALUE;
            for(int i = 0; i < data.length; i++) {
                if(data[i] > max)
                    max = data[i];
            }
            return max;
        }
    }

  7. #7
    iamchoilan is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks a lot, now I have to fit the data in it, where should the data go in the graph ?


    And how do I implement the second question ?

    Thanks.
    Last edited by iamchoilan; 04-25-2008 at 06:07 PM.

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    jButton3.addActionListener(new SimTest(this));
    From the looks of this line the error would likely say that the jvm cannot find a constructor in the SimTest class that takes an argument of the type referred to by the this keyword. If I try compiling
    Java Code:
    SimTest simTest = new SimTest("Hello World");
    I get
    Java Code:
    C:\jexp>javac test.java
    test.java:6: cannot find symbol
    symbol  : constructor SimTest(java.lang.String)
    location: class SimTest
            SimTest simTest = new SimTest("Hello World");
                              ^
    1 error
    The jvm cannot find a SimTest constructor that takes a String argument.
    The SimTest class (post #6) has no explicit constructor so the jvm provides a default, no–argument constructor.

    And how do I implement the second question ?
    From the original post #1:
    2. I want to insert a image to be the background of the grids, instead of that piece of white sheet, can this be done ?
    or from #5
    Q2. Here is where I want to insert the image.
    Sorry, I forgot about the image.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.util.Random;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class SimTest implements ActionListener {
        BufferedImage image;
        Random seed = new Random();
        JDialog dialog;
    
        public SimTest(BufferedImage image) {
            this.image = image;
        }
    
        public void actionPerformed(ActionEvent e) {
            int[] data = getData();
            DataPanel dataPanel = new DataPanel(data, image);
            if(dialog == null) {
                dialog = new JDialog(new Frame(), "data plot", false);
                dialog.setSize(400,400);
                dialog.setLocationRelativeTo(null);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            }
            dialog.add(dataPanel);
            dialog.setVisible(true);
        }
    
        private int[] getData() {
            int[] data = new int[8];
            for(int i = 0; i < data.length; i++) {
                data[i] = seed.nextInt(101);
            }
            return data;
        }
    
        private JPanel getContent() {
            JButton button = new JButton("test");
            button.addActionListener(this);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(button, new GridBagConstraints());
            return panel;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/hawk.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new SimTest(image).getContent());
            f.setSize(200,75);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class DataPanel extends JPanel {
        int[] data;
        BufferedImage image;
        final int PAD = 20;
    
        public DataPanel(int[] data, BufferedImage image) {
            this.data = data;
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            // Draw image centered.
            int x0 = (w - image.getWidth())/2;
            int y0 = (h - image.getHeight())/2;
            g2.drawImage(image, x0, y0, this);
            // Draw ordinate.
            g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
            // Draw abcissa.
            g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
            double xInc = (double)(w - 2*PAD)/(data.length-1);
            double scale = (double)(h - 2*PAD)/getMax();
            // Draw lines.
            g2.setPaint(Color.blue);
            for(int i = 0; i < data.length-1; i++) {
                double x1 = PAD + i*xInc;
                double y1 = h - PAD - scale*data[i];
                double x2 = PAD + (i+1)*xInc;
                double y2 = h - PAD - scale*data[i+1];
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            }
            // Mark data points.
            g2.setPaint(Color.red);
            for(int i = 0; i < data.length; i++) {
                double x = PAD + i*xInc;
                double y = h - PAD - scale*data[i];
                g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
            }
        }
    
        private int getMax() {
            int max = -Integer.MAX_VALUE;
            for(int i = 0; i < data.length; i++) {
                if(data[i] > max)
                    max = data[i];
            }
            return max;
        }
    }

  9. #9
    iamchoilan is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks a lot, I think the first Question solved now when I click on the button it pop up a graph, but I need it to get my data and plot according to it.


    But for Question 2, I think I did not make it clear enough, I want the image behind the grid of my program, but not the graph...

    About the grid of my program, here is it.

    Java Code:
    public void paint(Graphics g) {
            g.setColor(Color.white);
    //Here it draw the white background sheet of it, and I want to change this to an image.
    
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.gray);
    //Here it put in the gray grids.
    
            if (fpPoints != null && fpPoints.length > 0) {
                for (int i = 0; i < fpPoints.length; i++) {
                    g.drawLine(0, pix_grid * i, getWidth(), pix_grid * i);
                }
                for (int j = 0; j < fpPoints[0].length; j++) {
                    g.drawLine(pix_grid * j, 0, pix_grid * j, getHeight());
                }
            }

    I don't know if this is clear enough, sorry that I am pretty new to java. I attached the whole thing I am working on here, please have a look.

    Thanks a lot.
    Attached Files Attached Files

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public void paint(Graphics g) {
        //Here it draw the white background,
        // and I want to change this to an image.
    //    g.setColor(Color.white);
    //    g.fillRect(0, 0, getWidth(), getHeight());
        // Okay, then draw the image at this place in your code:
        g.drawImage(yourImage, x, y, this);
    
        g.setColor(Color.gray);
        //Here it put in the gray grids.
        if (fpPoints != null && fpPoints.length > 0) {
            for (int i = 0; i < fpPoints.length; i++) {
                g.drawLine(0, pix_grid * i, getWidth(), pix_grid * i);
            }
            for (int j = 0; j < fpPoints[0].length; j++) {
                g.drawLine(pix_grid * j, 0, pix_grid * j, getHeight());
            }
        }
    For custom drawing it is better to override the paintComponent method as recommended in the JComponent class api (see the paint method).
    If you choose to do so be sure to call super.paintComponent(g) before drawing. This will cause the background to be filled with the background color and will avoid artifacts. This is not necessary when overriding the paint method.

    The basic idea in drawing is to draw the bottom layer first and the top layer last. You can draw yout image followed by your grid and the grid will appear between your eye and the image.

    I attached the whole thing I am working on here, please have a look.
    I looked at the first zip file but was unable to do much with it.

  11. #11
    iamchoilan is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    HallArea.java:169: cannot find symbol
    symbol : variable ff
    location: class fpsim.HallArea
    g.drawImage(ff.jpeg, 0, 0, this);


    while my image is ff.jpeg, in the same folder...
    :(

  12. #12
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    g.drawImage([b][COLOR="Red"]ff.jpeg[/COLOR][/b], 0, 0, this);
    Note how the image was loaded in post #8: use ImageIO to read a file constructed from/with the image path. This will return a BufferedImage which you can pass to the drawImage method. You must load the image before you can draw it.
    Java Code:
    classs Pseudo extends JPanel }
        BufferedImage image;
    
        public Pseudo() {
            String pathToImage = "ff.jpg";
            try {
                image = ImageIO.read(new File(pathToImage));
            } catch(IOException e) {
                // recover
            }
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }
    }

  13. #13
    iamchoilan is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    One last question.
    First please read the piece of attached code.

    I attached your plot graph method into it, it runs smoothly, thanks.

    Now here is the question.

    I want it to show a graph that contain the number of appearance of null/very strong/strong/normal/weak/very weak record.

    So I put in 6 in the 421 row, stating that there is 6 record to present in the graph.

    Next I want the data read into the array be the numbers of 'null' appears, numbers of 'very strong' appears, etc. So basically I want to put in a count++ thing into the methods in FpPoint.java somewhere around row 82 to row 143, and then the method will return all the counts and then be put into the array of the plot graph method.

    I still need some help to implement this final question about the plot graph method.

    Thanks a lot.
    Attached Files Attached Files

Similar Threads

  1. Problem with my first Struts program....please help me
    By sireesha in forum Web Frameworks
    Replies: 5
    Last Post: 10-16-2011, 05:19 PM
  2. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 03:42 PM
  3. Problem with my program HelloWorld
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:32 PM
  4. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 04:08 PM
  5. getting problem in compiling java program?
    By sathish04021984 in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:26 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
  •