CompSci GUI paint() aid
2 posters
Page 1 of 1
CompSci GUI paint() aid
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
Re: CompSci GUI paint() aid
Thank you!
It is EXTREMELY frusterating in the graphics stuff to just lindly guess where's where.
This should be a MASSIVE help
It is EXTREMELY frusterating in the graphics stuff to just lindly guess where's where.
This should be a MASSIVE help
Thunderflash- Member
- Number of posts : 90
Age : 30
Location : Formalhaut B
Registration date : 2008-09-05
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|