🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.java
2 messages
2 total messages Started by Artur Tyloch Sun, 24 Aug 1997 12:07
Dwa watko-zegarki
#879
Author: Artur Tyloch
Date: Sun, 24 Aug 1997 12:07
269 lines
7514 bytes
Czesc
Na podstawie przykladu sun'a (clock2)
napisalem aplet, ktory wyswietla dwa
zegary na jednym ma byc (to zrobie pozniej)
czas u czytajacego strone a na drugim czas serwera (DTime)
...nie chodzi mi jednak o te zegarki ale
o to by byl to program wielowatkowy (dokladnie trzy)
Watki:
 apletu
 zegarka1
 zegarka2

I niby to dziala ...ale gdy
ktos w netscape nacisnie back lub reload to pojawiaja sie smieci
[wyswietlanych stary zegar -ktorego powinno nie byc- i nowy, na nim]

{w IE dzieje sie tak tylko dla back.}

Wygla na to, ze watki zegara nie zostaja zabite mimo
przypisania 
watek = null; 
w metodzie stop apletu. 

Prosze o porade jak pozbyc sie tego niepozadanego efektu.
Aplet mozna zobaczyc w dzialaniu na stronie:
http://friko.onet.pl/po/arturt/TyloTwoClock/TwoClock.html


Prosze o uwagi dotyczace stylu pisania apletu wielowatkowego
lub inny pomysl jak zrobic to wielowatkowo.

Artur

/*
Netscape smieci wystepuja dla refresh i back
IE smieci tylko dla back  ----  WHY ?
*/
import java.util.*;
import java.awt.*;
import java.applet.*;

public class TwoClock extends Applet 
{
	static int licznik = 0; // variable for debug only
	Thread watek1 = null;
	Thread watek2 = null;

  	public void init() 
  
	{
		resize(300,300);             
  	}
  
	public void paint(Graphics g) 
	{	}

	public void start() 
	{
		if(watek1 == null);
		{
			licznik++;
						// tu bedzie obiekt typu Date odczytany 
						// { usluga DTime serwera }
						// z serwera gdzie jest przchowywany 
						// applet      \/ to tymczasowe 
			Rysuj rysuj = new Rysuj(this, 80,50, new
Date(97,11,12,11,21,32),true);
						//zagar dla serwera/\			
			watek1 = new Thread(rysuj);
			watek1.start();
		}
		if(watek2 == null);
		{					// czas lokalny \/
			Rysuj rysuj = new Rysuj(this, 200,50, new Date(),false);
			watek2 = new Thread(rysuj);
			watek2.start();
		}
	}

	public void stop() 
	{
		
		watek1 = null;
		watek2 = null;
		licznik--;	 // debug
		System.gc(); // nadmiarowo
	}

	public void update(Graphics g) 
	{
		paint(g);
	}
}

class Rysuj implements Runnable
{
	TwoClock m_CurrentApplet = null;
	int m_x,m_y;
	int m_nLastXS=0, m_nLastYS=0, m_nLastXM=0, m_nLastYM=0, m_nLastXH=0,
m_nLastYH=0;
	Date m_Dummy = null;
	String m_Lastdate = null;
	boolean m_bRemote;
	long m_lDifference = 0;

	Rysuj(TwoClock a, int x, int y, Date d, boolean r)
	{
		m_CurrentApplet = a;
		m_x = x;
		m_y = y;
		Date localD = new Date();
		if ( localD != d) // D E B U G \/ !!!
		{   
			// Zmienana m_lDifference sluzy do przechowania inforamcji o 
			// roznicy miedzy czasem lokalnym i na serwerze 
			// jest to zastosowane po to aby usluga DTime 
			// (odczyt czasu z serwera) byla wykonana tyko raz
			// na poczatku przed utworzeniem obiektu Rysuj(zegar_zdalny)
			m_lDifference = localD.getTime() - d.getTime();
			System.out.println(m_lDifference);
			
			System.out.println(localD.toString());
			System.out.println(d.toString());
			
			System.out.println(new Date(d.getTime()-m_lDifference).toString());
			System.out.println("*****************"+m_CurrentApplet.licznik);

		}
		m_Dummy = d;
		m_Lastdate = m_Dummy.toLocaleString();
		m_bRemote = r;
	}
	
	public void run()
	{
		while (true) 
		{
			try {Thread.sleep(100);} catch (InterruptedException e){}
			clockPaint(m_CurrentApplet.getGraphics());
			m_CurrentApplet.repaint();
		}		
	}	
	
	synchronized public void clockPaint(Graphics g) 
	{
		Date dat = new Date();
		if (m_bRemote) 
			dat= new Date(dat.getTime()-m_lDifference); 
	
drawClock(dat.getSeconds(),dat.getMinutes(),dat.getHours(),m_x,m_y,dat.toLocaleString(),g);
	}

	public void drawClock(int s, int m, int h, int xcenter, int ycenter,
String today, Graphics g)
	{
		int xh, yh, xm, ym, xs, ys;
	// a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  	// x = r(cos a) + xcenter, y = r(sin a) + ycenter
  
	  xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  	  ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  	  xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  	  ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  	  xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +
xcenter);
  	  yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +
ycenter);
  
  	  // Draw the circle and numbers
  
  	  g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
  	  g.setColor(Color.blue);
  	  circle(xcenter,ycenter,50,g);
  	  g.setColor(Color.darkGray);
  	  g.drawString("9",xcenter-45,ycenter+3); 
  	  g.drawString("3",xcenter+40,ycenter+3);
  	  g.drawString("12",xcenter-5,ycenter-37);
  	  g.drawString("6",xcenter-3,ycenter+45);

  	  // Erase if necessary, and redraw
  
  	  g.setColor(m_CurrentApplet.getBackground());
  
	  if (xs != m_nLastXS || ys != m_nLastYS) 
	  {
    	  g.drawLine(xcenter, ycenter, m_nLastXS, m_nLastYS);
   		  g.drawString(m_Lastdate, m_x, 125);
  	  }
  	  if (xm != m_nLastXM || ym != m_nLastYM) 
	  {
		  g.drawLine(xcenter, ycenter-1, m_nLastXM, m_nLastYM);
		  g.drawLine(xcenter-1, ycenter, m_nLastXM, m_nLastYM); 
	  }
  	  if (xh != m_nLastXH || yh != m_nLastYH) 
	  {
    	  g.drawLine(xcenter, ycenter-1, m_nLastXH, m_nLastYH);
		  g.drawLine(xcenter-1, ycenter, m_nLastXH, m_nLastYH); 
	  }
  	  g.setColor(Color.darkGray);
  	  g.drawString(today, m_x, 125);  
  	  g.drawLine(xcenter, ycenter, xs, ys);
  	  g.setColor(Color.blue);
  	  g.drawLine(xcenter, ycenter-1, xm, ym);
  	  g.drawLine(xcenter-1, ycenter, xm, ym);
  	  g.drawLine(xcenter, ycenter-1, xh, yh);
  	  g.drawLine(xcenter-1, ycenter, xh, yh);
  	  m_nLastXS=xs; m_nLastYS=ys;
  	  m_nLastXM=xm; m_nLastYM=ym;
  	  m_nLastXH=xh; m_nLastYH=yh;
  	  m_Lastdate = today;
  }
	// Plotpoints allows calculation to only cover 45 degrees of the
circle,
  // and then mirror

  public void plotpoints(int x0, int y0, int x, int y, Graphics g) 
  {
	  g.drawLine(x0+x,y0+y,x0+x,y0+y);
	  g.drawLine(x0+y,y0+x,x0+y,y0+x);
	  g.drawLine(x0+y,y0-x,x0+y,y0-x);
	  g.drawLine(x0+x,y0-y,x0+x,y0-y);
	  g.drawLine(x0-x,y0-y,x0-x,y0-y);
	  g.drawLine(x0-y,y0-x,x0-y,y0-x);
	  g.drawLine(x0-y,y0+x,x0-y,y0+x);
	  g.drawLine(x0-x,y0+y,x0-x,y0+y);
}

  // Circle is just Bresenham's algorithm for a scan converted circle

  public void circle(int x0, int y0, int r, Graphics g) 
  {
	  int x,y;
	  float d;  
	  
	  x=0;
	  y=r;
	  d=5/4-r;
	  plotpoints(x0,y0,x,y,g);

	  while (y>x)
	  {
		  if (d<0) 
		  {
			  d=d+2*x+3;
			  x++;
		  }
		  else 
		  {
			  d=d+2*(x-y)+5;
			  x++;
			  y--;
		  }
		  plotpoints(x0,y0,x,y,g);
	  }
  }
 
}





 
-- 
++++++++++++++++++++++++++++++++++++
+  My HOME PAGE:                   + 
+  http://friko.onet.pl/po/arturt  + 
+  **UNDER CONSTRUCTION !!!**      +
++++++++++++++++++++++++++++++++++++
Re: Dwa watko-zegarki
#880
Author: Artur Biesiadowski
Date: Fri, 29 Aug 1997 14:47
13 lines
937 bytes
Artur Tyloch <arturt@priv.onet.pl> wrote:

: Wygla na to, ze watki zegara nie zostaja zabite mimo
: przypisania 
: watek = null; 
: w metodzie stop apletu. 


Pomijajac kwestie back/forward w przegladarkach (powinny killowac
wszystkie watki), trzeba wydac polecenie
watek.stop();

Artur
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads