質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

1225閲覧

Java解決。。。。。。。。

Kokkoo

総合スコア7

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/01/24 12:48

編集2018/01/25 11:40

…解決……………………………………。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

LouiS0616

2018/01/25 08:40

編集履歴を元に、質問内容を元に戻してください。
guest

回答1

0

ベストアンサー

このコードはあなたが作ったものではないようですね。
元コードを理解していれば、このような全く違う描画ロジックを採用するはずがないからです。
とりあえず、やっつけで座標調整等はしてないですが、次ブロック表示機能を入れてみましたので、載せておきます。
作ったパネルはmain()のところでadd()しています。これを踏まえて、スタートボタンや点数表示などを追加してみてはいかがですか?

java

1 2 3import java.awt.Color; 4import java.awt.Graphics; 5import java.awt.Point; 6import java.awt.event.KeyEvent; 7import java.awt.event.KeyListener; 8import java.util.ArrayList; 9import java.util.Collections; 10import javax.swing.JFrame; 11import javax.swing.JPanel; 12 13public class Tetris extends JPanel { 14 15 private static final long serialVersionUID = -8715353373678321308L; 16 private final Point[][][] Tetraminos = { 17 // I-Piece 18 { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) }, 19 { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }, 20 { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) }, 21 { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) } }, 22 // J-Piece 23 { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) }, 24 { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) }, 25 { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) }, 26 { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) } }, 27 // L-Piece 28 { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) }, 29 { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) }, 30 { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) }, 31 { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) } }, 32 // O-Piece 33 { { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, 34 { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, 35 { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, 36 { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) } }, 37 // S-Piece 38 { { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) }, 39 { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }, 40 { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) }, 41 { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) } }, 42 // T-Piece 43 { { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) }, 44 { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }, 45 { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) }, 46 { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) } }, 47 // Z-Piece 48 { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, 49 { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }, 50 { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, 51 { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) } } }; 52 private final Color[] tetraminoColors = { Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, 53 Color.pink, Color.red }; 54 private Point pieceOrigin; 55 private int preparedPiece = -1; 56 private int currentPiece = -1; 57 private int rotation; 58 private ArrayList<Integer> nextPieces = new ArrayList<Integer>(); 59 private long score; 60 private Color[][] well; 61 62 private static NextBlockPanel nextBlockpanel; 63 64 // Creates a border around the well and initializes the dropping piece 65 private void init() { 66 well = new Color[12][24]; 67 for (int i = 0; i < 12; i++) { 68 for (int j = 0; j < 23; j++) { 69 if (i == 0 || i == 11 || j == 22) { 70 well[i][j] = Color.GRAY; 71 } else { 72 well[i][j] = Color.BLACK; 73 } 74 } 75 } 76 newPiece(); 77 } 78 79 // Put a new, random piece into the dropping position 80 public void newPiece() { 81 pieceOrigin = new Point(5, 2); 82 rotation = 0; 83 if (nextPieces.isEmpty()) { 84 Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6); 85 Collections.shuffle(nextPieces); 86 } 87 if (preparedPiece == -1) { 88 currentPiece = createIndex(); 89 preparedPiece = createIndex(); 90 } else { 91 currentPiece = preparedPiece; 92 preparedPiece = createIndex(); 93 } 94 nextBlockpanel.setPiece( 95 preparedPiece, 96 Tetraminos[preparedPiece][0], 97 tetraminoColors[preparedPiece]); 98 } 99 100 public int createIndex() { 101 rotation = 0; 102 if (nextPieces.isEmpty()) { 103 Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6); 104 Collections.shuffle(nextPieces); 105 } 106 int index = nextPieces.get(0); 107 nextPieces.remove(0); 108 return index; 109 } 110 111 // Collision test for the dropping piece 112 private boolean collidesAt(int x, int y, int rotation) { 113 for (Point p : Tetraminos[currentPiece][rotation]) { 114 if (well[p.x + x][p.y + y] != Color.BLACK) { 115 return true; 116 } 117 } 118 return false; 119 } 120 121 // Rotate the piece clockwise or counterclockwise 122 public void rotate(int i) { 123 int newRotation = (rotation + i) % 4; 124 if (newRotation < 0) { 125 newRotation = 3; 126 } 127 if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) { 128 rotation = newRotation; 129 } 130 repaint(); 131 } 132 133 // Move the piece left or right 134 public void move(int i) { 135 if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) { 136 pieceOrigin.x += i; 137 } 138 repaint(); 139 } 140 141 // Drops the piece one line or fixes it to the well if it can't drop 142 public void dropDown() { 143 if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) { 144 pieceOrigin.y += 1; 145 } else { 146 fixToWell(); 147 } 148 repaint(); 149 } 150 151 // Make the dropping piece part of the well, so it is available for 152 // collision detection. 153 public void fixToWell() { 154 for (Point p : Tetraminos[currentPiece][rotation]) { 155 well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece]; 156 } 157 clearRows(); 158 newPiece(); 159 } 160 161 public void deleteRow(int row) { 162 for (int j = row - 1; j > 0; j--) { 163 for (int i = 1; i < 11; i++) { 164 well[i][j + 1] = well[i][j]; 165 } 166 } 167 } 168 169 // Clear completed rows from the field and award score according to 170 // the number of simultaneously cleared rows. 171 public void clearRows() { 172 boolean gap; 173 int numClears = 0; 174 for (int j = 21; j > 0; j--) { 175 gap = false; 176 for (int i = 1; i < 11; i++) { 177 if (well[i][j] == Color.BLACK) { 178 gap = true; 179 break; 180 } 181 } 182 if (!gap) { 183 deleteRow(j); 184 j += 1; 185 numClears += 1; 186 } 187 } 188 switch (numClears) { 189 case 1: 190 score += 100; 191 break; 192 case 2: 193 score += 300; 194 break; 195 case 3: 196 score += 500; 197 break; 198 case 4: 199 score += 800; 200 break; 201 } 202 } 203 204 // Draw the falling piece 205 private void drawPiece(Graphics g) { 206 g.setColor(tetraminoColors[currentPiece]); 207 for (Point p : Tetraminos[currentPiece][rotation]) { 208 g.fillRect((p.x + pieceOrigin.x) * 26, (p.y + pieceOrigin.y) * 26, 25, 25); 209 } 210 } 211 212 @Override 213 public void paintComponent(Graphics g) { 214 // Paint the well 215 g.fillRect(0, 0, 26 * 12, 26 * 23); 216 for (int i = 0; i < 12; i++) { 217 for (int j = 0; j < 23; j++) { 218 g.setColor(well[i][j]); 219 g.fillRect(26 * i, 26 * j, 25, 25); 220 } 221 } 222 223 // Display the score 224 g.setColor(Color.WHITE); 225 g.drawString("" + score, 19 * 12, 25); 226 // Draw the currently falling piece 227 drawPiece(g); 228 } 229 230 public static void main(String[] args) { 231 JFrame f = new JFrame("Tetris"); 232 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 233 f.setSize(600, 650); 234 f.setLayout(null); 235 f.getContentPane().setBackground(Color.darkGray); 236 f.setVisible(true); 237 final Tetris game = new Tetris(); 238 game.setBounds(30, 20, 12 * 26 + 10, 26 * 23 + 25); 239 nextBlockpanel = new NextBlockPanel(); 240 nextBlockpanel.setBackground(Color.WHITE); 241 nextBlockpanel.setBounds(370, 20,26*6, 26*6); 242 game.init(); 243 f.add(game); 244 f.add(nextBlockpanel); 245 246 // Keyboard controls 247 f.addKeyListener(new KeyListener() { 248 public void keyTyped(KeyEvent e) { 249 } 250 251 public void keyPressed(KeyEvent e) { 252 switch (e.getKeyCode()) { 253 case KeyEvent.VK_UP: 254 game.rotate(-1); 255 break; 256 case KeyEvent.VK_DOWN: 257 game.rotate(+1); 258 break; 259 case KeyEvent.VK_LEFT: 260 game.move(-1); 261 break; 262 case KeyEvent.VK_RIGHT: 263 game.move(+1); 264 break; 265 case KeyEvent.VK_SPACE: 266 game.dropDown(); 267 game.score += 1; 268 break; 269 } 270 } 271 272 public void keyReleased(KeyEvent e) { 273 } 274 }); 275 276 // Make the falling piece drop every second 277 new Thread() { 278 @Override 279 public void run() { 280 while (true) { 281 try { 282 Thread.sleep(1000); 283 game.dropDown(); 284 } catch (InterruptedException e) { 285 } 286 } 287 } 288 }.start(); 289 } 290}

java

1import java.awt.Color; 2import java.awt.Dimension; 3import java.awt.Graphics; 4import java.awt.Image; 5import java.awt.Point; 6 7import javax.swing.JPanel; 8 9/* 10 * Created on 2006/12/09 11 */ 12public class NextBlockPanel extends JPanel { 13 14 public static final int WIDTH = 26 * 6; 15 public static final int HEIGHT = 26 * 6; 16 17 private int bid; 18 private Color color; 19 private Point[] points; 20 21 public NextBlockPanel() { 22 setPreferredSize(new Dimension(WIDTH, HEIGHT)); 23 } 24 25 public void setPiece(int bid, Point[] points, Color color) { 26 27 this.bid = bid; 28 this.points = points; 29 this.color = color; 30 31 repaint(); 32 } 33 34 public void paintComponent(Graphics g) { 35 36 g.setColor(Color.BLACK); 37 g.fillRect(0, 0, WIDTH, HEIGHT); 38 39 drawPiece(g); 40 } 41 42 private void drawPiece(Graphics g) { 43 44 if (points == null) return; 45 46 int bx = 39, by = 52; 47 if (bid == 0) { bx = 26; by = 39; } 48 else if (bid == 2) by = 26; 49 else if (bid == 3) bx = 52; 50 g.setColor(color); 51 for (Point p : points) { 52 g.fillRect(p.x * 26 + bx, p.y * 26 + by, 25, 25); 53 } 54 } 55}

追記:ブロック位置の調整も入れてみました。

投稿2018/01/24 16:23

編集2018/01/24 22:24
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kokkoo

2018/01/25 08:22

初歩的な理解が出来ていなかったです。参考になりました。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問