11 Replies - 277 Views - Last Post: 19 June 2012 - 11:35 PM Rate Topic: -----

#1 longhorn2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 12

Java Applets - Draws my image last?

Posted 19 June 2012 - 12:55 PM

Basically my code just outputs a string followed by periods until complete like below...


Task 1 ....................................Complete
Task 2 ....................................Complete


For some reason, it writes the strings and periods first, THEN outputs the image, and then writes the strings again...Any ideas why?

import javax.swing.JApplet;
import java.awt.*;
import java.applet.*;

public class myApplet extends JApplet
{
	Image image;
	public void init()
	{
		
        getContentPane().setBackground( Color.black );
        image = getImage(getCodeBase(), "picture.jpg");
	}
	 
	 
	public void paint (Graphics g)
	{
		
		super.paint(g);
		int column=180;
		int doneposition=350;
		
		g.drawImage(image,170,10,this);
		
		WaitNmilliseconds(2000);
		
		
		Font font = new Font("Arial",Font.PLAIN,20);
		g.setFont(font);
		String message;
		g.setColor(Color.white);
		WaitNmilliseconds(2000);
		g.drawString ("Task1", 5, 380);
		PeriodBar(g,column, 380);
		g.drawString ("Complete.", 700, 380);
		WaitNmilliseconds(2000);
		g.drawString ("Task2", 5, 410);
		PeriodBar(g, column, 410);
		g.drawString ("Complete.", 700, 410);
		

		
	}
	
	void PeriodBar(Graphics g, int columnnum, int row)
	{
	for(int i = 0; i<=34; i++)
		{
		g.drawString(".", columnnum, row);
		columnnum=columnnum+15;
		WaitNmilliseconds(300);
		
		}
	}
	void WaitNmilliseconds(int n)
	{
		try
		{
			// Sleep at least n milliseconds.
			// 1 millisecond = 1/1000 of a second.
			Thread.sleep( 300 );
		}
		catch ( InterruptedException e )
		{
			System.out.println( "awakened prematurely" );
		}
		
	}
	

}






Is This A Good Question/Topic? 0
  • +

Replies To: Java Applets - Draws my image last?

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 01:05 PM

Jussayin, you should change that 300 in your waitNMilliseconds method to n
Was This Post Helpful? 0
  • +
  • -

#3 longhorn2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 12

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 01:08 PM

thanks. still have the problem though
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 02:02 PM

The events you've programmed are occurring in the correct order, but it may not appear that way to you (or to anyone else), because they're not finishing or being displayed since the thread doing the work has been put to asleep. It's an interesting question that I think demonstrates the way Java works, but I'm not smart enough on the timing to give you a definite answer.

I do know that if you play around with your delays (remove some or all, move them around), you'll see variable results that will show you the events are occurring as you'd planned.

Start by adding a setsize( 1000, 500 ) to your init() method so you can see what's going on, and then comment out the delays in your paint() method. The delays in the paint() method are redundant anyway. Then, when the first delay is finally done, you'll see the image drawn and the first Task1 line being drawn.

I suggest instead using a Timer that will allow the program to continue executing at a set interval rather than freezing the whole JApplet for long periods of time.
Was This Post Helpful? 0
  • +
  • -

#5 longhorn2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 12

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 03:47 PM

That makes sense...Any suggestions on how to use a different timer?
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • View blog
  • Posts: 3,121
  • Joined: 05-April 11

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 05:07 PM

It seems like a JApplet calls the paint method twice when the applet is started. The image will be rendered the second time the paint method is called, and not in the first. I do not know why it acts this way.

You are doing a bunch of mistakes here, and I will try and explain them short.

Don't sleep the event thread!!!!!!!!!!!!!
You are doing so no other event can be processed or registered, have you tried and click the close button while the thread was sleeping?

There should be no logic in your paint method. The way you should be handling this would be:
Complete a tiny bit of the task -> repaint -> complete another tiny bit of the task -> repaint

With some good thread programming, you let a background thread work, and give a notice to the event thread to repaint when progress has been made.
Was This Post Helpful? 1
  • +
  • -

#7 longhorn2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 12

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 05:29 PM

View PostCasiOo, on 19 June 2012 - 05:07 PM, said:

It seems like a JApplet calls the paint method twice when the applet is started. The image will be rendered the second time the paint method is called, and not in the first. I do not know why it acts this way.

You are doing a bunch of mistakes here, and I will try and explain them short.

Don't sleep the event thread!!!!!!!!!!!!!
You are doing so no other event can be processed or registered, have you tried and click the close button while the thread was sleeping?

There should be no logic in your paint method. The way you should be handling this would be:
Complete a tiny bit of the task -> repaint -> complete another tiny bit of the task -> repaint

With some good thread programming, you let a background thread work, and give a notice to the event thread to repaint when progress has been made.


So you are saying I should call wait's outside of the paint method?
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 05:42 PM

This isn't pretty and I'm ashamed to post it, but I don't have time to completely redesign your program. Even though it doesn't produce exactly what you are trying to do (but it could) I threw it together to show you how a timer could be used, where the logic that controls what is drawn should be, and how that logic can modify what is being drawn. Change the image definition back to suit your needs.
import javax.swing.JApplet;
import javax.swing.Timer;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.applet.*;

public class myApplet extends JApplet
{
    Image image;
    Timer myTimer;
    Font font;
    String message;
    int count;
    int column;
    int row;
    boolean dots;

    public void init()
    {
        setSize( 1000, 500 );
        getContentPane().setBackground( Color.black );
        image = getImage(getCodeBase(), "./AlbumArtSmall.jpg");
        font = new Font("Arial",Font.PLAIN,20);
        message = "Task1";
        myTimer = new Timer( 500, new TimerAction() );
        myTimer.setDelay( 250 );
        myTimer.start();
    }

    public void paint (Graphics g)
    {
        super.paint(g);

        g.drawImage(image,170,10,this);

        g.setFont(font);
        g.setColor(Color.white);
        g.drawString ( message, 5, row );
        if ( dots )
        {
            for ( int i = 180 ; i < column ; i += 15 )
            {
                g.drawString(".", i, row);
            }
        }
    }

    class TimerAction implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            count++;
            
            if ( count < 34 )
            {
                dots = true;
                column = 180 + count * 15;
                row = 380;
            }
            else if ( count >= 34 && count < 39 )
            {
                message = "Task 1 Complete";
                row = 450;
                column = 380;
                dots = false;
            }
            else if ( count >= 40 && count < 74 )
            {
                dots = true;
                message = "Task2";
                column = 180 + ( count - 40 ) * 15;
                row = 410;
            }
            else if ( count >= 74 && count < 79 )
            {
                message = "Task 2 Complete";
                row = 450;
                column = 380;
                dots = false;
            }
            else if ( count >= 79 )
            {
                myTimer.stop();
            }
            repaint();
        }
    }
}

Don't tell anyone where you got it.
Was This Post Helpful? 1
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • View blog
  • Posts: 3,121
  • Joined: 05-April 11

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 06:20 PM

GregBrannon when you use the swing timer, he will still be working in the Event thread. Use the util timer instead of the swing timer and it would be fine :)

Also if you want to find inspiration about how to do work in background AND updating the gui, then you could take a look at the two android classes Asynctask and Handler. If you just read a few tutorials about how they work, you will have an idea of how to make it in your own program (though you can't use google's Asynctask and Handler classes ;) )
Was This Post Helpful? 0
  • +
  • -

#10 longhorn2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 12

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 09:46 PM

View PostGregBrannon, on 19 June 2012 - 05:42 PM, said:

This isn't pretty and I'm ashamed to post it, but I don't have time to completely redesign your program. Even though it doesn't produce exactly what you are trying to do (but it could) I threw it together to show you how a timer could be used, where the logic that controls what is drawn should be, and how that logic can modify what is being drawn. Change the image definition back to suit your needs.
import javax.swing.JApplet;
import javax.swing.Timer;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.applet.*;

public class myApplet extends JApplet
{
    Image image;
    Timer myTimer;
    Font font;
    String message;
    int count;
    int column;
    int row;
    boolean dots;

    public void init()
    {
        setSize( 1000, 500 );
        getContentPane().setBackground( Color.black );
        image = getImage(getCodeBase(), "./AlbumArtSmall.jpg");
        font = new Font("Arial",Font.PLAIN,20);
        message = "Task1";
        myTimer = new Timer( 500, new TimerAction() );
        myTimer.setDelay( 250 );
        myTimer.start();
    }

    public void paint (Graphics g)
    {
        super.paint(g);

        g.drawImage(image,170,10,this);

        g.setFont(font);
        g.setColor(Color.white);
        g.drawString ( message, 5, row );
        if ( dots )
        {
            for ( int i = 180 ; i < column ; i += 15 )
            {
                g.drawString(".", i, row);
            }
        }
    }

    class TimerAction implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            count++;
            
            if ( count < 34 )
            {
                dots = true;
                column = 180 + count * 15;
                row = 380;
            }
            else if ( count >= 34 && count < 39 )
            {
                message = "Task 1 Complete";
                row = 450;
                column = 380;
                dots = false;
            }
            else if ( count >= 40 && count < 74 )
            {
                dots = true;
                message = "Task2";
                column = 180 + ( count - 40 ) * 15;
                row = 410;
            }
            else if ( count >= 74 && count < 79 )
            {
                message = "Task 2 Complete";
                row = 450;
                column = 380;
                dots = false;
            }
            else if ( count >= 79 )
            {
                myTimer.stop();
            }
            repaint();
        }
    }
}

Don't tell anyone where you got it.


This is pretty close to what I want, but I want the messages to stay on the display as it progresses. The repaint() erases the task1..... each time
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10646
  • View blog
  • Posts: 39,539
  • Joined: 27-December 08

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 11:08 PM

Store your messages in an ArrayList<String>. Use a StringBuilder instance field, as well as an index field. Each time the Timer fires, append the new message to the StringBuilder. Then in paint(), draw the StringBuilder, invoking its toString() method to pass to the drawString() method.
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Java Applets - Draws my image last?

Posted 19 June 2012 - 11:35 PM

Quote

This is pretty close to what I want, but I want the messages to stay on the display as it progresses. The repaint() erases the task1..... each time

Yes, I left plenty for you to figure out and lots of room for improvement.

Combine Mac's suggestion with a change to the design that draws different messages in 3 message areas, 1 each at rows 380, 410, and 450.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1