Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: OS Specific Java Error

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default OS Specific Java Error

    Hello everyone,

    we are three students that do a introductory course in java programming. As part of the course that runs a whole year we do a project where we are to make a couple of games and a game-collection "magazine" which finds which games exist in a subfolder, presents them as options to the user and allows the user to select which game to play. Upon selection the selected game start.

    All games work fine separately, however when our Reversi game is started through the game-collection "magazine" we encounter an OS specific problem: under Linux and MacOS everything works fine, but under Windows the process of drawing the board and pieces stops after a few pieces have been laid out on the board. However, when the "magazine" is closed down, the pieces are suddenly drawn fine. We feel it`s beyond our skills to figure out why this problem occurs under Windows and hope that someone might be able to help us.


    The reversi game is started from the Magazine with the following piece of code

    if (file.exists()) {
     
    			try {
    				// Run program
    				String [] cmd = new String [] {"java","-jar",path};
    				Runtime.getRuntime().exec(cmd, null, null);
    			}
    			catch (IOException e) {
    				JOptionPane.showMessageDialog(this, "Det oppsto en feil:\n" + e, "Det oppsto en feil", JOptionPane.ERROR_MESSAGE);
    			}



    The PaintComponent in the games Panel-class draws everything:board,pieces,etc. and gives the user feedback on how many black and white pieces there are, if it is whites or blacks turn, if the game is finished.etc.. It looks like this:

    public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D)g;
    		this.setBackground(Color.WHITE);//Bakgrunnsfarge
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);// Bruk Anti-Alias
    		g2.setColor(Color.GRAY);
    		g2.fillRect(0,0,brettstorrelse,brettstorrelse);//Farge for visuelt spillebrett
    		//***************Finn antall gyldige trekk for den som er i trekket og om aktuelt for motstander
    		igyldigetrekk=0;mgyldigetrekk=0;
    		for(int rekke = 0 ; rekke < 8 ; rekke++ ) 
    		{
    			for(int kolonne = 0 ; kolonne < 8 ; kolonne++ ) 
    				{
    					if (kontroller.kontroller_om_gyldigtrekk(rekke,kolonne,itrekket)==1)igyldigetrekk++;
    				}
    		}
    		if (igyldigetrekk==0){//hvis ingen gyldige trekk, sjekk om motstander har trekk
    			for(int rekke = 0 ; rekke < 8 ; rekke++ ) 
    			{
    				for(int kolonne = 0 ; kolonne < 8 ; kolonne++ ) 
    				{
    					if (kontroller.kontroller_om_gyldigtrekk(rekke,kolonne,itrekket*-1)==1)mgyldigetrekk++;
    					}
    				}
    			}
    		if (igyldigetrekk==0&mgyldigetrekk==0)gyldigstatus=2;
    		else if (igyldigetrekk==0)gyldigstatus=1;
    		else gyldigstatus=0;
    		//*******************************************************************
    		if (gyldigstatus==2&(kontroller.antallsvartebrikker()+kontroller.antallhvitebrikker()!=64))//Hvis brettet ikke er fullt, men ingen har et gyldig trekk; avslutt spillet
    		{	
    			if (kontroller.antallhvitebrikker()>kontroller.antallsvartebrikker())
    				g2.drawString("Ingen har gyldige trekk, maskinen vant "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+". Trykk for nytt spill.",0,brettstorrelse+15);
    			else if (kontroller.antallhvitebrikker()<kontroller.antallsvartebrikker())
    				g2.drawString("Ingen har gyldige trekk, du vant "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+". Trykk for nytt spill.",0,brettstorrelse+15);
    			else g2.drawString("Ingen har gyldige trekk, det ble "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+". Trykk for nytt spill.",0,brettstorrelse+15);
    			gyldigstatus=9;//sett variabel for å nullstille brettet
    		}
    		else if (gyldigstatus==1&(kontroller.antallsvartebrikker()+kontroller.antallhvitebrikker()!=64))//Hvis brettet ikke er fullt, det finnes ikke gyldig trekk, men motstander har gyldig trekk: gi trekket til motstander
    		{
    			if(itrekket==1)g2.drawString("Ingen gyldige trekk, din tur! ",0,brettstorrelse+15);
    			if(itrekket==-1)g2.drawString("Ingen gyldige trekk, det er min tur! ",0,brettstorrelse+15);
    			itrekket=itrekket*-1;
    		}
    		else 
    		{
    		if (kontroller.antallsvartebrikker()+kontroller.antallhvitebrikker()==4)g2.drawString("Klarer du å slå maskinen i Othello? Du begynner som svart.",0,brettstorrelse+15);//Gi velkomstmelding om det er 4 brikker på brettet
    		else if (kontroller.antallsvartebrikker()+kontroller.antallhvitebrikker()==64)//Avslutt spillet om brettet er fullt
    			{	
    			if (kontroller.antallsvartebrikker()>32)g2.drawString("Du vant "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+"! Trykk for å starte et nytt spill.",0,brettstorrelse+15);
    			if (kontroller.antallhvitebrikker()>32)g2.drawString("Maskinen vant "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+"! Trykk for å starte et nytt spill.",0,brettstorrelse+15);
    			if (kontroller.antallsvartebrikker()==kontroller.antallhvitebrikker())g2.drawString("Det ble uavgjort "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+"! Imponerende. Trykk for å starte et nytt spill.",0,brettstorrelse+15);
    			}
    		else //Hvis spillet fortsetter, fortell hva stillingen er og hvem som er i trekket
    		{
    			if (itrekket==-1)g2.drawString("Stillingen er "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+"  Det er din tur.",0,brettstorrelse+15);
    			if (itrekket==1)g2.drawString("Stillingen er "+Integer.toString(kontroller.antallhvitebrikker())+"-"+Integer.toString(kontroller.antallsvartebrikker())+"  Maskinen sin tur.",0,brettstorrelse+15);
     
    		}
    	}
    	//Tegn visuelt spillebrett
    	g2.setColor(Color.BLACK);
    	for (int i=0;i<9;i++)
    	{
    		g2.drawLine(0,(brettstorrelse/8)*i,brettstorrelse,(brettstorrelse/8)*i);
    		g2.drawLine((brettstorrelse/8)*i,0,(brettstorrelse/8)*i,brettstorrelse);
    	}
     
     
    	//Tegn spillebrikker
    	for(int rekke = 0 ; rekke < 8 ; rekke++ ) 
    	{
    		for(int kolonne = 0 ; kolonne < 8 ; kolonne++ ) 
    		{
    			if (brett.hentstatus(kolonne, rekke) ==-1)
    				{
    					g2.setColor(Color.BLACK);
    					g2.fillOval(kolonne*(brettstorrelse/8)+2,rekke*(brettstorrelse/8)+2,(brettstorrelse/8)-4,(brettstorrelse/8)-4);
    					g2.setColor(Color.WHITE);
    					g2.drawArc(kolonne*(brettstorrelse/8)+2,rekke*(brettstorrelse/8)+2,(brettstorrelse/8)-4,(brettstorrelse/8)-4,-95,90);
    				}
    			if (brett.hentstatus(kolonne, rekke) ==1)
    				{
    					g2.setColor(Color.WHITE);
    					g2.fillOval(kolonne*(brettstorrelse/8)+2,rekke*(brettstorrelse/8)+2,(brettstorrelse/8)-4,(brettstorrelse/8)-4);
    				}
    		}	
    	}

    And we have
    	private class Listener implements MouseListener
    		{
    		public void mousePressed (MouseEvent event)
    			{
    			}
    		public void mouseReleased (MouseEvent event)
    			{
    			}
    		public void mouseEntered (MouseEvent event)
    			{
    			}
    		public void mouseExited (MouseEvent event)
    			{
    			}
    		public void mouseClicked (MouseEvent event)
    		{
     
    //A bunch of code removed here for clarity
    			repaint(); 
     
    		}
    	}


    Very grateful if anyone has any ideas on this.

    Best regards from Java Students
    University of Stavanger


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: OS Specific Java Error

    I didn't read your whole code. But from the problem description i am very sure about one thing.
    I am particularly talking about windows, when you try to draw objects with paint component, some of them drew but what actually happens is your buffer that doesn't clear everything before it could draw new objects. You should try to flush the buffer evertime you draw an object, so that memory could easily make place for new objects and could place objects over.
    You should try that and if problem still persists, post here so that we could have a look over that more closely.
    Regards...

Similar Threads

  1. JavaMail - How do I get a specific Header of an Email?
    By gsanchezbiz in forum Java SE APIs
    Replies: 4
    Last Post: December 13th, 2011, 02:27 PM
  2. OS Specific Jars
    By sinistaDev80 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 26th, 2011, 09:29 AM
  3. Selecting a specific list
    By sapzero in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2011, 12:06 PM
  4. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 27th, 2010, 11:22 PM
  5. Need some general and specific advice.
    By Morevan in forum Loops & Control Statements
    Replies: 2
    Last Post: January 3rd, 2010, 11:31 PM