Results 1 to 5 of 5

Thread: HI.....

  1. #1
    sims is offline Member
    Join Date
    Jul 2008
    Posts
    9
    Rep Power
    0

    Default HI.....

    [CODE =JAVA]
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import java.awt.geom.*;

    class projRx extends JFrame implements ChangeListener,
    Runnable,
    ActionListener
    {
    // public int bottom;
    // public Thread t;
    // int padding = 4;
    JFrame f;
    JTabbedPane jt ,jt1;
    JPanel /*jp,jp1,*/ jp2,jp3,jp4;
    JButton j,j1,j2;
    JLabel lab,lab1;
    JComboBox jcb,jcb1;
    JColorChooser jc,jc1;
    public Color col1 ;

    public Graphics g;
    // public Graphics g1;
    // public Graphics2D g2d;
    // JDialog dialog;
    String k;
    // Line2D shape1;
    // f p1 = new f();
    // we need to access fields inside this class
    pan jp;
    pan jp1;
    // draw d;

    public projRx()
    {
    f = new JFrame("Graph");
    /*pan*/ jp = new pan();
    /*pan*/ jp1 = new pan();
    // d = new draw();
    jt = new JTabbedPane(JTabbedPane.TOP);
    jt.setTabPlacement(JTabbedPane.TOP);

    jp.setPreferredSize(new Dimension(50,50));
    jp.setLayout(new BorderLayout());
    jp.setBackground(Color.gray);
    jp.setPreferredSize(new Dimension(60,50));

    jp4 = new JPanel();
    j = new JButton("Draw the graph");
    j.addActionListener(this);
    jp4.setBorder(BorderFactory.createRaisedBevelBorde r());
    jp4.setPreferredSize(new Dimension(130,20));
    jp4.add(j);

    jp.add(jp4,BorderLayout.EAST);
    String[] a = "1 2 3 4 50".split("\\s");
    System.out.println(Arrays.toString(a));
    lab = new JLabel("x parameters");
    lab.setLocation(700,700);
    jp4.add(lab);
    jcb= new JComboBox(a);
    jcb.setLocation(700,480);
    jcb.addActionListener(this);
    jp4.add(jcb);
    lab1 = new JLabel("Y parameters");
    jp4.add(lab1);
    jcb1= new JComboBox(a);
    jcb1.addActionListener(this);
    jp4.add(jcb1);
    jt.addTab("graph1", jp);

    jp1.setBackground(Color.black);
    jt.addTab("Graph2",jp1);
    JPanel jp3=new JPanel();

    jc = new JColorChooser(Color.yellow);
    jc.createDialog(jp3,"Choose Background Color",true, jc, null,null);
    jc.getSelectionModel().addChangeListener(this);
    jp3.add(jc);
    jt.addTab("Change Grid Color", jp3);
    f.add(jt);
    f.pack();
    f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    f.setSize(400,400);
    f.setVisible(true);
    }


    public void run()
    {
    // this is for animations, not intitalizing your gui.
    System.out.println("run");
    }

    public void stateChanged(ChangeEvent e)
    {

    }

    public void actionPerformed(ActionEvent e)
    {
    System.out.println(e.getActionCommand());
    if(e.getSource() == jcb)
    {
    int n = jcb.getSelectedIndex() +1;
    int k = jcb.getSelectedIndex()+2;
    System.out.println("run = " + n);
    System.out.println("run = " + k);
    jp.run = n;
    }
    if(e.getSource() == jcb1)
    {
    int n = jcb1.getSelectedIndex()+1 ;
    System.out.println("rise = " + n);
    jp.rise = n;
    }
    if(e.getSource()==j)
    {
    Color col1 = jc1.showDialog(jp4,"Choose",Color.RED);
    draw( col1);

    }

    jp.repaint();
    }

    public static void main(String[] args)
    {
    projRx lp = new projRx();
    Thread t = new Thread(lp);
    t.start();
    }
    public void draw(Color c1)
    {
    Thread t = new Thread();
    t.start();
    System.out.print(c1);

    }
    public class pan extends JPanel
    {

    // Set these two member variables from the
    // JComboBox ActionListener above.
    public int run = 1;
    public int rise = 1;
    public Color col2 = col1;
    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Color col1=jc.getColor();
    g.setColor(col1);
    Rectangle clip = new Rectangle(30,30,800,600);
    g.drawRect( 30,30,800,600); //600 = width 400 = height
    // draw vertical lines
    for(int i=30;i<=620; i=i+20)
    {
    g.drawLine(30,i,830,i);
    }
    // draw ordinate labels
    g.setColor(Color.white);
    for(int i=10;i<=620; i=i+20)
    {
    k=Integer.toString(i); // or String.valueOf(i)
    g.drawString(k,5,i+27);
    }

    // draw horizontal lines
    for(int i=10;i<=820; i=i+20)
    {
    int l = i/10;
    g.drawString(Integer.toString(l),i+17,650);
    }
    // draw abcissa labels
    g.setColor(col1);
    for(int i =0;i<=800;i=i+20)
    {
    g.drawLine(i+30,30,i+30,630);
    }

    int S = 10; // cellSize
    /*
    int unitsPerCell = 20;
    int w = 800;
    int h = 600;
    // line origin at graph origin
    int x0 = S;
    int y0 = h + S;
    double slope = 0.0;
    if(run != 0)
    {
    slope = (double)rise/run;

    }
    int x = w + S;
    System.out.println("x = " + x); System.out.println("w = " + w); System.out.println("s = " + S);
    int y = y0 - (int)(slope*x);
    System.out.println("y = " + y);
    g.setColor(Color.RED);
    // show only the line segment that is
    // inside clip/graph bounds
    Shape origClip = g.getClip();
    g.setClip(clip);
    g.drawLine( x, y,rise,run);
    */
    // int x0;
    //g.setColor(col2);

    g.drawLine(50+20,110+20,370+20,410+20);
    // reset, g is usede by other methods later on
    // g.setClip(origClip);

    // navigation markers for design/debug
    g.setColor(Color.green.darker());
    // mark origin
    /* g.fillOval(x0-2, y0-2, 4, 4);
    // mark se corner of graph
    g.fillOval(S+w-2, y0-2, 4, 4);
    // mark ne corner
    g.fillOval(S+w-2, S-2, 4, 4);*/
    }

    }
    }
    [/CODE]

    How can i acess Graphics g,,,outside he pan class..Tried accesing it leads to null value..my requirement is when user clicks on button color chooser pops,,the user selects the color and the graph should be drawn....with movements on the interface visible to the users..The user can add upto 10 graphs simutaneously..The values to the graph is provided in the file which is read to the comboboxes.The combobox actually contains paramters(x,y).where the user on selecting the parameters the file is searched for those parameters...n till end of file the ,for those values the graph is drawn.......Plz help not able to get solution..

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

    Default

    How can i acess Graphics g,,,outside he pan class
    Not recommended; see pseudo code below.

    Tried accesing it leads to null value
    This is a problem with trying to use the getGraphics method.

    my requirement is when user clicks on button color chooser pops,,
    the user selects the color and the graph should be drawn

    Okay. We can do this with OOP techniques: see pseudo code below.

    with movements on the interface visible to the users
    What kind of movements are you trying to create?

    The user can add upto 10 graphs simutaneously
    Graphs or lines on a grid/graph background? Vague description.

    The values to the graph is provided in the file which is read to the comboboxes.
    "values to the graph"... need more info about what you are trying to create.

    Java Code:
    class GuiClass implements ActionListener {
        GraphicClass graphicClass = new GraphicClass();
        JColorChooser colorChooser = new JColorChooser();
    
        public void actionPerformed(ActionEvent e) {
            // When user presses button (while the JColorChooser
            // tab is showing) get user-selected color from it
            // and use it to set the color "col1" in the instance
            // of GraphicsClass, viz, graphicsClass.
            Color color =
                colorChooser.getSelectionModel().getSelectedColor();
            graphicClass.setCol1(color);
        }
    
        public static void main(String[] args) {
            // build gui:
            // make a JFrame and configure it
            // add graphicClass to gui
            // add colorChooser to gui
            // add button to gui (that will get the
            // selectedColor in the colorChooser)
            // add [i]this[/i] ActionListener to button
            // show gui
        }
    }
    
    class GraphicClass extends JPanel {
        Color col1;
    
        public void setCol1(Color color) {
            col1 = color;
            repaint();
        }
        
        protected void paintComponent(Graphics g)
        { 
            super.paintComponent(g);
            g.setColor(col1);
            ...
            // draw abcissa labels
            g.setColor(col1);
            ...
        }
    }

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

    Default

    I recommend that you read this, this, and then this.

  4. #4
    sims is offline Member
    Join Date
    Jul 2008
    Posts
    9
    Rep Power
    0

    Default

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.awt.image.BufferedImage;
    import java.awt.geom.*;
    import java.math.*;
    import java.util.*;

    public class ecg1 extends JFrame
    {
    pan view = new pan();
    Vector vr = new Vector();
    int ho = 50;

    public ecg1()
    {
    addWindowListener(new WindowAdapter()
    { public void windowClosing(WindowEvent ev)
    { dispose();
    System.exit(0);}});

    for (int i=0; i < 5000; i++)
    {
    int p = (int)(Math.random() * 260);
    vr.add(new Point(0,p-130));
    }

    setBounds(3,10,625,350);
    view.setBounds(10,10,600,300);
    getContentPane().add(view);
    getContentPane().setLayout(null);
    setVisible(true);

    while (ho < 500-300)
    {
    try
    {
    Thread.sleep(110);
    ho = ho + 1;
    repaint();
    } catch (InterruptedException e) {}
    }

    }
    public static void main (String[] args)
    {
    new ecg1();
    }

    public class pan extends JPanel
    {

    // Set these two member variables from the
    // JComboBox ActionListener above.


    BufferedImage I;
    Graphics2D G;

    public int run = 1;
    public int rise = 1;
    String k="";



    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);


    if (I == null)
    {
    I = new BufferedImage(getWidth(),getHeight(),BufferedImage .TYPE_INT_ARGB);
    System.out.print(I.toString());
    G = I.createGraphics();
    }



    Color col1=Color.black;
    g.setColor(col1);
    Rectangle clip = new Rectangle(30,30,800,600);
    g.drawRect( 30,30,800,600); //600 = width 400 = height
    // draw vertical lines
    for(int i=30;i<=620; i=i+20)
    {
    g.drawLine(30,i,830,i);
    }
    // draw ordinate labels
    g.setColor(Color.red);
    for(int i=10;i<=620; i=i+20)
    {
    k=Integer.toString(i); // or String.valueOf(i)
    g.drawString(k,5,i+27);
    }

    // draw horizontal lines
    for(int i=10;i<=820; i=i+20)
    {
    int l = i/10;
    g.drawString(Integer.toString(l),i+17,650);
    }
    // draw abcissa labels
    g.setColor(col1);
    for(int i =0;i<=800;i=i+20)
    {
    g.drawLine(i+30,30,i+30,630);
    }


    G.setStroke(new BasicStroke(4));
    // G.setColor(Color.white);
    // G.fillRect(0,0,getWidth(),getHeight());
    G.setColor(Color.red);
    Point p1,p2;
    p1 = (Point)vr.get(ho);
    int x = 0;
    for (int y=1; y < 600; y++)
    {
    p2 = (Point)vr.get(y+ho);
    G.drawLine(50,50,ho,ho);
    //G.drawLine(x,p1.y+150,x+6,p2.y+150);
    p1 = (Point)vr.get(y+ho);
    x = x + 6;
    }
    g.drawImage(I,0,0,null);





    }

    }



    }This is how i need the line to be drawn on my interface,,,
    The user can add upto 10 lines on the grid for observation..
    values to the graph means ..there is a file say for eg:Pressure,velocity,temperature,wheel rotation...pressure has say 100 values recorded,,similarly temperature...
    First case i like to plot graph for Velocity vs pressure,,,so that graph is drawn.Even before comlpetionof the line drawn i like to simultaneoualy plot graph for pressure vs velocity...so on the graph both the graph should be drawn...using thread...
    how can i do this on through interface

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

    Default

    so on the graph both the graph should be drawn
    This confuses me. Do you want both lines/functions to be drawn on a single graph/grid_structure or do you want each to be drawn on a separate graph/grid_structure that can be located side-by-side in your gui?
    If the former then you are you concerned about scaling or representing units along the axes?
    A plot of velocity vs. pressure may have units of
    ordinate: feet/second
    abcissa: pounds/square inch
    and a plot of temperature vs. another variable will likely have different units for the axes. If you're not concerned about these, okay. If you are then we have some more things to consider.

    Velocity vs pressure...simultaneoualy plot graph for pressure vs velocity
    This fragment is also confusing.

    If you just want to throw up some line-graph representations of functional relationships on a single graph/grid-structure without regard to units and the like you can certainly do it. One way, depending on what you are trying to do, is to create a Path2D for each relationship/function, scale these into your view/grid and draw them a little at a time on a BufferedImage which you draw in paintComponent much like it appears you may have done in ecg1 above (I didn't format and closely inspect what you posted).

Posting Permissions

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