java 在 JFrame 中使用坐标平面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10809514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:40:54  来源:igfitidea点击:

Using the coordinate plane in the JFrame

javaswingjframeawtcoordinates

提问by Martin Marino

//I am trying to learn how to draw objects in java. I'm getting better at it, but once I get an image on the screen I am having trouble manipulating it. The numbers I put in don't make sense to how the shapes are turning out. At least to me they don't. In algebra if you increase a number on the x axis it goes to the right and if you increase a number on the y axis it goes up. Thats not whats happening here. Can anyone explain to me how this works? I'm still new to java, so the more explanation and detail the better. I'm trying to take a couple of hours out a day over my summer to learn java and sometimes it gets a little frustrating. Any help is greatly appreciated.

//我正在尝试学习如何在java中绘制对象。我越来越擅长它,但是一旦我在屏幕上看到图像,我就无法操纵它。我输入的数字对形状的结果没有意义。至少对我来说他们没有。在代数中,如果你在 x 轴上增加一个数字,它会向右移动,如果你在 y 轴上增加一个数字,它会上升。这不是这里发生的事情。谁能向我解释这是如何工作的?我对java还是个新手,所以解释和细节越多越好。我想在暑假期间每天抽出几个小时来学习 Java,但有时会有点令人沮丧。任何帮助是极大的赞赏。

回答by nIcE cOw

Here the Co-ordinatesstart from the TOP LEFT SIDEof the screen, as as you increase value of X, you will move towards RIGHT SIDE, though as you increase the value of Y, you will move DOWNWARDS. Here is a small example Program for you to understand this a bit better, simply click on it anywhere.

这里Co-ordinatesTOP LEFT SIDE屏幕的开始,随着 值的增加X,您将向 移动RIGHT SIDE,但随着 值的增加Y,您将移动DOWNWARDS。这是一个小示例程序,您可以更好地理解这一点,只需在任何地方单击它即可。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DrawingExample
{
    private int x;
    private int y;
    private String text;
    private DrawingBase canvas;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Drawing Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        canvas = new DrawingBase();
        canvas.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                text = "X : " + me.getX() + " Y : " + me.getY();
                x = me.getX();
                y = me.getY();
                canvas.setValues(text, x, y);
            }
        }); 

        frame.setContentPane(canvas);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DrawingExample().displayGUI();
            }
        });
    }
}

class DrawingBase extends JPanel
{
    private String clickedAt = "";
    private int x = 0;
    private int y = 0;

    public void setValues(String text, int x, int y)
    {
        clickedAt = text;
        this.x = x;
        this.y = y;
        repaint();
    }

    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 400));
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawString(clickedAt, x, y);
    }
}