illegal start of expression
i really don't know what's wrong with the program
and I was wondering if someone could tell me why I get this error?!
the code file is long ,please take it easy to read it
i really need help ,thanks everybody in advance......
- CODE START -
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class Dda extends JFrame
{
JButton refresh=new JButton("refresh");
Draw draw;
PanelControl panelControl;
public Dda()
{
super("DDA");
setSize(350,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
GridLayout grid = new GridLayout(1,2,5,15);
pane.setLayout(grid);
draw=new Draw();
paneControl = new PanelControl();
pane.add(draw);
pane.add(panelControl);
Container content=getContentPane();
content.add(pnae);
pack();
setVisible(true);
}
public static void main(String[] arguments)
{
Dda frame=new Dda();
}
public class Draw extends JPanel
{
Draw (PanelControl control)
{
int[] value = new int[4];
for (int i=0;i<4;i++)
value = Integer.parseInt(control.setting.getText());
int x0=value[0],y0=value[1],x1=value[2],y1=value[3];
public void paintComponent(Graphics comp)
{
Graphics2D comp2D=(Graphics2D)comp;
comp2D.setColor(Color.white);
Rectangle2D.Float background=new Rectangle2D.Float(0F,0F,
(float)getSize().width,(float)getSize().height);
comp2D.fill(background);
comp2D.setColor(Color.black);
GeneralPath line=new GeneralPath();
line.moveTo(x0,y0);
double k=(y1-x1)/(y0-x0);
for(int x=x0;x<=x1;x+=1)
{
line.lineTo((int)x,(int)(y0+0.5));
y=y+k;
}
comp2D.draw(line);
}
paint.repaint();
}
}
public class PanelControl extends JPanel implements
ActionListener,FocusListener
{
Dda frame;
JTextField[] setting = new JTextField[4];
PanelControl(Dda parent,String[] label)
{
Dda frame=parent;
for(int i=0;i<4;i++)
{
setting = new JTextField(" ");
setting.addFoucusListener(this);
setting.addAcionListener(this);
JLabel settingLabel = new JLabel(label);
add(settingLabel);
add(setting);
}
setVisible(true);
}
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() instanceof JTextField)
frame.draw(this);
}
public void focusLost(FocusEvent evt)
{
frame.draw(this);
}
public void focusGained(FocusEvent evt) { }
}
- CODE END -
Thanks in advance!!!
There's a code button you should use when you post code. Since you didn't use that, I'm not sure, but it looks like the constructor for the Draw class has a method definition inside it. You can not define a method inside a constructor.
o,that's right
thanks! but as i change my code file
and complie is ok ,but the display is not what i want
the graphiccan't appear right
and the new code file
code start --
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class Dda extends JFrame
{
PanelControl panelControl;
Draw draws;
public Dda()
{
super("DDA");
setSize(400,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BorderLayout grid = new BorderLayout();
pane.setLayout(grid);
String[] coordinate={"x0","y0","x1","y1"};
panelControl = new PanelControl(this,coordinate);
draws = new Draw();
pane.add(panelControl,"North");
pane.add(draws,"Center");
setContentPane(pane);
setVisible(true);
}
public static void main(String[] arguments)
{
Dda frame=new Dda();
}
//
public static int x0,y0,x1,y1;
void draw (PanelControl control)
{
int[] value = new int[4];
for (int i=0;i<4;i++)
value = Integer.parseInt(control.setting.getText());
x0=value[0];y0=value[1];x1=value[2];y1=value[3];
draws.repaint();
}
}
//class PanelControl
class PanelControl extends JPanel implements ActionListener,FocusListener
{
Dda frame;
JTextField[] setting = new JTextField[4];
PanelControl(Dda parent,String[] label)
{
Dda frame=parent;
GridLayout cGrid = new GridLayout(4,2,10,10);
setLayout(cGrid);
for(int i=0;i<4;i++)
{
setting = new JTextField(" ");
setting.addFocusListener(this);
setting.addActionListener(this);
JLabel settingLabel = new JLabel(label);
add(settingLabel);
add(setting);
}
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() instanceof JTextField)
frame.draw(this);
}
public void focusLost(FocusEvent evt)
{
frame.draw(this);
}
public void focusGained(FocusEvent evt) { }
}
class Draw extends JPanel
{
public void paintComponent(Graphics comp)
{
Graphics2D comp2D=(Graphics2D)comp;
comp2D.setColor(Color.white);
Rectangle2D.Float background=new Rectangle2D.Float(0F,0F,50F,50F);
comp2D.fill(background);
comp2D.setColor(Color.black);
GeneralPath line=new GeneralPath();
line.moveTo(Dda.x0,Dda.y0);
int k=(Dda.y1-Dda.x1)/(Dda.y0-Dda.x0),y=Dda.y0;
for(int x=Dda.x0;x<=Dda.x1;x+=1)
{
line.lineTo((int)x,(int)(y+0.5));
y=y+k;
}
comp2D.draw(line);
}
}
code end
expectinghelp!!!
> expectinghelp!!!Expecting you to use the code tags.
> o,that's right > thanks! but as i change my code file> and complie is ok ,but the display is not what i> want> the graphiccan't appear right > and the new code file How does it not appear right?
i am sorry about the code tags
my english is very poor
so i can't write the tags clearly ,but i try to do ti
thank you very much again!
the source file added tagsas follow:
-codestart--
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
// -- the main class
public class Dda extends JFrame
{
PanelControl panelControl;
Draw draws;
public Dda()
{
super("DDA");
setSize(400,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BorderLayout grid = new BorderLayout();
pane.setLayout(grid);
String[] coordinate={"x0","y0","x1","y1"};
panelControl = new PanelControl(this,coordinate);
draws = new Draw();
pane.add(panelControl,"North");
pane.add(draws,"Center");
setContentPane(pane);
setVisible(true);
}
public static void main(String[] arguments)
{
Dda frame=new Dda();
}
//-draw method
public static int x0,y0,x1,y1;
void draw (PanelControl control)
{
int[] value = new int[4];
for (int i=0;i<4;i++)
value = Integer.parseInt(control.setting.getText());
x0=value[0];y0=value[1];x1=value[2];y1=value[3];
draws.repaint();
}
}
//class PanelControl, it for enter the parameter of the graphics
class PanelControl extends JPanel implements ActionListener,FocusListener
{
Dda frame;
JTextField[] setting = new JTextField[4];
PanelControl(Dda parent,String[] label)
{
Dda frame=parent;
GridLayout cGrid = new GridLayout(4,2,10,10);
setLayout(cGrid);
for(int i=0;i<4;i++)
{
setting = new JTextField(" ");
setting.addFocusListener(this);
setting.addActionListener(this);
JLabel settingLabel = new JLabel(label);
add(settingLabel);
add(setting);
}
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() instanceof JTextField)
frame.draw(this);
}
public void focusLost(FocusEvent evt)
{
frame.draw(this);
}
public void focusGained(FocusEvent evt) { }
}
//--class Draw,it for draw the graphics
class Draw extends JPanel
{
public void paintComponent(Graphics comp)
{
Graphics2D comp2D=(Graphics2D)comp;
comp2D.setColor(Color.white);
Rectangle2D.Float background=new Rectangle2D.Float(0F,0F,50F,50F);
comp2D.fill(background);
comp2D.setColor(Color.black);
GeneralPath line=new GeneralPath();
line.moveTo(Dda.x0,Dda.y0);
int k=(Dda.y1-Dda.x1)/(Dda.y0-Dda.x0),y=Dda.y0;
for(int x=Dda.x0;x<=Dda.x1;x+=1)
{
line.lineTo((int)x,(int)(y+0.5));
y=y+k;
}
comp2D.draw(line);
}
}
> the source file added tagsas follow:Code tag.When you click the [code] button above the Message: areathat inserts code start and end tags [code] and [/code]insert your code between those tags to retain formatting.
sorry ,i just understand what you said
so i post the code again
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
// -- the main class
public class Dda extends JFrame
{
PanelControl panelControl;
Draw draws;
public Dda()
{
super("DDA");
setSize(400,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BorderLayout grid = new BorderLayout();
pane.setLayout(grid);
String[] coordinate={"x0","y0","x1","y1"};
panelControl = new PanelControl(this,coordinate);
draws = new Draw();
pane.add(panelControl,"North");
pane.add(draws,"Center");
setContentPane(pane);
setVisible(true);
}
public static void main(String[] arguments)
{
Dda frame=new Dda();
}
//-draw method
public static int x0,y0,x1,y1;
void draw (PanelControl control)
{
int[] value = new int[4];
for (int i=0;i<4;i++)
value = Integer.parseInt(control.setting.getText());
x0=value[0];y0=value[1];x1=value[2];y1=value[3];
draws.repaint();
}
}
//class PanelControl, it for enter the parameter of the graphics
class PanelControl extends JPanel implements ActionListener,FocusListener
{
Dda frame;
JTextField[] setting = new JTextField[4];
PanelControl(Dda parent,String[] label)
{
Dda frame=parent;
GridLayout cGrid = new GridLayout(4,2,10,10);
setLayout(cGrid);
for(int i=0;i<4;i++)
{
setting = new JTextField(" ");
setting.addFocusListener(this);
setting.addActionListener(this);
JLabel settingLabel = new JLabel(label);
add(settingLabel);
add(setting);
}
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() instanceof JTextField)
frame.draw(this);
}
public void focusLost(FocusEvent evt)
{
frame.draw(this);
}
public void focusGained(FocusEvent evt) { }
}
//--class Draw ,it for draw the graphics
class Draw extends JPanel
{
public void paintComponent(Graphics comp)
{
Graphics2D comp2D=(Graphics2D)comp;
comp2D.setColor(Color.white);
Rectangle2D.Float background=new Rectangle2D.Float(0F,0F,50F,50F);
comp2D.fill(background);
comp2D.setColor(Color.black);
GeneralPath line=new GeneralPath();
line.moveTo(Dda.x0,Dda.y0);
int k=(Dda.y1-Dda.x1)/(Dda.y0-Dda.x0),y=Dda.y0;
for(int x=Dda.x0;x<=Dda.x1;x+=1)
{
line.lineTo((int)x,(int)(y+0.5));
y=y+k;
}
comp2D.draw(line);
}
}
Here's some problems.
In the PanelControl class constructor, the line "Dda frame=parent;" is declaring a new variable named frame and assigning it to parent. However, you want to use the instance variable frame. You need to change the line to frame=parent;
In the Draw class, the line int k=(Dda.y1-Dda.x1)/(Dda.y0-Dda.x0),y=Dda.y0;
is causing a divide by zero. I didn't try to understand this code - you will have to debug to find out why Dda.y0-Dda.x0 equals zero. One way is to put in System.out.println() of the y0 and x0 values.
You will need to write code that checks whether the user has entered any data. I think it is not an error if the user has not entered anything. Then you also need to write code that checks the entered data to see if it is a valid numer or not. One way is to write the code in the Dda.draw method.
> sorry ,i just understand what you said
> so i post the code again
You missed the point.
Without code tags, the commonly used index "[i]" means italics
and it is "gobbled up" as formatting.
You just pasted code between the code tags that the forum formatting had already munged...
You *should* have pasted the original code - this would also have retained indentation, like so
[code]import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
// -- the main class
public class Dda extends JFrame
{
PanelControl panelControl;
Draw draws;
public Dda() {
super("DDA");
setSize(400,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BorderLayout grid = new BorderLayout();
pane.setLayout(grid);
String[] coordinate={"x0","y0","x1","y1"};
panelControl = new PanelControl(this,coordinate);
draws = new Draw();
pane.add(panelControl,"North");
pane.add(draws,"Center");
setContentPane(pane);
setVisible(true);
}
public static void main(String[] arguments) {
Dda frame=new Dda();
}
//-draw method
public static int x0,y0,x1,y1;
void draw(PanelControl control) {
int[] value = new int[4];
for (int i=0;i<4;i++)
value[i] = Integer.parseInt(control.setting[i].getText());
x0=value[0];y0=value[1];x1=value[2];y1=value[3];
draws.repaint();
}
}
//class PanelControl, it for enter the parameter of the graphics
class PanelControl extends JPanel implements ActionListener,FocusListener {
Dda frame;
JTextField[] setting = new JTextField[4];
PanelControl(Dda parent,String[] label) {
Dda frame=parent;
GridLayout cGrid = new GridLayout(4,2,10,10);
setLayout(cGrid);
for(int i=0;i<4;i++) {
setting[i] = new JTextField(" ");
setting[i].addFocusListener(this);
setting[i].addActionListener(this);
JLabel settingLabel = new JLabel(label[i]);
add(settingLabel);
add(setting[i]);
}
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() instanceof JTextField)
frame.draw(this);
}
public void focusLost(FocusEvent evt) {
frame.draw(this);
}
public void focusGained(FocusEvent evt) { }
}
//--class Draw ,it for draw the graphics
class Draw extends JPanel {
public void paintComponent(Graphics comp) {
Graphics2D comp2D=(Graphics2D)comp;
comp2D.setColor(Color.white);
Rectangle2D.Float background=new Rectangle2D.Float(0F,0F,50F,50F);
comp2D.fill(background);
comp2D.setColor(Color.black);
GeneralPath line=new GeneralPath();
line.moveTo(Dda.x0,Dda.y0);
int k=(Dda.y1-Dda.x1)/(Dda.y0-Dda.x0),y=Dda.y0;
for(int x=Dda.x0;x<=Dda.x1;x+=1) {
line.lineTo((int)x,(int)(y+0.5));
y=y+k;
}
comp2D.draw(line);
}
}
[/code]
thanks tschodt for telling me about the format
and thanks atmguy for reading the code
but the problem still exist
i change line:
Dda frame=parent
to
frame = parent
and change line:
setting = new JTextField(" ")
to
setting = new JTextField("1")
and then the program display asthe oldcode ;
> thanks
> ...
> but the problem still exist
> i change line:>Dda frame=parent[
> to>frame = parent
and change line:>setting[i] = new JTextField(" ")
> to >setting[i] = new JTextField("1")
> and then the program display asthe oldcode ;
If you delete the class files before you recompile, that should not be able to happen.
As for solving your problem - please state *clearly* exactly what the problem is...
What behaviour do you expect
and what behaviour do you observe.
The problem:
as you see ,
theclass Dda will great a frame
class Draw great a panel and the method paintComponent will draw
a graphics on the panel
class PanelControl great a Textfield forinter the parameters of
the graphics
The code compiled well
but there was only the Textfield could display
the graphics didn't.
I have said thatmy English is very poor
so I may notexpress the problem clearly
and I very sorry about that !
But i really need solve it ,thanks in advance !
I find the problemthe line int k=(Dda.y1-Dda.y0)/(Dda.x1-Dda.x0),y=Dda.y0it cout be run errorbut i have't find how to avoid this problem!
Something like this?int k=0,y=Dda.y0;if (Dda.x1 != Dda.x0) k=(Dda.y1-Dda.y0)/(Dda.x1-Dda.x0);
> Something like this?int k=0,y=Dda.y0;
> if (Dda.x1 != Dda.x0)
> k=(Dda.y1-Dda.y0)/(Dda.x1-Dda.x0);
I think this is one method to slove the problem!
thanks tschodt very much
the code is running well now!