java
1import javax.swing.*;//swingのimport 2import java.awt.*;//awtのimport 3import java.awt.event.*;//awtのeventのimport 4 5public class action extends JFrame implements ActionListener{ 6 7 static action Frame;//Frame 8 9 JPanel Panel;//Panel 10 11 JLabel Player;//プレイヤーの画像が入ったJLabel 12 JLabel Block;//ブロックの画像が入ったJLabel 13 14 ImageIcon PlayerIcon;//プレイヤーの画像読み込み 15 ImageIcon BlockIcon;//ブロックの画像読み込み 16 17 Timer timer; 18 19 int PlayerX = 20;//プレイヤーのX座標 20 int PlayerY = 40;//プレイヤーのY座標 21 22 int BlockX = 0;//ブロックのX座標 23 int BlockY = 442;//ブロックのY座標 24 25 static boolean Jflag = false;//ジャンプ判定フラグ 26 static boolean UKflag = false;//ジャンプキー判定フラグ 27 static boolean LKflag = false;//左キー判定フラグ 28 static boolean RKflag = false;//右キー判定フラグ 29 static boolean Dflag = false;//デバック許可判定フラグ 30 31 static boolean f = true;//TESTフラグ 32 int start = 0; 33 34 public static void main(String args[]){ 35 36 Frame = new action(); 37 38 Frame.setSize(900, 500);//ウィンドウのサイズ設定(X, Y) 39 Frame.setTitle("アクションゲーム");//ウィンドウのタイトル 40 Frame.setLocationRelativeTo(null);//ウィンドウを画面の中央に表示する 41 Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//xで閉じるようにする 42 Frame.setVisible(true);//ウィンドウを可視化 43 44 } 45 46 47 48 action(){ 49 50 timer = new Timer(10, this);//タイマー間隔設定 51 52 Panel = new JPanel(); 53 Panel.setLayout(null);//レイアウトマネージャー無効化 54 55 PlayerIcon = new ImageIcon("./PlayerIcon.png");//プレイヤーの画像指定 56 BlockIcon = new ImageIcon("./BlockIcon.png");//ブロックの画像指定 57 58 Player = new JLabel(PlayerIcon);//プレイヤー画像設定 59 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標とサイズ設定 60 61 Block = new JLabel(BlockIcon);//ブロック画像設定 62 Block.setBounds(BlockX, BlockY, 20, 20);//ブロックの座標とサイズ設定 63 64 Panel.add(Player);//プレイヤー画像追加 65 Panel.add(Block);//ブロック画像追加 66 67 Container contentPane = getContentPane();//??? 68 contentPane.add(Panel, BorderLayout.CENTER);//??? 69 70 timer.start();//タイマースタート 71 72 while(f == true){ 73 74 start += 1; 75 76 System.out.println(start); 77 78 if(start >= 1000){ 79 80 break; 81 82 } 83 84 } 85 86 } 87 88 89 90 public void actionPerformed(ActionEvent e){//タイマーの処理 91 92 if(PlayerY < 399 && UKflag == false){//空中判定処理 93 94 Jflag = true;//ジャンプフラグをtrueにする 95 96 }else if(PlayerY >= 399){ 97 98 Jflag = false;//ジャンプフラグをfalseにする 99 100 } 101 102 if(Jflag == true){//重力処理 103 104 PlayerY += 6;//プレイヤーのY座標を+3 105 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定 106 Panel.repaint();//パネルを更新 107 108 } 109 110 if(UKflag == true){//ジャンプ処理 111 112 PlayerY -= 40;//プレイヤーのY座標を-40 113 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定 114 Panel.repaint();//パネルを更新 115 116 if(PlayerY <= 349){//もしPlayerYが359以下なら 117 118 Jflag = true;//ジャンプフラグをtrueにする 119 UKflag = false;//ジャンプキー判定フラグをfalseにする 120 121 } 122 123 } 124 125 if(LKflag == true){//左移動処理 126 127 PlayerX -= 4;//プレイヤーのX座標を-4 128 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定 129 Panel.repaint();//パネルを更新 130 131 if(PlayerX < 0){//もしPlayerXが0未満なら 132 133 PlayerX = 0;//プレイヤーのX座標を0に設定 134 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定 135 Panel.repaint();//パネルを更新 136 137 } 138 139 LKflag = false;//左キー判定フラグをfalseにする 140 141 } 142 143 if(RKflag == true){//右移動処理 144 145 PlayerX += 4;//プレイヤーのX座標を+4 146 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定 147 Panel.repaint();//パネルを更新 148 149 if(PlayerX >= 864){//もしPlayerXが864以上なら 150 151 PlayerX = 864;//プレイヤーのX座標を864に設定 152 Player.setBounds(PlayerX, PlayerY, 20, 40);//プレイヤーの座標再設定 153 Panel.repaint();//パネルを更新 154 155 } 156 157 RKflag = false;//右キー判定フラグをfalseにする 158 159 } 160 161 } 162 163 164 165 protected void processKeyEvent(KeyEvent e){//キー入力の処理 166 167 if(e.getID() == KeyEvent.KEY_PRESSED){ 168 169 if(e.getKeyCode() == KeyEvent.VK_UP && Jflag == false){//ジャンプキー判定 170 171 UKflag = true;//ジャンプキー判定フラグをfalseにする 172 173 } 174 175 if(e.getKeyCode() == KeyEvent.VK_LEFT){//左キー判定 176 177 LKflag = true;//左キー判定フラグをfalseにする 178 179 } 180 181 if(e.getKeyCode() == KeyEvent.VK_RIGHT){//右キー判定 182 183 RKflag = true;//右キー判定フラグをfalseにする 184 185 } 186 187 if(e.getKeyCode() == KeyEvent.VK_D && Dflag == true){//デバックキー判定 188 189 System.out.println("\nJflag:" + Jflag); 190 System.out.println("UKflag:" + UKflag); 191 System.out.println("LKflag:" + LKflag); 192 System.out.println("RKflag:" + RKflag); 193 System.out.println("Dflag:" + Dflag); 194 System.out.println("PlayerX座標:" + PlayerX); 195 System.out.println("PlayerY座標:" + PlayerY + "\n"); 196 197 } 198 199 if(e.getKeyCode() == KeyEvent.VK_S && Dflag == false){//デバック許可キー判定 200 201 Dflag = true;//デバック許可キー判定フラグをtrueにする 202 System.out.println("デバック機能を有効にしました");//デバック許可メッセージ出力 203 204 } 205 206 if(e.getKeyCode() == KeyEvent.VK_E && Dflag == true){//終了キー判定 207 208 Frame.dispose();//Frame削除 209 System.out.println("正常に終了しました");//終了メッセージ表示 210 211 } 212 213 } 214 215 } 216 217} 218
このように設定した画像をたくさん表示したいのですが
座標を変えて再表示したりしても
なかなかうまくいきません
どのようにしたらたくさん表示できるのでしょうか?
回答1件
あなたの回答
tips
プレビュー