944,191 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 985
  • Java RSS
Nov 7th, 2009
0

Blank JFrame

Expand Post »
Hi guys,
The following program works the way I want it to but there's one small problem; when it loads, it doesn't give a blank frame. It want it to show the output after I click on the blank area. Help me please.
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5.  
  6. public class TrackMouseMovementTest{
  7.  
  8. public static void main(String[] args){
  9.  
  10. EventQueue.invokeLater(new Runnable(){
  11. public void run(){
  12. MouseFrame frame = new MouseFrame();
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. frame.setVisible(true);
  15. }
  16. });
  17. }
  18. }
  19.  
  20. class MouseFrame extends JFrame
  21. {
  22. public MouseFrame(){
  23. setTitle("Mouse Movement Tracker");
  24. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  25.  
  26. MouseComponent component = new MouseComponent();
  27. add(component);
  28. }
  29. public static final int DEFAULT_WIDTH = 500;
  30. public static final int DEFAULT_HEIGHT = 500;
  31. }
  32.  
  33. class MouseComponent extends JComponent{
  34. public MouseComponent(){
  35. addMouseListener(new MouseHandler());
  36. }
  37.  
  38. private class MouseHandler extends MouseAdapter{
  39. public void mousePressed(MouseEvent event){
  40. x = event.getX();
  41. y = event.getY();
  42. repaint();
  43. }
  44. }
  45. public void paintComponent(Graphics g){
  46. g.drawString("X: " + x + ",Y: " + y + "",x,y);
  47. g.drawString("Click #: " + counter ++ ,x , y + 16);
  48. }
  49. int x;
  50. int y;
  51. private static int counter=-1;
  52. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babylonlion is offline Offline
19 posts
since Oct 2009
Nov 7th, 2009
1
Re: Blank JFrame
Introduce own boolean flag 'pressed' and use it to switch needed behavior
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 7th, 2009
0
Re: Blank JFrame
Don't paint when value of x & y variable is zero.
Java Syntax (Toggle Plain Text)
  1. public void paintComponent(Graphics g){
  2. if(x>0 && y>0) {
  3. g.drawString("X: " + x + ",Y: " + y + "",x,y);
  4. g.drawString("Click #: " + counter ++ ,x , y + 16);
  5. }
  6. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Nov 7th, 2009
0
Re: Blank JFrame
As the previous poster said, use a boolean for first click, also increment your counter in mousePressed. If you increment it in paintComonent, counter will be incremented everytime the frame is repainted. For example when you resize your frame, your counter dramatically changes. This is the fixed code.
java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5.  
  6. public class TrackMouseMovementTest {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. EventQueue.invokeLater(new Runnable() {
  11. public void run() {
  12. MouseFrame frame = new MouseFrame();
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. frame.setVisible(true);
  15. }
  16. });
  17. }
  18. }
  19.  
  20. class MouseFrame extends JFrame {
  21. public MouseFrame() {
  22. setTitle("Mouse Movement Tracker");
  23. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  24.  
  25. MouseComponent component = new MouseComponent();
  26. add(component);
  27. }
  28.  
  29. public static final int DEFAULT_WIDTH = 500;
  30. public static final int DEFAULT_HEIGHT = 500;
  31. }
  32.  
  33. class MouseComponent extends JComponent {
  34. public MouseComponent() {
  35. firstClick = false;
  36. addMouseListener(new MouseHandler());
  37. }
  38.  
  39. private boolean firstClick;
  40.  
  41. private class MouseHandler extends MouseAdapter {
  42. public void mousePressed(MouseEvent event) {
  43. x = event.getX();
  44. y = event.getY();
  45. counter++;
  46. firstClick = true;
  47. repaint();
  48. }
  49. }
  50.  
  51. public void paintComponent(Graphics g) {
  52. if (firstClick) {
  53. g.drawString("X: " + x + ",Y: " + y + "", x, y);
  54. g.drawString("Click #: " + counter, x, y + 16);
  55. }
  56. }
  57.  
  58. int x;
  59. int y;
  60. private static int counter = 0;
  61. }
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008
Nov 7th, 2009
0
Re: Blank JFrame
I appreciate the explanation ...that was very helpful
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babylonlion is offline Offline
19 posts
since Oct 2009
Nov 7th, 2009
0
Re: Blank JFrame
Thank you for the help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babylonlion is offline Offline
19 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Even Odd number synchronization using Threads
Next Thread in Java Forum Timeline: how to display private methods in javadoc?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC