Center Point GUI

Arman Majumder 0 Tallied Votes 722 Views Share

A very simple program, which displays the coordinate points for the center point of the panel. Automatically updates the (X,Y) form coordinates with size modification.

// Arman Majumder
// Center Point (X,Y) GUI

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
       
    public class XY extends JPanel
   {
       public XY (Color backColor)
      {
         setBackground(backColor);
      }
      
       public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         int x = getWidth() / 2 - 60;
         int y = getHeight() / 2;
         g.setColor(Color.red);
         Font font = new Font("Arial", Font.PLAIN, 14);
         g.setFont(font);
         g.drawString("( " + x + ", " + y + " )", x + 10, y + 15);	
      }
   	
       public static void main(String [] args)
      {
         JFrame theGUI = new JFrame();
         theGUI.setTitle("(X,Y)");
         theGUI.setSize(400, 400);
         theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         XY panel = new XY(Color.black);
         Container pane = theGUI.getContentPane();
         pane.add(panel);
         theGUI.setVisible(true);
      }
   }