基本的に私はゲームのプロトタイプの文字スプライトにchar配列を使用しようとしていますが、 (配列の行単位で塗りつぶし矩形を使用してスプライトを描画する方法を見つけようとする)文字を出力するための正しい '行'に挿入します。再び、新しい行のスプライトの矩形を塗りつぶすために「インデントする」ためにif (i % 5 == 0) y_temp += 5;
などの多くの方法を試しましたが、どれも機能していません。
提案/助けてください。forループを使用してchar配列からスプライトを描画する方法
コード:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame {
private int x_pos, y_pos;
private JFrame frame;
private draw dr;
private char[] WARRIOR;
private Container con;
public test() {
x_pos = y_pos = 200;
frame = new JFrame("StixRPG");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 500);
frame.setResizable(false);
frame.setVisible(true);
con = frame.getContentPane();
con.setBackground(Color.black);
dr = new draw();
dr.setBackground(Color.black);
con.add(dr);
WARRIOR = (
" " +
"!!!!!" +
"!!ooo" +
"!!!!!" +
"#####" +
"#####" +
"#####" +
"** **").toCharArray();
}
public static void main(String[] args) {
test tst = new test();
}
class draw extends JPanel {
public draw() {
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
int y_temp = y_pos;
for (int i = 0; i < WARRIOR.length; i++) {
if (WARRIOR[i] == '!') {
g.setColor(new Color(0, 0, 204));
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
else if (WARRIOR[i] == 'o') {
g.setColor(new Color(204, 0, 0));
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
else if (WARRIOR[i] == '#') {
g.setColor(new Color(0, 0, 102));
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
else if (WARRIOR[i] == '*') {
g.setColor(Color.black);
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
}
}
}
}
いくつかのベストプラクティスの提案:クラス名は大文字で始まり、大文字は定数用に予約する必要があります(static final)。 –