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.......
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.......