【问题标题】:How to move a matrix如何移动矩阵
【发布时间】:2016-05-05 07:06:12
【问题描述】:

我是 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));
}

这部分打印矩阵,但位置不好,我想移动它。

有人知道吗?
我该如何移动它?

【问题讨论】:

  • Graphics#drawString(String, int, int) 其中int 的值为x/y ...尝试更改y 的值

标签: java matrix jframe keyevent


【解决方案1】:

您将不得不更改这行代码:

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

DrawString 将您要写入的内容作为第一个参数,然后是 x 和 y 坐标。因此,例如,如果您想将其向上移动,请在 y 坐标上减去一些内容。

【讨论】:

  • "如果你想将它向上移动,在 y 坐标上添加一些东西" - 2D 图形在左上/左坐标空间中工作(0x0 是左上角),所以如果你想向上移动一些东西,你需要从 y 坐标中减去
  • 你是对的。我已经确定了我的答案。感谢您的反馈!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多