当前位置: 技术问答>java相关
关于多线程,急急急!
来源: 互联网 发布时间:2015-11-09
本文导语: 如何在一个canvas上通过两个独立的线程画两个不断闪烁的圆。 给出关键部分代码 | import javax.swing.*; import javax.swing.border.EtchedBorder; import java.awt.*; import java.awt.event.*; interface MyInitScheme { void...
如何在一个canvas上通过两个独立的线程画两个不断闪烁的圆。
给出关键部分代码
给出关键部分代码
|
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
interface MyInitScheme {
void initVarient();
void initFace();
}
class MyMenu extends JMenu implements MyInitScheme {
public MyMenu(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyMenuItem extends JMenuItem implements MyInitScheme {
public MyMenuItem(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyThread extends Thread {
private Component p;
private boolean stop=false;
public void run() {
while(!stop) {
try {
sleep(200);
} catch (InterruptedException e) {
System.out.println(e);
}
p.repaint();
}
}
public void stopThread() {
stop=true;
}
public void start() {
stop=false;
super.start();
}
public MyThread(Component cp) {
p=cp;
}
}
class MyFrame extends JFrame implements MyInitScheme {
class MyContent extends JPanel {
int x=10,y=10,r=30;
public void paintComponent(Graphics g) {
super.paintComponent(g);
java.util.Random rand=new java.util.Random();
r=10+rand.nextInt(90);
g.drawOval(x, y, r, r);
}
public MyContent() {
setBackground(SystemColor.activeCaption);
}
}
JPanel p1=new MyContent();
JPanel p2=new MyContent();
private JMenuBar mnuBar = new JMenuBar();
private JMenu mnuFile = new MyMenu("File");
private JMenuItem mnuFileExit = new MyMenuItem("Exit");
private JMenu mnuHelp = new MyMenu("Help");
private JMenuItem mnuHelpContact = new MyMenuItem("Contact Me");
public void initVarient() {};
public void initFace() {
this.setSize(new Dimension(320, 240));
this.setResizable(false);
getContentPane().setLayout(null);
getContentPane().setBackground(SystemColor.activeCaption);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mnuBar.setBackground(SystemColor.desktop);
mnuBar.setBorder(BorderFactory.createRaisedBevelBorder());
mnuFile.setMnemonic('F');
mnuFileExit.setMnemonic('x');
mnuFileExit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} );
mnuHelp.setMnemonic('H');
mnuHelpContact.setMnemonic('M');
mnuHelpContact.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
try {
Process p=Runtime.getRuntime().exec("C:/Program Files/"
+"Internet Explorer/IExplore.EXE"
+" mailto:zhangjiawang0809@sohu.com");
} catch (Exception ex) {
System.out.println(ex);
}
}
} );
mnuFile.add(mnuFileExit);
mnuHelp.add(mnuHelpContact);
mnuBar.add(mnuFile);
mnuBar.add(mnuHelp);
this.setJMenuBar(mnuBar);
p1.setBounds(new Rectangle(0,0,150,180));
p2.setBounds(new Rectangle(160,0,150,180));
getContentPane().add(p1);
getContentPane().add(p2);
}
public MyFrame(String title) {
super(title);
initVarient();
initFace();
show();
}
}
public class ThreadApp {
public static void main(String[] args) {
MyFrame frame=new MyFrame("Test Threads");
final MyThread t1=new MyThread(frame.p1);
final MyThread t2=new MyThread(frame.p2);
frame.getContentPane().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
t1.stopThread();
t2.stopThread();
}
});
t1.start();
t2.start();
}
}
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
interface MyInitScheme {
void initVarient();
void initFace();
}
class MyMenu extends JMenu implements MyInitScheme {
public MyMenu(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyMenuItem extends JMenuItem implements MyInitScheme {
public MyMenuItem(String text) {
super(text);
initVarient();
initFace();
}
public void initFace() {
setBackground(SystemColor.desktop);
setFont(new Font("Serif", 0, 16));
setForeground(SystemColor.info);
setBorder(BorderFactory.createEtchedBorder());
}
public void initVarient(){};
}
class MyThread extends Thread {
private Component p;
private boolean stop=false;
public void run() {
while(!stop) {
try {
sleep(200);
} catch (InterruptedException e) {
System.out.println(e);
}
p.repaint();
}
}
public void stopThread() {
stop=true;
}
public void start() {
stop=false;
super.start();
}
public MyThread(Component cp) {
p=cp;
}
}
class MyFrame extends JFrame implements MyInitScheme {
class MyContent extends JPanel {
int x=10,y=10,r=30;
public void paintComponent(Graphics g) {
super.paintComponent(g);
java.util.Random rand=new java.util.Random();
r=10+rand.nextInt(90);
g.drawOval(x, y, r, r);
}
public MyContent() {
setBackground(SystemColor.activeCaption);
}
}
JPanel p1=new MyContent();
JPanel p2=new MyContent();
private JMenuBar mnuBar = new JMenuBar();
private JMenu mnuFile = new MyMenu("File");
private JMenuItem mnuFileExit = new MyMenuItem("Exit");
private JMenu mnuHelp = new MyMenu("Help");
private JMenuItem mnuHelpContact = new MyMenuItem("Contact Me");
public void initVarient() {};
public void initFace() {
this.setSize(new Dimension(320, 240));
this.setResizable(false);
getContentPane().setLayout(null);
getContentPane().setBackground(SystemColor.activeCaption);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mnuBar.setBackground(SystemColor.desktop);
mnuBar.setBorder(BorderFactory.createRaisedBevelBorder());
mnuFile.setMnemonic('F');
mnuFileExit.setMnemonic('x');
mnuFileExit.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} );
mnuHelp.setMnemonic('H');
mnuHelpContact.setMnemonic('M');
mnuHelpContact.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
try {
Process p=Runtime.getRuntime().exec("C:/Program Files/"
+"Internet Explorer/IExplore.EXE"
+" mailto:zhangjiawang0809@sohu.com");
} catch (Exception ex) {
System.out.println(ex);
}
}
} );
mnuFile.add(mnuFileExit);
mnuHelp.add(mnuHelpContact);
mnuBar.add(mnuFile);
mnuBar.add(mnuHelp);
this.setJMenuBar(mnuBar);
p1.setBounds(new Rectangle(0,0,150,180));
p2.setBounds(new Rectangle(160,0,150,180));
getContentPane().add(p1);
getContentPane().add(p2);
}
public MyFrame(String title) {
super(title);
initVarient();
initFace();
show();
}
}
public class ThreadApp {
public static void main(String[] args) {
MyFrame frame=new MyFrame("Test Threads");
final MyThread t1=new MyThread(frame.p1);
final MyThread t2=new MyThread(frame.p2);
frame.getContentPane().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
t1.stopThread();
t2.stopThread();
}
});
t1.start();
t2.start();
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。