Blank JFrame

Thread Solved

Join Date: Oct 2009
Posts: 15
Reputation: babylonlion is an unknown quantity at this point 
Solved Threads: 0
babylonlion babylonlion is offline Offline
Newbie Poster

Blank JFrame

 
0
  #1
19 Days Ago
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
1
  #2
19 Days Ago
Introduce own boolean flag 'pressed' and use it to switch needed behavior
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,601
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 460
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #3
19 Days Ago
Don't paint when value of x & y variable is zero.
  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. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 53
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training
 
0
  #4
19 Days Ago
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: babylonlion is an unknown quantity at this point 
Solved Threads: 0
babylonlion babylonlion is offline Offline
Newbie Poster
 
0
  #5
19 Days Ago
I appreciate the explanation ...that was very helpful
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: babylonlion is an unknown quantity at this point 
Solved Threads: 0
babylonlion babylonlion is offline Offline
Newbie Poster
 
0
  #6
19 Days Ago
Thank you for the help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC