public class DrawJFrame extends javax.swing.JFrame {
public DrawJFrame() {
initComponents();
this.setTitle("多线程窗体");
this.setLocationRelativeTo(null);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
LeftHand = new com.derun.LeftHand();
RightHand = new com.derun.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, 178, Short.MAX_VALUE)
);
LeftHandLayout.setVerticalGroup(
LeftHandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 276, Short.MAX_VALUE)
);
RightHand.setBorder(javax.swing.BorderFactory.createTitledBorder("右手"));
RightHand.setRequestFocusEnabled(false);
javax.swing.GroupLayout RightHandLayout = new javax.swing.GroupLayout(RightHand);
RightHand.setLayout(RightHandLayout);
RightHandLayout.setHorizontalGroup(
RightHandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 178, Short.MAX_VALUE)
);
RightHandLayout.setVerticalGroup(
RightHandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 276, 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)
.addGap(18, 18, Short.MAX_VALUE)
.addComponent(RightHand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(RightHand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(LeftHand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
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() {
@Override
public void run() {
new DrawJFrame().setVisible(true);
}
});
}
private final LeftHand leftHand = new LeftHand();
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel LeftHand;
private javax.swing.JPanel RightHand;
// End of variables declaration//GEN-END:variables
}
package com.derun;
import java.awt.Color;
import java.awt.Graphics;
public class LeftHand extends javax.swing.JPanel implements Runnable {
int up = 145, down = 45, left = 100, right1 = 150, right2 = 200;
public LeftHand() {
initComponents();
new Thread(this).start();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
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, 190, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
@Override
public void run() {
while (true) {
while (right1 > 100) {
this.repaint(); //重绘
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
right1 -= 5;
}
up = 145;
while (up > 45) {
this.repaint(); //重绘
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
up -= 5;
}
left = 100;
while (left < 200) {
this.repaint(); //重绘
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
left += 5;
}
down = 45;
while (down < 145) {
this.repaint(); //重绘
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
down += 5;
}
right2 = 200;
while (right2 > 150) {
this.repaint(); //重绘
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
right2 -= 5;
}
right1 = 150;
up = 145;
down = 45;
left = 100;
right2 = 200;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
for (int i = 150; i >= right1; i--) {
g.drawLine(145, 150, 145, i);
}
for (int i = 145; i >= up; i--) {
g.drawLine(145, 100, i, 100);
}
for (int i = 100; i <= left; i++) {
g.drawLine(45, 100, 45, i);
}
for (int i = 45; i <= down; i++) {
g.drawLine(45, 200, i, 200);
}
for (int i = 200; i >= right2; i--) {
g.drawLine(145, 200, 145, i);
}
}
}
package com.derun;
import java.awt.Color;
import java.awt.Graphics;
public class RightHand extends javax.swing.JPanel implements Runnable {
int angel = 0;
public RightHand() {
initComponents();
new Thread(this).start();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
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>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
@Override
public void run() {
while (true) {
if(angel == 360)
angel = 0;
this.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
angel += 5;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
for (int i = 0; i <= angel; i++) {
g.drawArc(45, 100, 100, 100, 0, i);
}
}
}