画像を表示させたJLabelを、swingのTimerを使って少しずつsetBounds()の値を増やしたり減らしたりして画像を移動させるアニメーションを作りましたが、作ったのとは別のパソコンで動かしてみたらすごく重く、CPU稼働率100%までいってました。
重かったのはwin7 32bit クロック1.8GHz 2GBメモリ のもので、このくらいのスペックのパソコンではちゃんと動いて欲しいです。
CPUか稼働率は、アプリケーションを起動する前が約50%、起動して音楽が再生され始めて約75%、Timerで画像(JLabel)を動かすと100%になりますが、メモリはずっと45%程です。
Timerの時間の間隔を広げたりせずに、もっと軽くなる方法はありませんか?
画像がカクカクと動くように見えるのは最低限にしたいので。
それとも別の理由でしょうか??
一応、ソース貼ります。
java
1 //float charSize[]; 2 //int windowWidth = 850; 3 //int windowHeight = 580; 4 5 private class Funcs{ //インナークラス 6 7 private Timer st; 8 private int sw; 9 private int ct; 10 private int x; 11 12 private Timer jt; 13 private int jw; 14 private int jct; 15 private int jSpan; 16 17 private Timer mt; 18 private int start; 19 private int goal; 20 21 private JLabel label; 22 private int fc; 23 24 public Funcs(int type, JLabel label, int fc){ 25 26 this(type, label, fc, 0); 27 } 28 public Funcs(int type, JLabel label, int fc, int goal){ 29 30 this.label = label; 31 this.fc = fc; 32 33 sw = 0; 34 ct = 0; 35 x = label.getX(); 36 37 jw = 0; 38 jct = 0; 39 jSpan = 8; 40 41 start = 0; 42 this.goal = goal; 43 44 if(type == 0){ 45 46 st = new Timer(12, new SwingTimer()); 47 st.start(); 48 } else if(type == 1){ 49 50 jt = new Timer(13, new JumpTimer()); 51 jt.start(); 52 } else if(type == 2){ 53 54 start = label.getX(); 55 System.out.println("\t\tstart : " + start + "\n\t\tgoal : " + goal); 56 mt = new Timer(12, new MoveTimer()); 57 mt.start(); 58 } 59 } 60 61 private class SwingTimer implements ActionListener{ //インナークラスのインナークラス 62 63 public void actionPerformed(ActionEvent e){ 64 65 if(ct == 0 || ct == 2 || ct == 4) sw += 3; 66 else sw -= 3; 67 label.setBounds( 68 sw + x, 69 label.getY(), 70 label.getIcon().getIconWidth(), 71 label.getIcon().getIconHeight() 72 ); 73 if(sw == 12 || sw == -12) ct ++; 74 if(ct == 5 && sw == 0){ 75 76 st.stop(); 77 System.out.println("---- SwingTimer was stoped.(" + label.getText() + ") ----"); 78 checkReservation(); 79 } 80 } 81 } 82 83 private class JumpTimer implements ActionListener{ 84 85 public void actionPerformed(ActionEvent e){ 86 87 if(jct == 0 || jct == 2) { //up 88 89 jw += jSpan; 90 jSpan --; 91 } else{ //down 92 93 jw -= jSpan; 94 jSpan ++; 95 } 96 label.setBounds( 97 label.getX(), 98 windowHeight - (int)(label.getIcon().getIconHeight() / charSize[fc]) - jw, 99 label.getIcon().getIconWidth(), 100 label.getIcon().getIconHeight() 101 ); 102 103 if(jct == 3 && jSpan == 9){ 104 105 jt.stop(); 106 System.out.println("---- JumpTimer was stoped.(" + label.getText() + ") ----"); 107 checkReservation(); 108 } 109 if(jSpan == 0){ 110 111 jct ++; 112 } else if(jSpan == 9){ 113 114 jct ++; 115 jSpan --; 116 } 117 } 118 } 119 120 private class MoveTimer implements ActionListener{ 121 122 public void actionPerformed(ActionEvent e){ 123 124 if(start > goal) start -= 5; 125 else if(start < goal) start += 5; 126 label.setBounds( 127 start, 128 label.getY(), 129 label.getIcon().getIconWidth(), 130 label.getIcon().getIconHeight() 131 ); 132 if(start == goal){ 133 134 mt.stop(); 135 System.out.println("---- charMoveTimer was stoped.(" + label.getText() + ") ----"); 136 checkReservation(); 137 } 138 } 139 } 140 }
あなたの回答
tips
プレビュー