public class Experement1 extends JApplet{ int[] arr = new int[16]; arr[0] = 1; int x1 = Integer.valueOf(JOptionPane.showInputDialog("Введите 1 елемент матрицы")); int x6 = Integer.valueOf(JOptionPane.showInputDialog("Введите 5 елемент матрицы")); int x11 = Integer.valueOf(JOptionPane.showInputDialog("Введите 11 елемент матрицы")); int x16 = Integer.valueOf(JOptionPane.showInputDialog("Введите 16 елемент матрицы")); public void paint(Graphics g){ super.paint(g); g.setColor(Color.red); for(int i=0;i<200;i+=100){ for(int k=0;k<200;k+=100){ g.drawOval(i, k, 10, 10); } if(x1==1){ g.drawOval(9, 0, 20, 20); g.drawLine(9, 9, 10, 20); g.drawLine(9, 9, 20, 10); } if(x6==1){ g.drawOval(109, 0, 20, 20); g.drawLine(109, 9, 110, 20); g.drawLine(109, 9, 120, 10); } if(x11==1){ g.drawOval(9, 100, 20, 20); g.drawLine(9, 109, 10, 120); g.drawLine(9, 109, 20, 110); } if(x16==1){ g.drawOval(109, 100, 20, 20); g.drawLine(109, 109, 110, 120); g.drawLine(109, 109, 120, 110); } } } } 

enter image description here


Vseravno error !!! Help pliz !!! enter image description here

enter image description here

  • make a mistake in the form of text, not a screenshot - pavlofff

2 answers 2

It would be better for you, of course, to transfer the logic of initializing the arr array to the constructor.

But you can use a non-static initialization block:

 public class Experement1 extends JApplet { int[] arr; { arr = new int[16]; arr[0] = 1; } ... } 
  • Vseravno error - coder228

Oh. That's not the point. You are accessing an element of an array outside the method. That's the trouble.

 int[] arr = new int[16]; arr[0] = 1; 

Transfer arr[0] = 1; to the beginning of the public void paint(Graphics g) method.