私はJava GUIプロジェクトに取り組んでいます。私は2Dで台形の3Dボードで文字(JLabel with ImageIcon)を移動する必要があります。 nullレイアウトを使用しています。 これを行うには、上下に移動するときにx座標を変更する必要がありますが、それらを正しく変更するための方程式を取得することはできません。3Dボードで動くJava Swing
ここに私の文字を移動するためのコードです。
private int x=0 , y=0;
private int velx = 0 , vely = 0;
private JButton[][] cells;
private int rows = 9, cols = 9;
public void moveUp()
{
int[] arr = { 8 , 6,4 , 2 , 0 ,0, -2 , -4 ,-6 , -8};
vely -= h/50 ;
velx += arr[cols];
cells[rows][cols].setIcon(null);
cells[--rows][cols].setIcon(position);
width -= width/20;
height-= height/20;
character.setIcon(new ImageIcon(((new ImageIcon(
icon).getImage()
.getScaledInstance(width, height,
java.awt.Image.SCALE_SMOOTH)))));
character.setBounds(x+velx, y+vely, width, height);
}
public void moveDown()
{
int[] arr = { 8 , 6,4 , 2 , 0,0 , -2 , -4 ,-6 , -8};
vely += h/50;
velx -= arr[cols];
cells[rows][cols].setIcon(null);
cells[++rows][cols].setIcon(position);
width += width/20;
height+= height/20;
character.setIcon(new ImageIcon(((new ImageIcon(
icon).getImage()
.getScaledInstance(width, height,
java.awt.Image.SCALE_SMOOTH)))));
character.setBounds(x+velx, y+vely, width, height);
}
public void moveLeft()
{
int[] arr = {45 , 46 , 47 , 48 , 50 , 56 , 57 ,58 , 59,60};
velx -= arr[rows];//w/23;
cells[rows][cols].setIcon(null);
cells[rows][--cols].setIcon(position);
character.setBounds(x+velx, y+vely, width, height);
}
public void moveRight()
{
int[] arr = {45 , 46 , 47 , 48 , 50 , 56 , 57 ,58 , 59,60};
velx +=arr[rows]; // w/23;
cells[rows][cols].setIcon(null);
cells[rows][++cols].setIcon(position);
character.setBounds(x+velx, y+vely, width, height);
}
を設定してみてください? – reden
私は上下に動かすだけで、左右に動かすと完全に動きます。しかし、私が4方向の複数の組み合わせを試すと、キャラクターは最終的にボードの外に出る –