Article View: es.comp.lenguajes.java
Article #29143Re: No es tan sencillo.. creo
From: Chano G�mez
Date: Sun, 18 Jan 1998 00:00
Date: Sun, 18 Jan 1998 00:00
340 lines
9275 bytes
9275 bytes
Este es un mensaje multipartes en formato MIME.
--------------EE71BAA8D052E827A4A9CF1F
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
> Si prefieres que te mande el c�digo comentado te lo mando en un ZIP.
>
Por lo visto, el sistema de coger un mismo hilo y arrancarlo, pararlo,
arrancarlo, pararlo, no va bien.
Sugerencia (ver�s que funciona, porque lo he probado): genera un nuevo objeto
Thread cada vez que se ejecute el m�todo start() del applet.
El bucle del run() debe ser algo as�:
============0
void run(){
while(true){
/* Aqu� haces vas mostrando las noticias... */
}
}
void start(){
flux = new Thread(this);
flux.start();
}
void stop(){
flux.stop();
}
Si quieres que el thread conserve su posici�n entre que lo arrancas y lo paras,
tendr�s que usar suspend() y resume(), en vez de crear un Thread nuevo cada vez.
Te mando el c�digo modificado.
Chano.
--------------EE71BAA8D052E827A4A9CF1F
Content-Type: text/plain; charset=iso-8859-1; name="tiempo_real.java"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline; filename="tiempo_real.java"
/////
/////
import java.applet.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.MediaTracker;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Component;
import java.io.DataInputStream;
import java.net.URL;
import java.net.URLConnection;
public class tiempo_real extends Applet implements Runnable{
private static final int WIDTH=129;
private static final int HEIGHT=189;
private Image bgImage;
private int bX,bY,wWidth,wHeight;
private Color[] colores;
// Variables que se refieren a las noticias
private String news[];
//private int news_width[];
private int news_len[];
private String news_hours[];
private String news_URL[];
private String fecha;
private int actualNew,totalNews;
int total=0;
private int value1,value2,value3;
private Thread flux=new Thread(this);
private boolean isAlive;
public void init(){
generateLogEntry();
// init coloured array
colores=new Color[8];
colores[0]=new Color(0,0,255);
colores[1]=new Color(32,32,255);
colores[2]=new Color(64,64,255);
colores[3]=new Color(128,128,255);
colores[4]=new Color(150,150,255);
colores[5]=new Color(182,182,255);
colores[6]=new Color(214,214,255);
colores[7]=new Color(255,255,255);
// init memory vars
this.news=new String[40+1];
this.news_URL=new String[40+1];
this.news_hours=new String[40+1];
this.news_len=new int[40+1];
inicializaEntorno();
conectar();
actualNew=0;
isAlive=false;
this.value1=100;
this.value2=5000;
this.value3=2*60*1000;
generateLogEntry();
}
public boolean handleEvent(Event ev) {
switch(ev.id) {
case Event.MOUSE_ENTER:showStatus("Expansi�n: tiempo real");break;
case Event.MOUSE_EXIT:showStatus("");break;
case Event.MOUSE_DOWN:click();
}
return false;
}
private void click() {
changeMouse(Frame.WAIT_CURSOR);
try {
showStatus("Expansi�n directo en tiempo real");
super.getAppletContext().showDocument(new URL(this.news_URL[actualNew]));
stop();
}
catch (Exception murle) {}
changeMouse(Frame.DEFAULT_CURSOR);
}
private void changeMouse(int mode) {
Object frame=getParent();
((Frame) frame).setCursor(mode);
}
public void update(Graphics g){
g.drawImage(bgImage,0,0,this);
}
public void paint(Graphics g){
g.drawImage(bgImage,0,0,this);
}
private void fade(int seq) {
Graphics gfx=bgImage.getGraphics();
Font f=new Font("TimesRoman",Font.PLAIN,12);
gfx.setFont(f);
FontMetrics fm=gfx.getFontMetrics(f);
int dateline=bY+fm.getHeight();
int fline=dateline+fm.getHeight()+2;
int steps=fm.getHeight()-2;
gfx.setColor(colores[7]);
gfx.fillRect(bX,bY,wWidth,wHeight); //clear window
gfx.setColor(colores[0]);
gfx.drawString(this.fecha,bX+8,dateline); // put date
gfx.setColor(colores[seq]);
gfx.setFont(f);
//put date and hour
gfx.drawString(news_hours[actualNew],wWidth-fm.stringWidth(news_hours[actualNew])-bX,dateline);
//put the new, but formated !
int line=0;
char car;
String last_word="";
int xline=bX+8;
for(int indexa=0;(indexa<this.news_len[actualNew]);indexa++) {
car=this.news[actualNew].charAt(indexa);
last_word+=car;
if((car==' ')||(indexa==this.news_len[actualNew]-1)) {
if((xline+fm.stringWidth(last_word))>=wWidth) {
line++;
xline=bX+8;
gfx.drawString(last_word,xline,fline+line*steps);
}
else {
gfx.drawString(last_word,xline,fline+line*steps);
}
xline+=fm.stringWidth(last_word);
last_word="";
}
}
repaint();
}
private Image imageRead(String name){
Image imagen=null;
try {
imagen=getImage(new URL(getCodeBase(),name));
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(imagen,0);
try {
tracker.waitForAll();
}
catch (InterruptedException ie) {}
}
catch (Exception e) {
}
return(imagen);
}
private void inicializaEntorno() {
Image tmp1,tmp2,tmp3,tmp4;
// draw all back images
tmp1=imageRead("applet1.gif");
tmp2=imageRead("applet2.gif");
tmp3=imageRead("applet3.gif");
tmp4=imageRead("applet4.gif");
bgImage=createImage(WIDTH,HEIGHT);
Graphics g=bgImage.getGraphics();
g.setColor(colores[7]);
g.fillRect(0,0,WIDTH,HEIGHT);
g.drawImage(tmp1,0,0,this);
g.drawImage(tmp3,0,tmp1.getHeight(this),this);
g.drawImage(tmp2,0,tmp1.getHeight(this)+tmp3.getHeight(this),this);
g.drawImage(tmp4,tmp1.getWidth(this)-tmp4.getWidth(this),tmp1.getHeight(this),this);
bX=tmp3.getWidth(this);
bY=tmp1.getHeight(this);
wWidth=WIDTH-bX-tmp4.getWidth(this);
wHeight=HEIGHT-bY-tmp2.getHeight(this);
}
private void conectar() {
String linea;
int cont=0;
generateLogEntry();
try {
// Declaramos lo que vamos a pillar
URL direccion=new URL(getDocumentBase(),"tiempo_real.htm"); //"http://challenge.recoletos.es/marca/m@rca/noticias.txt");
// Abrimos la conexion
URLConnection luv_conexion=direccion.openConnection();
// Declaramos el fichero
DataInputStream fichero=new DataInputStream(luv_conexion.getInputStream());
//this.text=getCodeBase();
this.fecha=fichero.readLine();
totalNews=0;
while (((linea=fichero.readLine())!=null)&&(cont<=40)){
if(linea.trim()=="") break;
this.news_hours[cont]=linea;
// the new
linea=fichero.readLine();
if((linea.trim()=="")||(linea==null)) break;
this.news[cont]=linea;
//this.news_width[cont]=this.fm.stringWidth(linea);
this.news_len[cont]=linea.length();
linea=fichero.readLine();
if((linea.trim()=="")||(linea==null)) {
this.news_URL[cont++]="";
break;
}
else
this.news_URL[cont++]=linea;
totalNews++;
} // del while
this.total=cont-1;
fichero.close();
} // del try
catch (Exception e){}
}
private void generateLogEntry() {
Image i=imageRead("/RealMedia/ads/adstream_nx.cgi/est_marca.recoletos.es/marca/marcador");
/* String name="";
try {
//http://www.recoletos.es/RealMedia/ads/adstream_nx.cgi/est_marca.recoletos.es/marca/marcador
imagen=getImage(new URL("/RealMedia/ads/adstream_nx.cgi/est_marca.recoletos.es/marca/marcador"));
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(imagen,0);
try {
tracker.waitForAll();
}
catch (InterruptedException ie) {}
}
catch (MalformedURLException me) {
System.out.println("MalformedURLException: " + me);
}
catch (IOException ioe) {
System.out.println("IOException: " + ioe);
}*/
}
public void run() {
while(true) {
long contador=0;
try {
for(int i=7;i>=0;i--) {
flux.sleep(100);
fade(i);
}
flux.sleep(5000);
if(contador++>=20) {
conectar();
contador=0;
}
for(int i=0;i<8;i++) {
flux.sleep(100);;
fade(i);
}
actualNew++;
if(actualNew==totalNews)
actualNew=0;
}
catch(Exception ie) {
stop();
}
}
}
public void start() {
flux=new Thread(this);
flux.start();
}
public void stop() {
flux.stop();
}
public void destroy() {
stop();
flux.destroy();
}
}
--------------EE71BAA8D052E827A4A9CF1F--
Technical Details
Message-ID:
<34C1CB42.D7B2CD32@ran.es>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<69g0jo$s80$1@eubuf21.eu.concert.net> <34BB96C8.F3FEF799@ran.es> <69ih8l$oue$1@eubuf21.eu.concert.net> <34BE89DB.FE070C87@ran.es> <69nt0u$eqh$1@eubuf21.eu.concert.net>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: 1116f3,6e177b47b509efd4 X-Google-Attributes: gid1116f3,public From: "Chano G�mez" <chano@ran.es> Subject: Re: No es tan sencillo.. creo Date: 1998/01/18 Message-ID: <34C1CB42.D7B2CD32@ran.es> X-Deja-AN: 317092672 References: <69g0jo$s80$1@eubuf21.eu.concert.net> <34BB96C8.F3FEF799@ran.es> <69ih8l$oue$1@eubuf21.eu.concert.net> <34BE89DB.FE070C87@ran.es> <69nt0u$eqh$1@eubuf21.eu.concert.net> Content-Type: multipart/mixed; boundary="------------EE71BAA8D052E827A4A9CF1F" Organization: Unisource Espana NEWS SERVER Mime-Version: 1.0 Newsgroups: es.comp.lenguajes.java