//主程序
package com.yuanfang;
public class DrawJFrame extends javax.swing.JFrame {
public DrawJFrame() {
initComponents();
this.setTitle("多线程窗体");
this.setLocationRelativeTo(null);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
leftHand = new com.yuanfang.LeftHand();
RightHand = new com.yuanfang.RightHand();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
leftHand.setBorder(javax.swing.BorderFactory.createTitledBorder("左手"));
javax.swing.GroupLayout leftHandLayout = new javax.swing.GroupLayout(leftHand);
leftHand.setLayout(leftHandLayout);
leftHandLayout.setHorizontalGroup(
leftHandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 188, Short.MAX_VALUE)
);
leftHandLayout.setVerticalGroup(
leftHandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 276, Short.MAX_VALUE)
);
RightHand.setBorder(javax.swing.BorderFactory.createTitledBorder("右手"));
javax.swing.GroupLayout RightHandLayout = new javax.swing.GroupLayout(RightHand);
RightHand.setLayout(RightHandLayout);
RightHandLayout.setHorizontalGroup(
RightHandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 203, Short.MAX_VALUE)
);
RightHandLayout.setVerticalGroup(
RightHandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(leftHand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(RightHand, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(leftHand, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(RightHand, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DrawJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DrawJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DrawJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DrawJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DrawJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel RightHand;
private javax.swing.JPanel leftHand;
// End of variables declaration
}
//左手
package com.yuanfang;
import java.awt.Color;
import java.awt.Graphics;
public class LeftHand extends javax.swing.JPanel implements Runnable {
public LeftHand() {
initComponents();
new Thread(this).start();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
int line0 = 0;
int line1 = 0;
int line2 = 0;
int line3 = 0;
boolean lenn0 = false;
boolean lenn1 = false;
boolean lenn2 = false;
boolean lenn3 = false;
@Override
public void run() {
while (true) {
if (line0 >= 100 && line1 >= 100 && line2 >= 100 && line3 >= 100) {
line0 = 0;
line1 = 0;
line2 = 0;
line3 = 0;
}
while (line0 < 100) {
this.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
line0 += 5;
}
while (line1 < 100) {
this.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
line1 += 5;
}
while (line2 < 100) {
this.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
line2 += 5;
}
while (line3 <= 100) {
this.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
line3 += 5;
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(50, 100, 50, 100 + line0);
g.drawLine(50, 200, 50 + line1, 200);
g.drawLine(150, 200, 150, 200 - line2);
g.drawLine(150, 100, 150 - line3, 100);
}
}
//右手
package com.yuanfang;
import java.awt.Color;
import java.awt.Graphics;
public class RightHand extends javax.swing.JPanel implements Runnable {
int angle = 0;
public RightHand() {
initComponents();
new Thread(this).start();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
@Override
public void run() {
while(true){
/* if(line == 5){
line = 0;
}*/
if(angle == 360){
angle = 0;
}
this.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
// line++;
angle += 5;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.yellow);
/*for(int i=0;i<=line;i++){
g.drawString(poen[i], 10, 30+30*i);
}*/
for(int i=0;i<angle;i++){
g.fillArc(50, 50, 150, 150, 0, i);
}
}
}