JAVA Probleem.
Het probleem is namelijk dat ik bij de public vars x_pos en y_pos, waar ze ook maar gedefineerd staan, de error krijg, symbol not found. Het lijkt me toch dat ik alles goed geschreven heb?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CirkelMain extends JFrame {
public int x_pos = 100;
public int y_pos = 100;
public static void main( String args[] ) {
JFrame frame = new CirkelMain();
frame.setSize( 850, 400 );
frame.setLocation( 200, 200);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "Bewegende Cirkel" );
frame.setContentPane( new CirkelPaneel() );
frame.setVisible( true );
}
}
class CirkelPaneel extends JPanel {
private Cirkel cirkel;
private JButton knop0;
private JButton knop1;
private JButton knop2;
private JButton knop3;
public CirkelPaneel() {
setBackground( Color.PINK );
knop0 = new JButton( "Rechts" );
knop1 = new JButton( "Links" );
knop2 = new JButton( "Groter" );
knop3 = new JButton( "Kleiner" );
knop0.addActionListener( new RechtsKnopHandler() );
knop1.addActionListener( new LinksKnopHandler() );
knop2.addActionListener( new GroterKnopHandler() );
knop3.addActionListener( new KleinerKnopHandler() );
add( knop0 );
add( knop1 );
add( knop2 );
add( knop3 );
}
public void paintComponent( Graphics g ) {
super.paintComponent( g );
g.setColor ( Color.GREEN );
g.drawOval ( x_pos, y_pos, 100, 100 );
g.fillOval ( x_pos, y_pos, 100, 100 );
}
class RechtsKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
x_pos += 30;
repaint();
}
}
class LinksKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
x_pos -= 30;
repaint();
}
}
class GroterKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
y_pos += 30;
repaint();
}
}
class KleinerKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
y_pos -= 30;
repaint();
}
}
}
import java.awt.*;
import java.awt.event.*;
public class CirkelMain extends JFrame {
public int x_pos = 100;
public int y_pos = 100;
public static void main( String args[] ) {
JFrame frame = new CirkelMain();
frame.setSize( 850, 400 );
frame.setLocation( 200, 200);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "Bewegende Cirkel" );
frame.setContentPane( new CirkelPaneel() );
frame.setVisible( true );
}
}
class CirkelPaneel extends JPanel {
private Cirkel cirkel;
private JButton knop0;
private JButton knop1;
private JButton knop2;
private JButton knop3;
public CirkelPaneel() {
setBackground( Color.PINK );
knop0 = new JButton( "Rechts" );
knop1 = new JButton( "Links" );
knop2 = new JButton( "Groter" );
knop3 = new JButton( "Kleiner" );
knop0.addActionListener( new RechtsKnopHandler() );
knop1.addActionListener( new LinksKnopHandler() );
knop2.addActionListener( new GroterKnopHandler() );
knop3.addActionListener( new KleinerKnopHandler() );
add( knop0 );
add( knop1 );
add( knop2 );
add( knop3 );
}
public void paintComponent( Graphics g ) {
super.paintComponent( g );
g.setColor ( Color.GREEN );
g.drawOval ( x_pos, y_pos, 100, 100 );
g.fillOval ( x_pos, y_pos, 100, 100 );
}
class RechtsKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
x_pos += 30;
repaint();
}
}
class LinksKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
x_pos -= 30;
repaint();
}
}
class GroterKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
y_pos += 30;
repaint();
}
}
class KleinerKnopHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
y_pos -= 30;
repaint();
}
}
}
Maar zoals ik al zei kan de plank mis slaan, maar ik mis even de koppeling :)
Mvg,
Jacco
Dat klopt inderdaad. De variabelen y_pos en x_pos zijn buiten de scope van de klasse CirkelPaneel. Waarom heb je deze variabelen sowieso gedefineerd in de CirkelMain class?