LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2006, 03:59 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
class A extends B- where is paintComponent(Graphics) supposed to paint?


Hi,
i have the following problem: ControlPanel extends GraphArea. I thought ControlPanel.paintComponent(Graphics) was supposed to paint the GraphArea (since ConrolPanel.paintComponent(Graphics) overrides GraphAreaExtension.paintComponent(Graphics) ), but lines AND vertices appear on the ControlPanel. Any ideas?
Code:
import ...
 
public class GraphArea extends JPanel
{
    
    public int clickCounter;  //how many clicks have been performed, ie
                                //how many vertices have been painted
    public int clickedX;
    public int clickedY;
    public Point[] darray;
    
    public GraphAreaExtension()
    {
        setBorder(BorderFactory.createTitledBorder("Graph Area"));
        setPreferredSize(new Dimension(640, 450));  
        setMaximumSize(new Dimension(640, 450));
        
        clickCounter=0;
        darray=new Point[8];
        clickedX=-50;
        clickedY=-50;       
        initializeDarray(); 
        
        addMouseListener(new MouseAdapter()
        {
           public void mouseClicked (MouseEvent e)
           {
                clickCounter++;
                if (clickCounter<=8)
                {
                    clickedY=e.getY();                
                    clickedX=e.getX();
                    darray[clickCounter-1]=new Point(clickedX, clickedY);
                    System.out.print("Click counter "+clickCounter+" ");                    
                    System.out.println("Mouse clicked X: "+clickedX+" Y:"+clickedY);
                    repaint();
                    validate();
                }
           }
        });           
    }//GraphAreaExtension constructor
    
    public void paintComponent(Graphics g)
    {
        System.out.println("paint vertex...");
        g.drawOval(clickedX,clickedY, 25,25);
        g.drawString(getVertexName(clickCounter),clickedX+11, clickedY+15);
        
        printDarray();
    }        
    
    private static String getVertexName(int num)
    {
        num=num+96;
        char c=(char)num;
        String s=String.valueOf(c);
        return s.toUpperCase();
    }    
               
    
    private void initializeDarray()
    {
        for (int i=0; i<darray.length; i++)
            darray[i]=new Point(0,0);
    }
    
    public void printDarray()
    {
        for (int i=0; i><darray.length;i++)
            System.out.print(darray[i].getX()+"\t");            
        
        System.out.println();
        
        for (int i=0; i><darray.length;i++)
            System.out.print(darray[i].getY()+"\t");        
        
        System.out.println();
    }    
}
Code:
import ...
 
 
/*  This is the Control Panel. 3 kinds of visual objects exist here:
 *  -the JCheckBoxes, which indicate the incoming connections: in1-in8
 *  -the JCheckBoxes, which indicate the outcoming connections: out1-out8
 *  -the "Enter" button, which when pressed updates the in/out-coming
 *   connections -graphically designs the edges, between the vertices.
 *
 *  The data taken from the in/out-coming JCheckBoxes are stored inside
 *  2 boolean arrays respectively: inCon and outCon.
 */
 
 
public class ControlPanel extends GraphArea implements ItemListener
{
 
    JPanel controlPanel;
    JButton jb;
    boolean[] inCon;
    boolean[] outCon;
    JCheckBox in1, in2, in3, in4, in5, in6, in7, in8;
    JCheckBox out1, out2, out3, out4, out5, out6, out7, out8;
 
    public ControlPanel()
    {
        inCon= new boolean[8];
        outCon= new boolean[8];
 
        initializeBooleanTable(inCon);
        initializeBooleanTable(outCon);              
 
        setPreferredSize(new Dimension(800,150));
        setBackground(Color.LIGHT_GRAY);                   
        setBorder(BorderFactory.createTitledBorder("Connections"));
        setPreferredSize(new Dimension(120, 150));                    
        setMaximumSize(new Dimension(120, 150));
 
        JLabel in= new JLabel("Incoming connections");
        add(in);
        in1=new JCheckBox("A");
        in1.addItemListener(this);
        add(in1);
        .......
 
        in4=new JCheckBox("D");
        in4.addItemListener(this);
        add(in4);
        ....
 
        in8=new JCheckBox("H");
        in8.addItemListener(this);
        add(in8);
 
        JLabel out= new JLabel("Outcoming connections");
        add(out);           
        out1=new JCheckBox("A");
        add(out1);       
        out1.addItemListener(this);
        ...
 
        out3=new JCheckBox("C");            
        add(out3);   
        out3.addItemListener(this);
        ...
 
        out8=new JCheckBox("H");            
        add(out8);       
        out8.addItemListener(this);
        
        initializeCheckbox();
 
       /*  pressing the Enter button, the data concerning the connections,
        *  which the user has entered are transformed into edges between the 
        * vertices.
        *  Incoming edges: Green and Outcoming: Red
        */
        jb= new JButton("Enter");
        add(jb);
        jb.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                printConnectionArrays();
                repaint(); 
                validate();
            }
        });                
                               
    }//ControlPanel constructor
    
    public void paintComponent(Graphics g)
    {
        System.out.println("paint edge...");
        for (int i=0; i><darray.length; i++)
        {
            if (inCon[i]==true)
            {                
                System.out.println("Entered incoming edges condition for node num "+ (i+1));
                g.drawLine(clickedX,clickedY,(int)darray[i].getX(), (int)darray[i].getY());
                System.out.println("baseX "+clickedX+" baseY "+clickedY+" destX "+(int)darray[i].getX()+" destY "+(int)darray[i].getY());
                g.setColor(Color.GREEN);
            }
 
            if (outCon[i]==true)
            {
                System.out.println("Entered outcoming edges condition for node num "+ (i+1));
                g.drawLine(clickedX, clickedY,(int)darray[i].getX(), (int)darray[i].getY());
                System.out.println("baseX "+clickedX+" baseY "+clickedY+" destX "+(int)darray[i].getX()+" destY "+(int)darray[i].getY());
                g.setColor(Color.RED);                                               
            }                
        }//END for       
 
        initializeCheckbox();                
        initializeBooleanTable(inCon);
        initializeBooleanTable(outCon);        
    } 
    
        /******************************Helper***Functions***************************/        
        /*  unchecks Checkboxes for next use
         */
        public void initializeCheckbox()
        {            
            in1.setSelected(false);     out1.setSelected(false);
            ....                                          ...
            in8.setSelected(false);     out8.setSelected(false);                        
        }
 
        
        public void itemStateChanged(ItemEvent e) 
        {
            System.out.println("Entered ItemStateChanged()...");
             Object source = e.getItemSelectable();
 
             if (source.equals(in1)) inCon[0]=true;
             else if (source.equals(in2)) inCon[1]=true;
             ...
             else if (source.equals(in8)) inCon[7]=true;
             
             if (source.equals(out1)) outCon[0]=true;
             else if (source.equals(out2)) outCon[1]=true;
              ...
             else if (source.equals(out8)) outCon[7]=true;             
        }  
        
        
        public void printConnectionArrays()
        {
            System.out.print("Incoming  ");
            for (int i=0; i><darray.length; i++)
                System.out.print(inCon[i]+"  ");
            System.out.print("Outcoming  ");
            for (int i=0; i><darray.length; i++)
                System.out.print(outCon[i]+"  ");
            System.out.println();
        }
        
        public static void initializeBooleanTable(boolean[] t)
        {
            for (int i=0; i><t.length-1; i++)
                t[i]=false;
        }        
    
    
}

Last edited by kpachopoulos; 03-15-2006 at 06:27 AM.
 
Old 03-15-2006, 06:28 AM   #2
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
Updated; fixed some mistakes
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Quest extends single sign-on support LXer Syndicated Linux News 0 02-20-2006 10:01 PM
LXer: National Instruments Extends Linux Support to 200 Devices LXer Syndicated Linux News 0 12-15-2005 03:31 PM
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration