Results 1 to 12 of 12
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default ending a graphics class

    Im making a program which has a level editor. I finally completed the editor itself with some help of members of this forum (thanks again). Yet I found another problem. My level editor should return an ArrayList<Point> to the calling methode when done, however the methode I called in editor just made a window, some listeners and some graphics and then just kept listening for mouseclicks.

    I have a methode in the editor class that is called when the user clicks the "done with my level"-button. I want that when the user clicks that button that the window closes, control is returned to the calling methode and that the calling methode gets the ArrayList<Point>. I could just get the ArrayList<Point> with an get methode, but I need help with the other 2 things.

    Code of the editor class with names in Dutch (I can translate things if necesary):

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class editor extends JFrame implements MouseListener, ActionListener
    {
    	private static int veldGrootte=30, sizeVakjes=21, bovenbalk=53, zijkant=4,
    	onderbalk=0;
    	private vakje[][] speelveld;
    	private JMenuBar bar=new JMenuBar();
    	private JMenu opties=new JMenu("opties"), klaarMetEditor=new JMenu("klaar");
    	private JMenuItem reset=new JMenuItem("reset"), repaint=new JMenuItem("repaint")
    	, undo=new JMenuItem("undo"), 
    	editorAfsluitenEnSpelen=new JMenuItem("editor afsluiten en spelen");
    	private boolean paintAlles=true, afsluiten=false;
    	private vakje vakjeTeTekenen;
    	private String status="kasteel";
    	private ArrayList<Point> route=new ArrayList<Point>();
    	private Point eindpunt;
    	
    	/*empty constructor, I wanted to do everything in a methode 
    	 * in an attempt to get a return statement.
    	*/
    	public editor()
    	{
    		
    	}
    	//The methode I call when I want the editor to start, first this was all in the constructor.
    	public void editorBeginnen()
    	{
    		setTitle("tower defence");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(veldGrootte*sizeVakjes+2*zijkant,
    				sizeVakjes*veldGrootte+bovenbalk+zijkant+onderbalk);
    		setLocation(0,0);
    		getContentPane().setBackground(Color.white);
    		speelveld=new vakje[veldGrootte][veldGrootte];
    		for(int i=0; i<veldGrootte;i++)
    		{
    			for(int j=0;j<veldGrootte;j++)
    			{
    				speelveld[i][j]=new vakje(sizeVakjes, i, j, true, zijkant, bovenbalk);
    			}
    		}
    		((Component) getContentPane()).addMouseListener(this);
    		opties.add(reset);
    		opties.add(undo);
    		opties.add(repaint);
    		klaarMetEditor.add(editorAfsluitenEnSpelen);
    		bar.add(opties);
    		setJMenuBar(bar);
    		reset.addActionListener(this);
    		repaint.addActionListener(this);
    		undo.addActionListener(this);
    		klaarMetEditor.addActionListener(this);
    		setVisible(true);
    	}
    	public void paint(Graphics g)
    	{
    		if(paintAlles)
    		{
    			super.paintComponents(g);
    			for(int i=0; i<veldGrootte;i++)
    			{
    				for(int j=0;j<veldGrootte;j++)
    				{
    					speelveld[i][j].paint(g);
    				}
    			}
    		}
    		else
    		{
    			if(vakjeTeTekenen!=null)
    			{
    				vakjeTeTekenen.paint(g);
    			}
    			paintAlles=true;
    			vakjeTeTekenen=null;
    		}
    	}
    	public void mouseClicked(MouseEvent e) {
    		int horizontaal=e.getX()/sizeVakjes;
    		int verticaal=e.getY()/sizeVakjes;
    		if(!(horizontaal>29||verticaal>29))
    		{
    			if(status.equals("kasteel"))
    			{
    				kasteelPlaatsen(speelveld[horizontaal][verticaal]);
    			}
    			if(status.equals("beginpunt"))
    			{
    				beginpuntPlaatsen(speelveld[horizontaal][verticaal]);
    			}
    			if(status.equals("route"))
    			{
    				routeVakjesPlaatsen(speelveld[horizontaal][verticaal]);
    			}
    		}
    	}
    	//last if statement is true when the user clicks to exit the editor
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource()==reset)
    		{
    			reset();
    		}
    		if(e.getSource()==repaint)
    		{
    			repaint();
    		}
    		if(e.getSource()==undo)
    		{
    			undo();
    		}
    		if(e.getSource()==klaarMetEditor)
    		{
    			
    		}
    	}
    	public void undo()
    	{
    		if(status.equals("klaar"))
    		{
    			route.remove(route.size()-1);
    			status="route";
    			bar.remove(klaarMetEditor);
    		}
    		if(status.equals("route"))
    		{
    			vakje teVerwijderenVakje=speelveld[(int) route.get(route.size()-1).getX()]
    			             			          [(int) route.get(route.size()-1).getY()];
    			teVerwijderenVakje.reset();
    			route.remove(route.size()-1);
    			if(route.size()==0)
    			{
    				status="beginpunt";
    			}
    			repaint();
    		}
    		else
    		{
    			reset();
    		}
    	}
    	public void reset()
    	{
    		route=new ArrayList<Point>();
    		if(status.equals("klaar"))
    		{
    			bar.remove(klaarMetEditor);
    		}
    		status="kasteel";
    		for(int i=0; i<veldGrootte;i++)
    		{
    			for(int j=0;j<veldGrootte;j++)
    			{
    				speelveld[i][j].reset();
    			}
    		}
    		repaint();
    	}
    	public void kasteelPlaatsen(vakje gekliktVakje)
    	{
    		int x=gekliktVakje.getHorizontaleLocatie();
    		int y=gekliktVakje.getVerticaleLocatie();
    		if(gekliktVakje.getStatus()==null&&x!=0&&x!=(veldGrootte-1)&&y!=0&&y!=(veldGrootte-1))
    		{
    			gekliktVakje.setKasteelEindpunt();
    			speelveld[x-1][y].setKasteelMuur();
    			speelveld[x+1][y].setKasteelMuur();
    			speelveld[x-1][y+1].setKasteelMuur();
    			speelveld[x][y+1].setKasteelMuur();
    			speelveld[x+1][y+1].setKasteelMuur();
    			eindpunt=new Point(x,y-1);
    			status="beginpunt";
    			repaint();
    		}
    	}
    	public void beginpuntPlaatsen(vakje gekliktVakje)
    	{
    		int x=gekliktVakje.getHorizontaleLocatie();
    		int y=gekliktVakje.getVerticaleLocatie();
    		if(gekliktVakje.getStatus()==null&&(x==0||x==(veldGrootte-1)||y==0||y==(veldGrootte-1)))
    		{
    			gekliktVakje.setBeginpunt();
    			Point p=new Point(x,y);
    			route.add(p);
    			if(p.equals(eindpunt))
    			{
    				Point q=new Point((int)eindpunt.getX(), (int) (eindpunt.getY()+1));
    				route.add(q);
    				status="klaar";
    				bar.add(klaarMetEditor);
    			}
    			else{
    			status="route";
    			}
    			if(!((status.equals("klaar"))))
    			{		
    			paintAlles=false;
    			vakjeTeTekenen=gekliktVakje;
    			}
    			repaint();
    		}
    	}
    	public void routeVakjesPlaatsen(vakje gekliktVakje)
    	{
    		if(gekliktVakje.getStatus()==null)
    		{
    			int x=gekliktVakje.getHorizontaleLocatie();
    			int y=gekliktVakje.getVerticaleLocatie();
    			Point vorige=route.get(route.size()-1);
    			if((vorige.getX()==x&&vorige.getY()==(y+1))
    					||(vorige.getX()==x&&vorige.getY()==(y-1))
    					||(vorige.getX()==(x+1)&&vorige.getY()==(y))
    					||(vorige.getX()==(x-1)&&vorige.getY()==(y)))
    			{
    				gekliktVakje.setRoute();
    				Point p=new Point(x,y);
    				route.add(p);
    				if(p.equals(eindpunt))
    				{
    					Point q=new Point((int)eindpunt.getX(), (int) (eindpunt.getY()+1));
    					route.add(q);
    					status="klaar";
    					bar.add(klaarMetEditor);
    					setJMenuBar(bar);
    				}
    				if(!((status.equals("klaar"))))
    				{		
    				paintAlles=false;
    				vakjeTeTekenen=gekliktVakje;
    				}
    				repaint();
    			}
    		}
    	}
    	public void mouseEntered(MouseEvent arg0) {
    	}
    	public void mouseExited(MouseEvent arg0) {	
    	}
    	public void mousePressed(MouseEvent arg0) {	
    	}
    	public void mouseReleased(MouseEvent arg0) {
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code is a bit much for me to go through completely, but I think your biggest problem is creating a JFrame rather than use a modal JDialog which has the behavior you desire: after the dialog has been dealt with, program flow returns to the calling program at the location just after where the dialog was set visible.

    Your other major problem is that you're trying to do drawing and other things in a top-level container (here a JFrame, but a JDialog would be just as bad), rather than drawing in a JPanel's paintComponent method and then then adding that JPanel to the contentPane of a top-level container.

    edit: I'll see if I can look at your code some more if you so desire.
    Last edited by Fubarable; 08-14-2010 at 01:46 AM.

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your code is a bit much for me to go through completely, but I think your biggest problem is creating a JFrame rather than use a modal JDialog which has the behavior you desire: after the dialog has been dealt with, program flow returns to the calling program at the location just after where the dialog was set visible.

    Your other major problem is that you're trying to do drawing and other things in a top-level container (here a JFrame, but a JDialog would be just as bad), rather than drawing in a JPanel's paintComponent method and then then adding that JPanel to the contentPane of a top-level container.

    edit: I'll see if I can look at your code some more if you so desire.
    Well, I'm living in the Netherlands, so it''s now 2 am here, so I'm going to bed. I might redo the graphics completly. Now I just say paint a polygon at these coordinates. This works great when I just wanted to daw something, but it created some mayor difficulties. For example when clicking a square, I need to calculate which square "lives" on that pixel and then send the message to that square. A better way would be to somehow add the squares as objects and then when clicked just use a methode that returns the uppermost object on screen. This is how they did it at the stanford programming methodology lectures on youtube, only they used their own librarys.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    private vakje[][] speelveld;
    What is vakje, and speelveld?



    What this methods should do and what is gekliktVakje?
    Java Code:
    public void kasteelPlaatsen(vakje gekliktVakje)
    
    public void beginpuntPlaatsen(vakje gekliktVakje)
    
    public void routeVakjesPlaatsen(vakje gekliktVakje)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default

    Try the Google Translator. Many languages to many others.
    vakje gekliktVakje to English: box box clicked

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I need to calculate which square "lives" on that pixel and then send the message to that square.
    On this forum there are few little programs that I have written with that listener.
    It is not difficult.
    Link for one:
    Draw circles, select circles



    This is "which circle lives on that pixel". Try to write similar method for squares. In the method selectCircle number 4 means that you can click not just on the right one pixel but also on the pixel that is maximum 4 pixels away from pixels that is on the circle.
    Java Code:
    [COLOR="Blue"]public boolean selectCircle(Point point) {
    		int newRadius = (int)center.distance(point);
    		if(Math.abs(radius - newRadius) < 4)
    			return true;
    		
    		return false;
    	}[/COLOR]
    	
    	public void setSelectedCircle(boolean set) {
    		selected = set;
    	}
    	
    	public boolean getSelectedCirlce() {
    		return selected;
    	}
    Last edited by cselic; 08-14-2010 at 03:12 AM.

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Try the Google Translator.
    It is nothing personal, but I don't like google. I'm yahoo/bing user. :o

  8. #8
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    ok, imagine this code:
    Java Code:
    public class tester{
    	public static void main(String [] args)
    	{
    		new window
    		//some code
    	}
    }
    Java Code:
    public class window extends JFrame{
    private JButton exit=new JButton("exit");
    	public window
    	{
    		setTitle("window");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(100,100)
    		setLocation(0,0);
    		getContentPane().setBackground(Color.white);
    		add(exit);
    		exit.addActionListener(this);
    		setVisible(true);
    	}
    	public void actionPreformed(ActionEvent e)
    	{
    		if(e.getSource()==exit)
    		{
    			//exit code
    		}
    	}
    }
    Now, I want that when "//exit code" is reached (if my syntax is correct, that happens when the button is pressed, but that is not important here), that somehow the window class is exited and the tester class continues to excecute "//some code". My question is, could you help me write "//exit code"?
    Last edited by imorio; 08-16-2010 at 05:53 PM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default

    window class is exited
    What does 'exited' mean?
    Did you post the correct code? The window class you show does NOT have the methods you call in its constructor: setTitle for example.
    I assume you left out the "extends JFrame" part.
    The window class object will continue to exist. You can make the window invisible by calling the setVisible() method.

    the tester class continues to excecute
    The window constructor will return to the tester main() method and execute the // some code

    Add some println() statements to the code to show where execution flow goes.
    Last edited by Norm; 08-16-2010 at 05:54 PM.

  10. #10
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Yes, I forgot the extends "JFrame" part. My problem isn't that I want the window gone, I want that somehow the program starts excecuting "//some code" when the button is clicked. With my current knowledge, "//some code" is unreachable code.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default

    If you add print statements to your code you'll see when the '// some code' is being executed.
    To execute '// some code' on a button click, you need to call it from the action listener code.
    Make it a method that contains '// some code' and have the tester code pass a this reference to the window class who saves it for the action listener to be able to use to call the method.

  12. #12
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    It worked! I wanted to make a second window in "//some code", but because of one error, that window apeared very small and another error created a whole list of exceptions when making that window. I therefor assumed it had something to do with the first window still being there. However both bugs were found and solved when you pointed out it couldn't be the first window causing the problems. Thank you!

Similar Threads

  1. Help with 2d graphics please
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 12:48 PM
  2. [SOLVED] Ending for loop with a String
    By jdiesel in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 03:11 AM
  3. Problem in g.drawArc() api of Graphics class
    By narasimman in forum AWT / Swing
    Replies: 0
    Last Post: 07-30-2008, 09:19 AM
  4. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 07:24 PM
  5. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •