Problem z przekazaniem listy do funkcji

0

Witam serdecznie, Potrzebuje pomocy w przekazaniu listy do class, która jest rozszerzeniem klasy JPanel Próbowałem przekazywać Listę przez konstruktor ale nic z tego nie wychodziło ma może ktoś jakiś pomysł?

public class Rysowanie extends JPanel {
	

	
public void paintComponent(Graphics g) {
	
	  
	
	
		g.setColor(Color.orange);
		g.fillRect(56,70,50,50);

	}
	



    public Rysowanie() {
    	
        setPreferredSize(new Dimension(400, 400));
   
    			
    			
 }
}

PS: Klasa wewnętrzna nie działa

0

Wyglądało to jakoś tak zanim usunąłem

public class Rysowanie extends JPanel {
	private ArrayList<Cell> List;

	
public void paintComponent(Graphics g) {
	
	  
	for(Cell x : List) {
	
		g.setColor(Color.orange);
		g.fillRect(x.getX(),x.getY(),40,40);

	}
}



    public Rysowanie(ArrayList<Cell> ListaGłówna) {
    	this.List=ListaGłówna;
        setPreferredSize(new Dimension(400, 400));
   
    			
    			
 }
}

0

No i co Ci w tym nie pasuje?

0
szweszwe napisał(a):

No i co Ci w tym nie pasuje?

W tym problem nic nie rysuje a okno robi się całe czarne, kiedy zamieniam kod na ten który podałem w pierwszym wpisie wszystko działa dobrze.

0

No ale ja bym zakładał problem w metodzie paintComponent a nie w konstruktorze. Przecież w 1 poście ta metoda jest inna niż w tym kodzie podanym niżej. Z tych fragmentów wiadomo tylko tyle, że możesz przekazać listę tak jak przekazujesz w konstruktorze. A co tam dalej robisz z nią robisz to już nie bardzo wiadomo.

0

Tutaj jest reszta kodu bez klasy Cell

public class ramka extends JFrame {
	
	
	JButton button1, button2;
	JTextField textField1, textField2,textField3, textField4;
	JLabel label1, label2,label3, label4;
	ArrayList<Cell>ListaGłówna;

	static public void main(String[] args) {
		ArrayList<Cell> ListaGłówna=tworzenieListy(5,5);	
		connectingCells(ListaGłówna, 5);
		new ramka();
	}
			
			
			
public ramka()
	{
	
	  this.setLocationRelativeTo(null);
	  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  this.setTitle("My First Frame");
	  
	  
	  /////////////////////////////////Panel1///////////////////////////////////////////
	  JPanel thePanel1 = new JPanel();
	  //thePanel1.setLayout(new FlowLayout(FlowLayout.LEFT, 30,20));
	  Box theBox = Box.createHorizontalBox();
	  

	  label1 = new JLabel("Wiersz");
	  
	  
	  textField1 = new JTextField("5",5);
	  thePanel1.add(textField1);
	  
	  label2 = new JLabel("Kolumna");
	  thePanel1.add(label2);
	  
	  textField2 = new JTextField("5",5);
	  thePanel1.add(textField2);
	  
	  button1 = new JButton("Generuj");
	  ListenForButton lForButton =new ListenForButton();
	  button1.addActionListener(lForButton);
	  thePanel1.add(button1);
	  
	  
	  label3 = new JLabel("Start");
	  thePanel1.add(label3);
	  
	  textField3 = new JTextField("",5);
	  thePanel1.add(textField3);
	  
	  label4 = new JLabel("Koniec");
	  thePanel1.add(label4);
	  
	  textField4 = new JTextField("",5);
	  thePanel1.add(textField4);
	  
	  button2 = new JButton("Szukaj");
	  button2.addActionListener(lForButton);
	  
	  theBox.add(label1);
	  theBox.add(textField1);
	  theBox.add(label2);
	  theBox.add(textField2);
	  theBox.add(button1);
	  theBox.add(label3);
	  theBox.add(textField3);
	  theBox.add(label4);
	  theBox.add(textField4);
	  theBox.add(button2);
	  
	  thePanel1.add(theBox);
	  this.add(thePanel1, BorderLayout.SOUTH);
	  /////////////////////////////Panel2////////////////////////////////////////
	  	
	 Rysowanie thePanel2 = new Rysowanie(ListaGłówna);
	  
      this.add(thePanel2 ,BorderLayout.CENTER); 

	  this.pack();
	  this.setVisible(true);
	 
	}

	private class ListenForButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==button1) {
			repaint();
			}
			
		}
		
		
	}
	
	static public ArrayList<Cell> tworzenieListy( int x, int y ) {

		
		
		ArrayList<Cell> ListaGłówna=new ArrayList<Cell>(x*y);
		int wspX=0;
		int wspY=0;
		int wiersz=x;
		for(int i=1; i<=x*y; i++) {
			if(i==wiersz+1) {
				wspX=0;
				wspY+=40;
				wiersz+=wiersz;
			}
			Cell to = new Cell(i,wspX,wspY);
			ListaGłówna.add(to);
			wspX+=40;
		}
		return ListaGłówna;
	}
	
	static public void connectingCells(ArrayList<Cell> ListaGłowna, int x) {
		
		int wiersz=x;
		for(Cell tokio : ListaGłowna) {
			if(tokio.getName()==wiersz && tokio.getName()!=ListaGłowna.size() ){ // Połączenia dla ostatniej kolumny
				tokio.setConnectionForLastColumn(ListaGłowna, x);
				wiersz=wiersz+x;
			}
			else if(tokio.getName() >= (ListaGłowna.size()-x +1) && tokio.getName()!=ListaGłowna.size()) { //Połączenia dla ostatniego wiersza
				tokio.setConnectionForLastRow(ListaGłowna);
			}
			else if(tokio.getName()!=ListaGłowna.size()) {	 //Ustawnianie dla całej reszty 
				tokio.setConnectionForReminder(ListaGłowna, x);
			}
		}
	}
	
	

}
0

Klasa cell :

public class Cell {
	
	 int name;
	 private int old;
	 int wspX;
	 int wspY;
	 private boolean a;
	 private boolean b;
	 private boolean c;
	 private boolean d;
	 private ArrayList<Cell> list = new ArrayList<Cell>(4);
	 String image;
         /////////////////////////////////////// KONSTRUKTOR //////////////////////////////////////////////////////////////////////////////////////
	public Cell(int name, int wspX,int wspY) {
		this.name=name;
		this.wspX=wspX;
		this.wspY=wspY;
		
	}
             //////////////////////////////////////// USTAWIANIE I POBIERANIE ZMIENYCH ///////////////////////////////////////////////////////////////////
	public int getName() {
		return this.name;
	}
	public void setName(int x) {
		this.name=x;
	}
	public int getwspX() {
		return this.wspX;
	}
	public void setwspX(int x) {
		this.wspX=x;
	}
	public int getwspY() {
		return this.wspY;
	}
	public void setwspY(int x) {
		this.wspY=x;
	}
	
	public boolean getA() {
		return this.a;
	}
	public void setA(boolean x) {
		this.a=x;
	}
	public boolean getB() {
		return this.b;
	}
	public void setB(boolean x) {
		this.b=x;
	}
	public boolean getC() {
		return this.c;
	}
	public void setC(boolean x) {
		this.c=x;
	}
	public boolean getD() {
		return this.d;
	}
	public void setD(boolean x) {
		this.d=x;
	}
	
	
	
              ///////////////////////////////////////////ŁĄCZENIE KOMÓREK //////////////////////////////////////////////////////////////////////////////////	
	
	public void setConnectionForLastRow(ArrayList<Cell> ListaGłowna) {
		Random generator = new Random();
		int xx =generator.nextInt(101);
		
		if(xx>=70) {
			Cell taki =ListaGłowna.get(this.getName());
			this.list.add(taki);
			this.b=true;
			taki.list.add(this);
			taki.d=true;
		} 
	}
	public void setConnectionForLastColumn(ArrayList<Cell> ListaGłowna, int x) {
		Random generator = new Random();
		int xx =generator.nextInt(101);
		
		if(xx>=70) {
			Cell taki =ListaGłowna.get(this.getName()+x-1);
			this.list.add(taki);
			this.c=true;
			taki.list.add(this);
			taki.a=true;
		} 
	}
	public void setConnectionForReminder(ArrayList<Cell> ListaGłowna, int x) {
		Random generator = new Random();
		int xx =generator.nextInt(101);
		
		if(xx<40) { // łaczenie na lewo i dół 
			Cell taki =ListaGłowna.get(this.getName()+x-1);
			this.list.add(taki);
			taki.list.add(this);
			this.c=true;
			taki.a=true;
			
			taki =ListaGłowna.get(this.getName());
			this.list.add(taki);
			taki.list.add(this);
			this.b=true;
			taki.d=true;
			
		} else if(xx>40 && xx<=60) { // lączenie w dół
			Cell taki =ListaGłowna.get(this.getName()+x-1);
			this.list.add(taki);
			taki.list.add(this);
			this.c=true;
			taki.a=true;
			
		} else if(xx>60 && xx<=80) { // lączenie w lewo
			Cell taki =ListaGłowna.get(this.getName());
			this.list.add(taki);
			taki.list.add(this);
			this.b=true;
			taki.d=true;
		}
	}	
      	
	
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1