Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the coordinate plane in the JFrame

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

like image 453
Martin Marino Avatar asked May 30 '12 03:05

Martin Marino


People also ask

How to work with JFrame in Java?

JFrame class provides some methods which play an important role in working with JFrame. 1. AccessibleContext getAccessibleContext () – This method gets the accessible context that remains associated with the JFrame. 2. Container getContentPane () – This method creates the JFrame’s contentPane object. 3.

How to prevent a JFrame from being resized?

Use the following code if we want to prevent the frame from being resized: Undecorate the frame: If the frame is undecorated, its border, title bar and window buttons are all removed, only keep its content pane visible. 8. JFrame demo program

What is the difference between JFrame and jpanel?

The easiest way to explain JFrame and JPanel is to think of a painting hanging on the wall. The frame is what holds the painting. In that scenario the JFrame is the frame holding the painting and the JPanel is the painting. Where it says public class Frame after the word Frame type extends JFrame

How to customize JFrame’s appearance?

Customizing JFrame’s appearance. Set icon image for the frame:The icon image is in the file system:Image icon = new javax.swing.ImageIcon("images/android.png").getImage(); frame.setIconImage(icon);Here the image android.png is placed under directory images which is relative to the application’s directory.


1 Answers

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);
    }
}
like image 190
nIcE cOw Avatar answered Sep 28 '22 07:09

nIcE cOw