2014-02-14 21 views
0

我试图绘制简单的图表,并使用下面的代码来管理图形的坐标系:的Java的AffineTransform视图改变

Graphics2D g2d = (Graphics2D) g; 

// Save the current transform to put it back the way I found it 
saveAT = g2d.getTransform(); 

int height = getHeight(); 
// Get the get the magnitude of the sample 
long scale = thePlotArray.getRealScale(); 

// Set the x origin at 0 and the y origin in the center of the window 
double yTrans = ((double)height)/2.0; 
AffineTransform tform = AffineTransform.getTranslateInstance(0.0, yTrans); 

// set the y scale to show all of the sample 
double yScale = ((double)height)/((double)scale); 
tform.scale(1, yScale); 
g2d.setTransform(tform); 

// Plot the data with a series of 
g2d.drawLine(x1, y1, x2, y2); 

// reset the transform back 
g2d.setTransform(saveAT); 

看来,直到我调整窗口的大小,然后起源跳起来运行良好大约1/10的窗口。当我打印出身高,体重,yTrans等数字时,这些数字似乎都是一样的。

有没有我没有做的事情?

回答

0

我遇到过同样的问题(对于简单的条形图)。画方法是:

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    if(_data==null){ 
     return; 
    } 

    int bars = _data.length; 
    int width = this.getSize().width/bars; 
    int height = this.getSize().height; 

    Graphics2D g2d = (Graphics2D)g.create(); 
    AffineTransform flip = new AffineTransform(); 
    flip.translate(0, height); 
    flip.scale(1, -1); 
    g2d.setTransform(flip); 

    //Painting code via g2d.drawRect(), g2d.fillRect() 

}

我的显示面板中嵌入另一面板。无论何时调用display.componentResized()都会导致原点翻译大约24个点。我将它作为MVC演示的一部分,对于某些生成重绘的事件(无论是否调整显示中的值),原点似乎都已正确重置为(0,0)。

进一步的故障排除表明,只有当此显示面板存在于JTabbedPane中时才会出现该错误。作为一个例子,添加一个MouseListener来重画mouseMoved()动作的显示,解决了原点问题。

标签之间切换恢复问题。试图重新显示作为处理ChangeEvent的一部分的显示没有效果。

尚未解决。