将默认的坐标系改为直角坐标系,y轴向上
------解决方案--------------------
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class Aloha extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.translate(getWidth() / 2, getHeight() / 2);
g2d.scale(1, -1);
g2d.drawLine(-200, 0, 200, 0);
g2d.drawLine(0, -200, 0, 200);
g2d.fillRect(0, 0, 100, 100);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame();
// Add your component.
frame.setContentPane(new Aloha());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) throws IOException {
createAndShowGUI();
}
}