Mu Alpha Theta
Would you like to react to this message? Create an account in a few clicks or log in to continue.

CompSci GUI paint() aid

2 posters

Go down

CompSci GUI paint() aid Empty CompSci GUI paint() aid

Post by Michael 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
Michael
Michael
Graduated member
Graduated member

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

http://elkinschess.forummotion.com

Back to top Go down

CompSci GUI paint() aid Empty Re: CompSci GUI paint() aid

Post by Thunderflash 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
Thunderflash
Thunderflash
Member
Member

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

Back to top Go down

Back to top


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