Click to See Complete Forum and Search --> : error in java swing code


aks_hcu
March 13th, 2005, 01:57 AM
hi friends,
i wrote a program in java swing .i am using netbeans as a editor.
this program has some error, but i am unable to remove it. please check out this and help me...

here is the code.......wherever it is showing error, i have put stars there (***)
---------------------------------------------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class PaintDrawApplet1 extends JFrame
{

private PaintDrawApplet1()

{
initComponents();
setSize(800, 600);
setTitle("Project1");


}

private void initComponents()
{

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.setBackground(Color.black);
TestCanvas drawArea = new TestCanvas();
Figure selectedFigure = null;
boolean dragging = false;

contentPane.add (drawArea);
contentPane.add(moveButton);
moveButton.addActionListener(new MoveListener());
figures.add(a);
figures.add(b);
figures.add(c);
figures.add(d);
connections.add(ab);
connections.add(ca);
connections.add(bc);
connections.add(ad);
connections.add(bd);
}

*** private RoundRectangleFigure a = new RoundRectangleFigure("A", 30, 50, 50, 50);
private OvalFigure b = new OvalFigure("B", 200, 50, 50, 50);
private RectangleFigure c = new RectangleFigure("C", 30, 150, 50, 50);
private OvalFigure d = new OvalFigure("D", 100, 100, 60, 30);

private Connection ab = a.connect("AB", b);
private Connection ca = c.connect("CA", a);
*** private Connection bc = b.connect("BC", c);
private Connection ad = a.connect("AD", d);
private Connection bd = b.connect("BD", d);

*** private ArrayList figures = new ArrayList();
*** private ArrayList connections = new ArrayList();

private Button moveButton = new Button("Animate");

public Dimension getPreferredSize()
{ return new Dimension(600,400);
}

public Dimension getMinimumSize()
{ return new Dimension(500, 300);
}

class TestCanvas extends JPanel
{
private Image offscreen = null;
private static final boolean doubleBuffering = false;
private Graphics og = null; // The offscreen graphics context.

public TestCanvas()
{ addMouseListener(new HitListener());
addMouseMotionListener(new DragListener());
setBackground(Color.white);
}

public void invalidate()
{ super.invalidate();
offscreen = null;
}

public Dimension getPreferredSize()
{
*** Dimension d = PaintDrawApplet.this.getPreferredSize();

return new Dimension(d.width - 100, d.height);
}

public Dimension getMinimumSize()
{
return new Dimension(400, 300);
}

public void update(Graphics g)
{ if(doubleBuffering)
{ paint(g);
}
else
{ super.update(g);
}
}

public void flip(Figure f)
{
Graphics g = getGraphics();
g.setXORMode(getBackground());
f.drawOutline(g); // Try draw here instead of drawOutline for a different effect
g.dispose();
}

private void doDraw(Graphics g)
{
*** Iterator e = connections.iterator();
while(e.hasNext())
((Figure) e.next()).draw( g);
e = figures.iterator();
while(e.hasNext())
((Figure) e.next()).draw( g);
}

public void paint( Graphics g )
{ if(!doubleBuffering)
{ doDraw(g);
return;
}
Dimension d = getSize(); int h = d.height; int w = d.width;
if(offscreen == null) offscreen = createImage(w, h);
Graphics og = offscreen.getGraphics();
og.setClip(0, 0, w, h);
doDraw(og); // equivalent to the next few statements (marginally faster)

g.drawImage(offscreen, 0, 0, null);
og.setColor(getBackground()); // Clear the image for next paint.
og.fillRect(0, 0, w, h);
og.dispose();
}
}

class MoveListener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ for (int i = 0; i < 120; ++i)
{
*** drawArea.flip(b);
b.move(b.location().x, b.location().y + 2);
*** drawArea.flip(b);
pause(3);
}
for (int i = 0; i < 120; ++i)
{
*** drawArea.flip(b);
b.move(b.location().x + 2, b.location().y);
*** drawArea.flip(b);
pause(3);
}
for (int i = 0; i < 120; ++i)
{
drawArea.flip(b);
b.move(b.location().x, b.location().y - 2);
drawArea.flip(b);
pause(3);
}
for (int i = 0; i < 100; ++i)
{
drawArea.flip(b);
b.move(b.location().x - 2, b.location().y);
drawArea.flip(b);
pause(3);
}
drawArea.repaint();
}

private void pause(int n)
{ try{Thread.currentThread().sleep(n);}catch(InterruptedException e){}
}
}

class HitListener extends MouseAdapter
{ public void mouseClicked(MouseEvent e)
{ Point p = e.getPoint();
Iterator v = figures.iterator();
selectedFigure = null;
boolean doDraw = false;
while(v.hasNext())
{ Figure f = (Figure)v.next();
if(f.contains(p))
{ if(! f.isSelected())
{ doDraw = true;
f.setSelected(true);
selectedFigure = f;
}
}
else
{ if(f.isSelected())
{ doDraw = true;
f.setSelected(false);
}
}
}
if (doDraw) drawArea.repaint();
}

public void mousePressed(MouseEvent e)
{ if(selectedFigure != null && selectedFigure.contains(e.getPoint()))
dragging = true;
}

public void mouseReleased(MouseEvent e)
{ dragging = false;
if(selectedFigure != null)
{ selectedFigure.setSelected(false);
selectedFigure = null;
}
drawArea.repaint();
}
}

class DragListener extends MouseMotionAdapter
{ public void mouseDragged(MouseEvent e)
{ if(dragging && selectedFigure != null)
{ drawArea.flip(selectedFigure);
selectedFigure.move(e.getPoint());
drawArea.flip(selectedFigure);
}
}
}
}



__________________________________________________________________________

thanks............

please help me.......

dlorde
March 13th, 2005, 06:26 PM
So what are the errors?

It would help if you posted up the full error text. Java error messages usually tell you exactly what is wrong...

Optimism is an occupational hazard of programming: testing is the treatment...
K. Beck

aks_hcu
March 14th, 2005, 01:03 AM
Hello sir
i am making a editor of drawing different process modelling.but problem is that in the editior after the drawing of one digram(like rectangle) and when draw the second one(circle),the first hasbeen(rectangle) disappeared due to repaint method.
so give me some idea about ,how to come over that, and i want to use mysql as database so as to store the information about the digram.

aks_hcu
March 14th, 2005, 01:05 AM
hi friends,
thanks for viewing the code. i am sending the detail of output error text with each line of error in the code.

error text is started by /////////// and line of code which has error is started by ****************.
error point is shown by ^ .
please check out this ...


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class PaintDrawApplet1 extends JFrame
{

private PaintDrawApplet1()

{
initComponents();
setSize(800, 600);
setTitle("Project1");


}

private void initComponents()
{

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.setBackground(Color.black);
TestCanvas drawArea = new TestCanvas();
Figure selectedFigure = null;
boolean dragging = false;

contentPane.add (drawArea);
contentPane.add(moveButton);
moveButton.addActionListener(new MoveListener());
figures.add(a);
figures.add(b);
figures.add(c);
figures.add(d);
connections.add(ab);
connections.add(ca);
connections.add(bc);
connections.add(ad);
connections.add(bd);
}


/////PaintDrawApplet1.java [44:1] cannot resolve symbol
/////symbol : class RoundRectangleFigure
////location: class PaintDrawApplet1
//// private RoundRectangleFigure a = new RoundRectangleFigure("A", 30, 50, 50, 50);
^

*********** private RoundRectangleFigure a = new RoundRectangleFigure("A", 30, 50, 50, 50);


private OvalFigure b = new OvalFigure("B", 200, 50, 50, 50);
private RectangleFigure c = new RectangleFigure("C", 30, 150, 50, 50);
private OvalFigure d = new OvalFigure("D", 100, 100, 60, 30);

private Connection ab = a.connect("AB", b);
private Connection ca = c.connect("CA", a);

//////////////PaintDrawApplet1.java [51:1] connect(java.lang.String,Figure) in Figure cannot be applied to (java.lang.String,RectangleFigure)
///////////// private Connection bc = b.connect("BC", c);
^

**************private Connection bc = b.connect("BC", c);


private Connection ad = a.connect("AD", d);
private Connection bd = b.connect("BD", d);


///////////PaintDrawApplet1.java [55:1] cannot resolve symbol
///////////symbol : class ArrayList
/////////////location: class PaintDrawApplet1
///////////// private ArrayList figures = new ArrayList();
^
***************** private ArrayList figures = new ArrayList();

///////////////PaintDrawApplet1.java [56:1] cannot resolve symbol
///////////////symbol : class ArrayList
//////////////location: class PaintDrawApplet1
////////////// private ArrayList connections = new ArrayList();
^

************ private ArrayList connections = new ArrayList();

private Button moveButton = new Button("Animate");

public Dimension getPreferredSize()
{ return new Dimension(600,400);
}

public Dimension getMinimumSize()
{ return new Dimension(500, 300);
}

class TestCanvas extends JPanel
{
private Image offscreen = null;
private static final boolean doubleBuffering = false;
private Graphics og = null; // The offscreen graphics context.

public TestCanvas()
{ addMouseListener(new HitListener());
addMouseMotionListener(new DragListener());
setBackground(Color.white);
}

public void invalidate()
{ super.invalidate();
offscreen = null;
}

public Dimension getPreferredSize()
{

//////////////PaintDrawApplet1.java [87:1] not an enclosing class: PaintDrawApplet
Dimension d = PaintDrawApplet.this.getPreferredSize();
^

************** Dimension d = PaintDrawApplet.this.getPreferredSize();

return new Dimension(d.width - 100, d.height);
}

public Dimension getMinimumSize()
{
return new Dimension(400, 300);
}

public void update(Graphics g)
{ if(doubleBuffering)
{ paint(g);
}
else
{ super.update(g);
}
}

public void flip(Figure f)
{
Graphics g = getGraphics();
g.setXORMode(getBackground());
f.drawOutline(g); // Try draw here instead of drawOutline for a different effect
g.dispose();
}

private void doDraw(Graphics g)
{

//////PaintDrawApplet1.java [116:1] cannot resolve symbol
symbol : class Iterator
l//////ocation: class PaintDrawApplet1.TestCanvas
Iterator e = connections.iterator();
^

********** Iterator e = connections.iterator();


while(e.hasNext())
((Figure) e.next()).draw( g);
e = figures.iterator();
while(e.hasNext())
((Figure) e.next()).draw( g);
}

public void paint( Graphics g )
{ if(!doubleBuffering)
{ doDraw(g);
return;
}
Dimension d = getSize(); int h = d.height; int w = d.width;
if(offscreen == null) offscreen = createImage(w, h);
Graphics og = offscreen.getGraphics();
og.setClip(0, 0, w, h);
doDraw(og); // equivalent to the next few statements (marginally faster)

g.drawImage(offscreen, 0, 0, null);
og.setColor(getBackground()); // Clear the image for next paint.
og.fillRect(0, 0, w, h);
og.dispose();
}
}

class MoveListener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ for (int i = 0; i < 120; ++i)
{

//////////PaintDrawApplet1.java [145:1] cannot resolve symbol
///////////symbol : variable drawArea
///////////location: class PaintDrawApplet1.MoveListener
/////////// { drawArea.flip(b);
^

********** drawArea.flip(b);
b.move(b.location().x, b.location().y + 2);

///////PaintDrawApplet1.java [147:1] cannot resolve symbol
///////symbol : variable drawArea
location: class PaintDrawApplet1.MoveListener
drawArea.flip(b);
^

********* drawArea.flip(b);

pause(3);
}
for (int i = 0; i < 120; ++i)
{

////////PaintDrawApplet1.java [152:1] cannot resolve symbol
////////symbol : variable drawArea
location: class PaintDrawApplet1.MoveListener
drawArea.flip(b);
^
************* drawArea.flip(b);

b.move(b.location().x + 2, b.location().y);

/////////PaintDrawApplet1.java [154:1] cannot resolve symbol
////////symbol : variable drawArea
location: class PaintDrawApplet1.MoveListener
drawArea.flip(b);
^

************ drawArea.flip(b);

pause(3);
}
for (int i = 0; i < 120; ++i)
{

//////////PaintDrawApplet1.java [158:1] cannot resolve symbol
////////symbol : variable drawArea
location: class PaintDrawApplet1.MoveListener
{ drawArea.flip(b);
^

************drawArea.flip(b);

b.move(b.location().x, b.location().y - 2);

////////PaintDrawApplet1.java [160:1] cannot resolve symbol
////////symbol : variable drawArea
location: class PaintDrawApplet1.MoveListener
drawArea.flip(b);
^

**********drawArea.flip(b);

pause(3);
}
for (int i = 0; i < 100; ++i)
{

**************drawArea.flip(b); ////same error haere
b.move(b.location().x - 2, b.location().y);

*************drawArea.flip(b); //////same here
pause(3);
}
***********drawArea.repaint();//////same here
}

private void pause(int n)
{ try{Thread.currentThread().sleep(n);}catch(InterruptedException e){}
}
}

class HitListener extends MouseAdapter
{ public void mouseClicked(MouseEvent e)
{ Point p = e.getPoint();

///////PaintDrawApplet1.java [180:1] cannot resolve symbol
///////symbol : class Iterator
location: class PaintDrawApplet1.HitListener
Iterator v = figures.iterator();
^

***********Iterator v = figures.iterator();
///////PaintDrawApplet1.java [181:1] cannot resolve symbol
////////symbol : variable selectedFigure
location: class PaintDrawApplet1.HitListener
selectedFigure = null;
^

***********selectedFigure = null;

boolean doDraw = false;
while(v.hasNext())
{ Figure f = (Figure)v.next();
if(f.contains(p))
{ if(! f.isSelected())
{ doDraw = true;
f.setSelected(true);
selectedFigure = f;
}
}
else
{ if(f.isSelected())
{ doDraw = true;
f.setSelected(false);
}
}
}
***********if (doDraw) drawArea.repaint();
^ ///same error here
}

public void mousePressed(MouseEvent e)

{
*********if(selectedFigure != null && selectedFigure.contains(e.getPoint()))
^ ^ ///// same error here as previous

//////PaintDrawApplet1.java [204:1] cannot resolve symbol
///////symbol : variable dragging
location: class PaintDrawApplet1.HitListener
dragging = true;
^
********dragging = true;

}

public void mouseReleased(MouseEvent e)
{
*********dragging = false;
^ ///same error here

*******if(selectedFigure != null)
^ ///same error here as previous

{
******** selectedFigure.setSelected(false);
^
*******selectedFigure = null;
^
}
*********drawArea.repaint();
^
}
}

class DragListener extends MouseMotionAdapter
{ public void mouseDragged(MouseEvent e)
{
*********if(dragging && selectedFigure != null)
^ ^
{
*********drawArea.flip(selectedFigure);
^ ^
*********selectedFigure.move(e.getPoint());
^
********drawArea.flip(selectedFigure);
^ ^
}
}
}
}

Joe Nellis
March 14th, 2005, 04:42 AM
You are missing a series of import statements for all that code YOU wrote. The compiler can't resolve symbols (classes) it has never seen before.

NoHero
March 14th, 2005, 05:04 AM
And please use code tags when posting code!

aks_hcu
March 15th, 2005, 12:25 AM
hi friends,
thanks for ur guidelines and sorry for not giving full code form of all classes , which i am using.
now i have removed all compilation error from the code. but now it is showing run time error-
stack overflow in main thread.
if u know tell me the reason for this type of error.
thanks.....

dlorde
March 15th, 2005, 05:49 AM
Stack overflow is generally caused by recursion - a method is calling itself directly or indirectly with no way to stop.

There does not now, nor will there ever exist, a programming language in which it is the least bit hard to write bad programs...
L. Flon