6

//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.

3
  • 4
    In Java, (0,0) is at the upper left hand corner.
    – Jeffrey
    Commented May 30, 2012 at 3:13
  • 3
    This link might help.
    – CRM
    Commented May 30, 2012 at 3:20
  • 3
    This example discusses transforming between Cartesian and screen coordinates.
    – trashgod
    Commented May 30, 2012 at 3:24

1 Answer 1

9

Here the Co-ordinates start from the TOP LEFT SIDE of 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.

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);
    }
}
5
  • The code is saying it cannot find symbol class DrawingBase. The errors are private "DrawingBase" canvas; canvas = new "DrawingBase"(); The quotes are to show where the actual error is coming from. I appreciate the help, but I have no idea how to fix this. Could I get a little more help? Commented May 30, 2012 at 17:42
  • I added comments to your code next to where the errors were. Again thanks for all the help. Commented May 30, 2012 at 17:52
  • It appears to me as if you don't know how to run this sort of a program :-), Simply copy this to your notepad, and save it as DrawingExample.java , now run it from your command prompt by writing driveLetter: locationOfFile/javac DrawingExample.java and press ENTER. Now type java DrawingExample and press ENTER. That's it. If still you had issues let me know, or once try to visit this link of mine, run program from notepad
    – nIcE cOw
    Commented May 30, 2012 at 18:58
  • I checked out your link but it looks like that is instructions for Windows. I have OSX. I tried running DrawingExample from Text Wrangler. Just to be clear we are running this from the Terminal window right? I have some experience with Python and know how to run files from there but not so much with Java. I tried what you told me to do, but it's still not working. driveLetter: locationOfFile/javac DrawingExample.java, but with my file location instead of locationOfFile. Im getting -bash: driveLetter:: command not found. Would this be because you gave me instructions for a Windows user? Commented May 31, 2012 at 17:25
  • Yeah, might be the case, never used MacOS, though do one more thing, use some IDE, make a package put both these .java classes inside the same package and then try to run it. Hope this might can solve the problem for you.
    – nIcE cOw
    Commented May 31, 2012 at 17:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.