The Artima Developer Community
Sponsored Link

Java Answers Forum
a thread question

2 replies on 1 page. Most recent reply: Jul 16, 2004 8:01 PM by KevinT

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
fengjia

Posts: 1
Nickname: mickeymiki
Registered: Jul, 2004

a thread question Posted: Jul 11, 2004 11:31 AM
Reply to this message Reply
Advertisement
i want to use a thread make the message flash,and i write a test application as below,but it could not work as i want. could someone tell me what's wrong? thanks a lot

import java.awt.*;
import javax.swing.*;
public class test {
public static void main(String[] args) {
testFrame frame = new testFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

/**
A frame that contains a message panel
*/
class testFrame extends JFrame
{
public testFrame() {
setTitle("test");
setSize(WIDTH, HEIGHT);
//add panel to frame
testPanel panel = new testPanel();
Thread t = new Thread(panel);
t.start();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
}

/**
A panel that displays a message
*/
class testPanel extends JPanel implements Runnable
{
int flag = 1;
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (flag == 1)
g.setColor(Color.BLUE);
else
g.setColor(Color.yellow);
g.drawString("test", MESSAGE_X, MESSAGE_Y);
}

public void run() {
flag = -flag;
try {
Thread.sleep(250);
}
catch (InterruptedException e) {
}
}
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 100;
}


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: a thread question Posted: Jul 14, 2004 9:40 PM
Reply to this message Reply
import java.awt.*;
import javax.swing.*;
public class test {
    public static void main(String[] args) {
	testFrame frame = new testFrame();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.show();
    }
}
 
/**
   A frame that contains a message panel
*/
class testFrame extends JFrame
{
    public testFrame() {
	setTitle("test");
	setSize(WIDTH, HEIGHT);
	//add panel to frame
	testPanel panel = new testPanel();
	Container contentPane = getContentPane();
	contentPane.add(panel);
	new Thread(panel).start();
    }
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
}
 
/**
   A panel that displays a message
*/
class testPanel extends JPanel implements Runnable
{
    int flag = 1;
    public void paintComponent(Graphics g) {
	super.paintComponent(g);
 
	if (flag == 1)
	    g.setColor(Color.BLUE);
	else
	    g.setColor(Color.yellow);
	g.drawString("test", MESSAGE_X, MESSAGE_Y);
    }
    
    public void run() {
	try {
	    while(true){
		flag = -flag;
		Thread.sleep(250);
		repaint();
	    }
	}
	catch (InterruptedException e) {
	}
    }
 
    public static final int MESSAGE_X = 75;
    public static final int MESSAGE_Y = 100;
}



PS : Please learn how to post.

KevinT

Posts: 24
Nickname: metzedler
Registered: Jun, 2003

what's wrong Posted: Jul 16, 2004 8:01 PM
Reply to this message Reply
I tested it, it works pretty fine, i mean the text does flash.
So isn't that what you expected ?

Flat View: This topic has 2 replies on 1 page
Topic: connecting game-port to the java program Previous Topic   Next Topic Topic: gaming in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use