class A extends B- where is paintComponent(Graphics) supposed to paint?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.