How to change the coordinates of the location in the Java ah, urgent urgent!

The default coordinate to Cartesian coordinates, Y axis

Started by Merle at November 09, 2016 - 11:10 AM

Graphics2D.scale(1, -1);

Posted by Samantha at November 17, 2016 - 11:52 AM

Hello, could you speak in detail, I was a beginner, tried or not displayed, trouble

Posted by Merle at November 21, 2016 - 12:12 PM


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();
    }
}

Posted by Samantha at December 01, 2016 - 1:07 PM

That if I is to define a JPanel in the JFrame, then JPanel is smaller than JFrame, is now in the JPanel picture I would like to ask how to coordinate getWidth Panel () and getHeight () is the window length to width, that how to get the length and width of Panel I this in JPanel is not.

Posted by Merle at December 15, 2016 - 1:10 PM