2016-05-05 3 views
2

私はJavaを初めて使いました。私は学校のプロジェクトに取り組んでいます。マトリックスの移動方法

このプロジェクトでは、KeyListenerを使用して行列を作成する必要があります。

if (!(e.getKeyChar()>='0' && e.getKeyChar()<='9' || e.getKeyChar()=='-' || e.getKeyCode()==10)) { 
     if(!(e.getKeyChar() == 'w' || e.getKeyChar() == 'b')){ 
      JOptionPane.showMessageDialog(lince, "Not Allowed", "Fatal Error", JOptionPane.ERROR_MESSAGE); 
     } 
    }else{ 
     if(e.getKeyCode()==10){ 
      try{  
       MatrixI[i][j]=Integer.parseInt(Posicion); 
       j++; 
       Posicion = ""; 
     if (j== 4){ 
       i++; 
       j=0; 
     }if (i==5){ 
      if (!(i >= 6)){  
       JOptionPane.showMessageDialog(lince, "You cannot add more numbers", "Not Allowed!!!", JOptionPane.ERROR_MESSAGE); 
       contar(); 
      } 
     }  
      }catch(ArrayIndexOutOfBoundsException ex){ 
       JOptionPane.showMessageDialog(lince, "You cannot add more numbers", "Not Allowed!!!", JOptionPane.ERROR_MESSAGE); 
       System.exit(0); 
      } 
      catch(NumberFormatException ex){ 
        JOptionPane.showMessageDialog(lince, "Just write a number", "Not Allowed!!!", JOptionPane.ERROR_MESSAGE); 
      } 
     }else{ 
      Posicion=Posicion+e.getKeyChar(); 
      this.escribe(lince.getGraphics()); 
     } 
    } 


public void escribe(Graphics g){ 
    g.setColor(Color.red); 
    g.setFont(new Font("Arial", Font.CENTER_BASELINE,12)); 
    g.drawString(Posicion, 30+((1+j)*50),((1+i)*35)); 
} 

この部分は行列を出力しますが、それは良い位置にないと私はそれを移動したい: 私はそうのようにそれを作りました。

enter image description here

誰もが任意のアイデアを持っていますか?
どうすれば移動できますか?あなたは、このコード行を変更する必要があります

+0

'グラフィックス#drawString(String、int、int)' int値はx/yです... y値を変更してみてください – MadProgrammer

答えて

0

g.drawString(Posicion, 30+((1+j)*50),((1+i)*35)); 

はDrawStringは、あなたが最初のパラメータxとy座標として書きたいものを取ります。例えば、あなたがそれを上に移動したいなら、y coordに何かを引いてください。

+1

* "上に移動したい場合は、 y coord "* - 2Dグラフィックスは上/左座標空間で動作します(0x0は左上です)。何かを上に移動したい場合は、y座標から引く必要があります – MadProgrammer

+0

あなたは正しいです。私は私の答えを修正しました。ご意見ありがとうございます! – Aurasphere