CompSci GUI paint() aid

Go down

CompSci GUI paint() aid

Post by Michael on Thu Sep 25, 2008 10:47 am

GraphicsAid allows you to determine the pixel position of your mouse when you click. A popup frame will come up showing the x and y values of your mouse click.

Code:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class GraphicsAid
{
   public static void main(String[] args)
   {

      new GraphicsAid();

   }
   private JComponent painter;
   private int x=-1;
   private int y=-1;


   public GraphicsAid()
   {

      final JFrame c=new JFrame("Click me")
      {
         public void paint(Graphics window)
         {
            window.setColor(Color.white);
            window.fillRect(0, 0, getWidth(), getHeight());

            window.setColor(Color.red);
            window.fillOval(x-1, y-1, 3, 3);
         }
      };


      //outputs to a popup frame.

      painter=new JComponent()
      {
         public void paint(Graphics window)
         {
            window.setColor(Color.black);
            window.setFont(new Font("Arial", 0, 12));

            window.drawString("X: "+x, 10, 20);
            window.drawString("Y: "+y, 10, 40);
         }
      };
      painter.setPreferredSize(new Dimension(100, 50));

      final JFrame frame=new JFrame();

      frame.add(painter);
      frame.pack();
      frame.setResizable(false);


      c.addMouseListener(new MouseListener()
      {
         public void mouseClicked(MouseEvent e)
         {
            x=e.getX();
            y=e.getY();
            painter.repaint();
            c.repaint();
         }
         public void mouseEntered(MouseEvent e){}
         public void mouseExited(MouseEvent e){}
         public void mousePressed(MouseEvent e){}
         public void mouseReleased(MouseEvent e){}


      });

      c.addFocusListener(new FocusListener()
      {
         public void focusGained(FocusEvent e)
         {
            c.repaint();
         }
         public void focusLost(FocusEvent e)
         {
         }
      });

      frame.setVisible(true);

      c.setSize(400, 400);
      c.setVisible(true);
   }
}


Last edited by Michael on Wed Oct 29, 2008 10:36 am; edited 1 time in total
avatar
Michael
Graduated member
Graduated member

Male Number of posts : 151
Age : 27
Registration date : 2008-04-30

View user profile http://elkinschess.forummotion.com

Back to top Go down

Re: CompSci GUI paint() aid

Post by Thunderflash on Fri Sep 26, 2008 11:29 am

Thank you!
It is EXTREMELY frusterating in the graphics stuff to just lindly guess where's where.
This should be a MASSIVE help
avatar
Thunderflash
Member
Member

Male Number of posts : 90
Age : 25
Location : Formalhaut B
Registration date : 2008-09-05

View user profile

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum