Results 1 to 17 of 17
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By masijade

Thread: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Arrow scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Hi,
    I've made the graph, it works alright for input file with small number of lines(a sample file with 16 lines), but shows a dark line for big files(my file for project work of my college which consists of 65,000 lines)

    Java Code:
    public void paintComponent(Graphics 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 abscissa.
            g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
            // Draw labels.
            Font font = g2.getFont();
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics("0", frc);
            float sh = lm.getAscent() + lm.getDescent();
            // Ordinate label.
            String s = "data";
            float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
            for(int i = 0; i < s.length(); i++) {
                String letter = String.valueOf(s.charAt(i));
                float sw = (float)font.getStringBounds(letter, frc).getWidth();
                float sx = (PAD - sw)/2;
                g2.drawString(letter, sx, sy);
                sy += sh;
            }
            s = "x axis";
            sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
            float sw = (float)font.getStringBounds(s, frc).getWidth();
            float sx = (w - sw)/2;
            g2.drawString(s, sx, sy);
            double xInc = (double)(w - 2*PAD)/(this.xArray.length-1);
            double scale = (double)(h - 2*PAD)/getMax();
            g2.setPaint(Color.green.darker());
            for(int i = 0; i < this.yArray.length-1; i++) {
                double x1 = PAD + i*xInc;
                double y1 = h - PAD - scale*((this.yArray[i]));  //for yArray, the array size = number of lines of the input file, 
                                                                                //and it is working fine with scanning the input file and plotting them
                double x2 = PAD + (i+1)*xInc;
                double y2 = h - PAD - scale*this.yArray[i+1];
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            }
            // Mark data points.
            g2.setPaint(Color.red);
            
    //////////////////////////////////////////////////////////////////////////////////////////
    	}
    	
    	private double getMax() {
    	       double max = -Integer.MAX_VALUE;
    	        for(int i = 0; i < this.yArray.length; i++) {
    	            if(this.yArray[i] > max)
    	                max = this.yArray[i];
    	        }
    	        return max;
    	    }
    output graph for a sample file with 16 lines sample.bmp
    output graph for original input file with 63374 values originalfile.bmp
    So, I need the graph scrollable with , maybe a JSlider, I guess,
    I'm ready to post my full codings if needed

    thx
    dhilip
    Last edited by noobplus; 03-19-2012 at 01:18 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    See JScrollPane

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Quote Originally Posted by masijade View Post
    See JScrollPane
    hi,
    where do we add "JScrollPane scroll = new JScrollPane();"?
    (think so, I should add it into "void paintComponent(Graphics g)" and pretty sure, I should add the ScrollPane into the frame "frame.add(scroll);" )
    and how should we attach the graph drawn over the scaled values into the scrollpane we have created?

    thx
    dhilip

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Your graph component (the one with the paintComponent() method in it above) would be placed inside the JScrollPane.
    I'd suggest reading up on the JScrollPane tutorial at Oracle.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Quote Originally Posted by Tolls View Post
    Your graph component (the one with the paintComponent() method in it above) would be placed inside the JScrollPane.
    Hi,
    Thx for the link,
    Quote Originally Posted by Tolls View Post
    would be placed inside the JScrollPane.
    Does that mean, "JScrollPane jsp=new JScrollPane(); jsp.add(paintComponent());"?
    that 1 didn't work anyway.
    Could u demonstrate a small one plz

    thx
    dhilip

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    If you don't know how Swing works, and how Components are added together to make a GUI, then I would suggest going through the whole Swing tutorial, but not the Netbeans GUI builder bits.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Quote Originally Posted by Tolls View Post
    If you don't know how Swing works, and how Components are added together to make a GUI, then I would suggest going through the whole Swing tutorial, but not the Netbeans GUI builder bits.
    if u tell me how to attach the graphComponent to the JScrollPane, it would help my project

    thx
    dhilip

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    What does this, from the API, tell you

    Java Code:
    JScrollPane(Component view)

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    And, as a corollary to that what does this, also from the API, tell you
    Java Code:
    setViewportView(Component view)

  10. #10
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Quote Originally Posted by masijade View Post
    What does this, from the API, tell you

    Java Code:
    JScrollPane(Component view)
    JScrollPane(Component view)
    I susbstituted "JScrollPane(paintComponent **)"
    It asks me to enter an argument

    hence i could not proceed to setting view-port
    ETA: API soz what means API? I'm using eclipse
    thx
    dhiliip

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    paintComponent? As in the method? If so, whatever made you think you could use a method name as an argument? You use the the actual graph component object as the argument, of course.

  12. #12
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Quote Originally Posted by masijade View Post
    paintComponent? As in the method? If so, whatever made you think you could use a method name as an argument? You use the the actual graph component object as the argument, of course.
    maybe I should introduce a new argument as 'g' in 'paintComponent(Graphics g)'
    example: JScrollPane(Component v)

    but i think the paintComponent() can be added to ScrollPane in some ways, cause it ran without errors

    thx
    dhilip

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    You don't need to go through the Swing tutorials, yet, you need to go through the basic Java tutorials.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    You cannot expect to write Java code at this level (and Swing is not a starting point for writing Java) if you cannot read a method signature.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    hi,
    my project submission date is on March last week, I'd love somebody help me out

    thx
    dhilip

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    I assume the "graph" is part of either a panel or a label, right? That entire panel/label is what is used as the argument to the constructor (or method setViewPointView). Then place the scrollpane onto the gui rather than the original component and continue as before.
    noobplus likes this.

  17. #17
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: scrollable graph on JFrame. (I've made the graph and I need it scrollable)

    Quote Originally Posted by masijade View Post
    I assume the "graph" is part of either a panel or a label, right? That entire panel/label is what is used as the argument to the constructor (or method setViewPointView). Then place the scrollpane onto the gui rather than the original component and continue as before.
    thx,
    I got that..
    So, the class extending the JPanel or JLabel should be the argument for the constructor(I got it that constructor means, 'JScrollPane jsp=new JScrollPane(*argument*)')
    methods cannot be arguments,
    I've added the main class name as argument to JScrollPane,
    Java Code:
     jsp.setViewPortView(*class_name*);
    and

    I added JScrollPane to a frame's constructor as an argument [ETA: sorry not as an argument, I added it using frame.add();]

    everything ran well,
    thanks

    regards
    dhilip
    Last edited by noobplus; 03-21-2012 at 11:49 AM.

Similar Threads

  1. Replies: 7
    Last Post: 03-16-2012, 06:24 PM
  2. Making my JComponent scrollable
    By kjkrum in forum AWT / Swing
    Replies: 4
    Last Post: 03-11-2012, 08:08 PM
  3. Replies: 1
    Last Post: 03-03-2012, 09:35 PM
  4. Replies: 7
    Last Post: 09-23-2011, 08:04 PM
  5. scrollable applets
    By kaemonsaionji in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 08:47 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
  •