TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- import java.awt.BasicStroke;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.geom.GeneralPath;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.Timer;
-
-
- public class ShowMovedDottedLine extends JPanel implements ActionListener{
-
- @Override
- public void paint(Graphics g) {
- Color c = getBackground();
- Graphics2D g2 = (Graphics2D)g;
- g2.setColor(c);
- g2.fillRect(0, 0, this.getWidth(),this.getHeight());
- g2.setColor(Color.RED);
- g2.setStroke(bs[(index++)%6]);
-
- g2.draw(path);
- }
-
- private static final long serialVersionUID = 1L;
-
- Timer timer ;
- GeneralPath path;
- final BasicStroke[] bs = new BasicStroke[6];
- int index = 0;
-
- public ShowMovedDottedLine(){
- timer=new Timer(50,this);
- path = new GeneralPath();
- path.moveTo(50, 50);
- path.lineTo(200, 200);
- float[] dash = new float[]{5,5};
- for (int i = 0; i < 6; i++) {
- bs[i] = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, i);
- }
- timer.start();
- }
-
-
- public void actionPerformed(ActionEvent e) {
- repaint();
- }
-
-
- public static void main(String[] args) {
- JFrame frame=new JFrame();
- frame.setSize(800,800);
- ShowMovedDottedLine show = new ShowMovedDottedLine();
- frame.getContentPane().add(show,BorderLayout.CENTER);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
-
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/11/18/235741156.zip |
|