我試圖創建標記爲X-axis.Two問題數行:號線使用Java圖形API
- 一切正常0-9。但在此之後的任何事情,這些數字都會被壓縮在一起,並且不能在規模上正確定位。
- 我的主軸線趨於消失每次我嘗試我的最大化窗口或有時它只是不會在all.Every時間任何這些發生出現,我不得不重新編譯我的代碼,它工作得很好。
與上述問題的任何幫助,將不勝感激。
import java.awt.Graphics;
import javax.swing.JFrame;
/**
* @author Emil Shirima
*
*/
public class Drawing extends JFrame {
/**
* @param args
*/
int width = 300, height = 300, spacing = 10;
int x1 = 0, y1 = 150, x2 = 300, y2 = 150;
public Drawing() {
setTitle("Trial");
setSize(width, height);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics brush) {
brush.drawLine(x1, y1, x2, y2);
x1 = 10;
y1 = 150;
x2 = 10;
y2 = 130;
// brush.drawLine(x1, y1, x2, y2);
for (int i = 0; i < 12; ++i) {
String ID = Integer.toString(i);
x1 = x2 += spacing;
brush.drawLine(x1, y1, x2, y2);
if (i < 10) {
brush.drawString(ID, x1 - 3, y2 + 40);
} else {
// With the below implementation, the numbers overlap each other
// and are not properly oriented on the axis
brush.drawString(ID, x1 - 3, y2 + 40);
// TODO: I need to resize the numbers after 10 so as they fit
// properly on the scale
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Drawing draw_object = new Drawing();
}
當前實現:
最大化GUI:
請參閱編輯回答。 –