PaintCompnent

[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 proj extends JFrame implements ChangeListener ,Runnable,ActionListener //main class
     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;
          Color col1,c2;
     public     Graphics g;
     public     Graphics g1;
     public     Graphics2D g2d;
     JDialo     g dialog;
     String     k;
     Line2D     shape1;
     f p1 = new f();
          public void run()
               f = new JFrame("Graph");
               pan jp = new pan();
               pan jp1 = new pan(); ;
               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.createRaisedBevelBorder());
               jp4.setPreferredSize(new Dimension(130,20));
               jp4.add(j);
               jp.add(jp4,BorderLayout.EAST);     
               String[] a = {"select "};
               lab = new JLabel("x parameters");
               lab.setLocation(700,700);
               jp4.add(lab);
               jcb= new JComboBox(a);
               jp4.add(jcb);
               lab1 = new JLabel("Y parameters");
               jp4.add(lab1);
               jcb1= new JComboBox(a);
               jp4.add(jcb1);
               jt.addTab("graph1", jp);
          //panel2
               jp1.setBackground(Color.black);     
               jt.addTab("Graph2",jp1);
               JPanel jp3=new JPanel();
          //Colorchooser
               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 stateChanged(ChangeEvent e)
          public void actionPerformed(ActionEvent e)
               if((JButton)e.getSource() == j)
               /*jc1 = new JColorChooser();
               JColorChooser.showDialog(jp4,"Select The Color", Color.red);
               c2=jc1.getColor();*/
               p1.repaint();
               p1.paint(Graphics g);
          public static void main(String[]args)
               proj lp = new proj();
               Thread t = new Thread(lp);
               t.start();
          public class pan extends JPanel
               Graphics g;     
               public void paintComponent(Graphics g)
                    super.paintComponent(g);
                    Color col1=jc.getColor();
                    g.setColor(col1);
                    g.drawRect( 30,30,800,600);//600 = width 400 = height
                    for(int i=30;i<=620; i=i+20)
                         g.drawLine(30,i,830,i);                     }
                    g.setColor(Color.white);
                    for( Integer i=10;i<=620; i=i+20)
                         k=Integer.toString(i);
                         g.drawString(k,5,i+27);
                    for(Integer i=10;i<=820; i=i+20)
                         int l = i/10;
                         g.drawString(Integer.toString(l),i+17,650);
                    g.setColor(col1);
                    for(int i =0;i<=800;i=i+20)
                    g.drawLine(i+30,30,i+30,630);                }
my problem is when a user clicks on the button a line shud be drawn. tried many ways ..but not able to draw line on the graph.

Yes, very simply :
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.JLabel;
public class Wdes extends JPanel {
     private static final long serialVersionUID = 1L;
     private JLabel jLabel = null;
     public Wdes() {
          super();
          initialize();
     private void initialize() {
          jLabel = new JLabel();
          jLabel.setBounds(new Rectangle(8, 45, 268, 24));
          jLabel.setText("hello : mi size at destiny container is :");
          this.setLayout(null);
          this.setSize(300, 117);
          this.add(jLabel, null);     
          public void paint(Graphics g) {
          System.out.println("paint"+this.getWidth()+ " "+ this.getHeight());
     public void paintComponent(Graphics g) {
          System.out.println("paintcomponent"+this.getWidth()+ " "+ this.getHeight());
}As you can see I have A Jlabel : jLabel.setText("hello : mi size at destiny container is :");
Ok, I put this bean onto my application and I give it the size desired.
I can use paint or paintcomponent to know my real size , isn't it ? ( and please forget my initial condition of 2 paint events ... -simply I come from VB ...- )
Is there another way ?
And, I dont know what is happen but in this example only work paint ( paintComponent does not )
( If i want to use paintcomponent I have to delete paint , isn't it ? )
Thanks

Similar Messages

  • How to get the size of 'MyComponent'  when it is used in another place ?

    'My component' is JPanel based ( for example)
    I find problems to use the real size of MyComponent ( the size it has at the destiny)
    In 'Initialize' this.getheight gives me the size of the component by itself
    Must I use 'paintComponent' to know the real size or where ?
    By other side, must I count 2 paintComponent() ( the first 2 times paintCompnent is called by the 'system' , one for width and one for height ) to make sure the component is already drawed ?
    Thank you

    Yes, very simply :
    import javax.swing.JPanel;
    import java.awt.Rectangle;
    import javax.swing.JLabel;
    public class Wdes extends JPanel {
         private static final long serialVersionUID = 1L;
         private JLabel jLabel = null;
         public Wdes() {
              super();
              initialize();
         private void initialize() {
              jLabel = new JLabel();
              jLabel.setBounds(new Rectangle(8, 45, 268, 24));
              jLabel.setText("hello : mi size at destiny container is :");
              this.setLayout(null);
              this.setSize(300, 117);
              this.add(jLabel, null);     
              public void paint(Graphics g) {
              System.out.println("paint"+this.getWidth()+ " "+ this.getHeight());
         public void paintComponent(Graphics g) {
              System.out.println("paintcomponent"+this.getWidth()+ " "+ this.getHeight());
    }As you can see I have A Jlabel : jLabel.setText("hello : mi size at destiny container is :");
    Ok, I put this bean onto my application and I give it the size desired.
    I can use paint or paintcomponent to know my real size , isn't it ? ( and please forget my initial condition of 2 paint events ... -simply I come from VB ...- )
    Is there another way ?
    And, I dont know what is happen but in this example only work paint ( paintComponent does not )
    ( If i want to use paintcomponent I have to delete paint , isn't it ? )
    Thanks

  • DrawPanel Draw JFrame

    I need little help with thi code i need some feedback please here is the code:
    import java.awt.Graphics;
    import javax.swing.JPanel;
    public class DrawPanel extends JPanel
         PostalAddress drawingAddress = new PostalAddress();//instantiate PostalAddress class to get to drawingAddress method
              public DrawPanel(PostalAddress myAddress)
                   super();
                   drawingAddress = myAddress;
                   } //end of constructor          
              public void paintComponent(Graphics g)//paint method puts the data in drawingAddress on the panel
              super.paintComponent(g);
              drawingAddress.DrawAddress(g,25,35);
    } // end paintCompnent
    } // end class DrawPanel
    // Application to display a DrawPanel.
    import javax.swing.JFrame;
    public class DrawPanelTest
    public static void main( String args[] )
    // create a panel that contains our drawing
    DrawPanel panel = new DrawPanel();
    // create a new frame to hold the panel
    JFrame application = new JFrame();
    // set the frame to exit when it is closed
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    application.add( panel ); // add the panel to the frame
    application.setSize( 250, 250 ); // set the size of the frame
    application.setVisible( true ); // make the frame visible
    } // end main
    } // end class DrawPanelTest
    Thanks

    Thanks for the reply.
    Basically i want the orogrm to disply a postal address lebel in a frame or panel with name,street address ,city,state and zipcode+four digit that follow the zipcode
    public class PostalAddress
       private String name;
       private String streetAddress;
       private String city;
       private String state;
       private String zipcode;
       private String plusFour;
      public void DrawAddress (Graphics  g,  int  x,  int  y)
          g.drawString(x,y,25,35);
          g.setClor(Color.black);
          this.name = name;
          this.streetAddress = streetAddress;
          this.city = city;  
          this.state = state;
          this.zipcode = zipcode;
          this.plusFour = plusFour;
    }   // end class PostalAddress

Maybe you are looking for

  • Reg: Calling RFC in webdynpro

    Hi, I am supporting some webdynpro application. In the application we are connecting to R3 systems using RFC. But in my application the developer had created different models for each Bapi instead of dumping them into one model. I just wanted to know

  • IMac 10.5 does not sleep

    My iMac does not sleep. The display does go to sleep but the computer itself does not, and as a result heats up like none other. Any help?

  • ADF: Hide expendable icon in Master-Detail treetables

    Hello, We are using a treetable in order to display Master-detail data. The problem here is that not all the master (parents) rows have details (childs). In fact we do not want to display the expandable symbol for the master rows which do not have ch

  • Gmail label/os x mail problems

    In an effort to fix some strange gmail/imap/apple-mail/iphone behavior, i inadvertently broke it even more. my OS X Mail was not displaying the correct gmail inbox. it would only display older messages (from a few weeks ago). based on some advice i f

  • Spinning Ball in iTunes Start up

    Every time that I open my iTunes I get the spinning ball and it locks up iTunes and I have to shut down my computer to start over.I read a post that said to go into utilities and post what is happening.  This happens no matter what device I plug in t