Hello,
I am having a difficulty in passing a barrier of JAVA graphics generating. I hope you could explain some things to me, cause I have noone to help me and it is hard to get help in internet, because I feel like missing something important.

The thing is I cannot find an example of JAVA application which looks like similar to the one I am showing a picture to you.


This is what I already managed to do and I was really happy about it. The problematic thing is, I am totally stuck with making it work in a beautiful way, which I want to be: being able to make a user of the program see step-by-step line drawing (once he clicks a button after placing points, the program should paint each line every half a second or so). That would be a demonstration of the algorithms used. Now the only thing I could do was just to painting all lines at once just after the button is clicked. I am trying to put a Thread.sleep(500) in a lines drawing loop, but it just ignores that and runs to the result. I also try to add that sleep in an algorithm and repaint() everything all the time, but it still skips all my efforts and I cannot do anything.

I put a component Graphic(my written class) called G in my Main class. That Graphic has paint() method which paints all the points and lines. On PW(stands for package wrapping) button clicked I get the result of Lines I need to draw. Here is how it looks:

if (arg0.getSource() == pw) 
{
	PackageWrapping PW = new PackageWrapping(G);
	Vector<Line>BE = PW.lines(POINTS);
			
	G.transfer(POINTS, BE);
	G.repaint();		
}

I do not have anyone to turn to, who could give me a working example, explain my mistakes or just give any good advice.

I really expect you could help me. If you need any deeper information related to my code, just let me know.

You could use a Timer to help you draw your lines at specific intervals. You could also use Thread.sleep(), as you mentioned - if that isn't working there must be something wrong in your implementation. Can you post all of your (relevant) code, such as your repaint() method, everywhere you call repaint(), and where you attempted to put Thread.sleep()? And have you looked at this?

http://java.sun.com/docs/books/tutorial/essential/concurrency/sleep.html

Here is the code in PackageWrapping class:

public void show(Vector<Point>POINTS)
{
	Vector<Line>LINES = this.lines(POINTS);
	Vector<Line>PWLINES = new Vector<Line>();
	for (int i = 0; i < LINES.size(); i++)
	{
		PWLINES.add(LINES.get(i));
		this.G.transfer(POINTS, PWLINES);
		this.G.repaint();
		try
		{
			Thread.sleep(500);
		}
		catch (Exception e){}
	}
}

And here is how it looks in Graphic class:

public void paint(Graphics G)
{		
	G.setColor(Color.white);
	G.fillRect(0, 0, 400, 400);
		
	G.setColor(Color.red);
	for(int i = 0; i < this.LINES.size(); i++)
	{			
		Point A = this.LINES.get(i).A;
		Point B = this.LINES.get(i).B;			
			
		for (int j = 0; j < 5; j++)
		{
			G.drawLine(A.x-2+j, A.y, B.x-2+j, B.y);
			G.drawLine(A.x, A.y-2+j, B.x, B.y-2+j);
		}
	}
		
	G.setColor(Color.blue);
	for(int i = 0; i < this.POINTS.size(); i++)
	{			
		Point P = this.POINTS.get(i);
		G.fillOval(P.x-5, P.y-5, 10, 10);
	}
}

And here is how it is in Main class:

Graphic G;
....
public Main()
{
....
            G = new Graphic();					
	G.setBounds(50, 35, 400, 400);
	CONTAINER.add(G);
	G.addMouseListener(this);
.....
	G.repaint();
}
.....
public void actionPerformed(ActionEvent arg0) 
{
            if (arg0.getSource() == pw) 
	{
		PackageWrapping PW = new PackageWrapping(G);
		PW.show(POINTS);
	}
}

Hope I was clear enough. The funny thing is, that after I click the button pw, the program gets stuck (I sometimes put some System.out.println("whatever...") just to see that it perfectly is written each 500ms!!!), the button is like it was pressed and not released... and after it makes everything it just then shows the final result(without showing the middle results that I want so much). I did not implement anything related to Threads however. Where do I have to do that?

public class Main extends JFrame implements ActionListener, MouseListener
public class PackageWrapping
public class Graphic extends Canvas

Did I have to put anything more somewhere?

By the way, I've read a lot of articles related to Threads and they are working to me, but not working when I am trying to use them for my graphics!

Thank you for your help.

Another funny thing is that I found, that is: the loop(in the code below) is called the required amount of times, however that repaint() is not called here

for (int i = 0; i < LINES.size(); i++)
{
	PWLINES.add(LINES.get(i));
	this.G.transfer(POINTS, PWLINES);
	this.G.repaint();
	try
	{
		Thread.sleep(500);
	}
	catch (Exception e){}
}

I found that adding a simple System.out.println in a repaint() method and in that loop. It just completes the last repaint() and thats it. If only I knew how to solve this part...

The problem is that all of your code, including the thread.sleep() calls, are executing on the AWT event queue thread, which is the same thread that handles the repaint calls to update the display. You have to separate the animation updates from the painting. One way to do that would be to wrap the code you currently have in the show() method into a small Runnable class

class LineAnimation implements Runnable{
        public void run() {
            Vector<Line> LINES = this.lines(POINTS);
            Vector<Line> PWLINES = new Vector<Line>();
            for (int i = 0; i < LINES.size(); i++) {
                PWLINES.add(LINES.get(i));
                this.G.transfer(POINTS, PWLINES);
                this.G.repaint();
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
        }
    }

and start a new thread with that Runnable in your show method

Thread lineAnim = new Thread(new LineAnimation());
lineAnim.start();

As BestJewSinceJC mentioned, a Swing Timer can also be used for this.

commented: A very decent help to my problem! +1
public void show(Vector<Point>POINTS)
{
	Vector<Line> LINES = lines(POINTS);
	Thread lineAnim = new Thread(new LineAnimation(POINTS, LINES, this.G));
	lineAnim.start();		
}

and I made a class

import java.util.Vector;

class LineAnimation implements Runnable
{
	Vector<Point>POINTS;
	Vector<Line>LINES;
	Graphic G;
    public LineAnimation(Vector<Point>P, Vector<Line>L, Graphic G)
    {
    	this.LINES = L;
    	this.G = G;
    	this.POINTS = P;
    }
	
	public void run() {
        
        Vector<Line> PWLINES = new Vector<Line>();
        for (int i = 0; i < LINES.size(); i++) {
            PWLINES.add(LINES.get(i));
            this.G.transfer(POINTS, PWLINES);
            this.G.repaint();
            try {
                Thread.sleep(500);
            } catch (Exception e) {
            }
        }
    }
}

And it worked!!!!!!!

Thank you soooo much for your help!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.