| | |
Blank JFrame
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 15
Reputation:
Solved Threads: 0
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.
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)
import java.awt.*; import javax.swing.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class TrackMouseMovementTest{ public static void main(String[] args){ EventQueue.invokeLater(new Runnable(){ public void run(){ MouseFrame frame = new MouseFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class MouseFrame extends JFrame { public MouseFrame(){ setTitle("Mouse Movement Tracker"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); MouseComponent component = new MouseComponent(); add(component); } public static final int DEFAULT_WIDTH = 500; public static final int DEFAULT_HEIGHT = 500; } class MouseComponent extends JComponent{ public MouseComponent(){ addMouseListener(new MouseHandler()); } private class MouseHandler extends MouseAdapter{ public void mousePressed(MouseEvent event){ x = event.getX(); y = event.getY(); repaint(); } } public void paintComponent(Graphics g){ g.drawString("X: " + x + ",Y: " + y + "",x,y); g.drawString("Click #: " + counter ++ ,x , y + 16); } int x; int y; private static int counter=-1; }
0
#3 17 Days Ago
Don't paint when value of x & y variable is zero.
Java Syntax (Toggle Plain Text)
public void paintComponent(Graphics g){ if(x>0 && y>0) { g.drawString("X: " + x + ",Y: " + y + "",x,y); g.drawString("Click #: " + counter ++ ,x , y + 16); } }
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: May 2008
Posts: 52
Reputation:
Solved Threads: 1
0
#4 17 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.
java Syntax (Toggle Plain Text)
import java.awt.*; import javax.swing.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class TrackMouseMovementTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MouseFrame frame = new MouseFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class MouseFrame extends JFrame { public MouseFrame() { setTitle("Mouse Movement Tracker"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); MouseComponent component = new MouseComponent(); add(component); } public static final int DEFAULT_WIDTH = 500; public static final int DEFAULT_HEIGHT = 500; } class MouseComponent extends JComponent { public MouseComponent() { firstClick = false; addMouseListener(new MouseHandler()); } private boolean firstClick; private class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent event) { x = event.getX(); y = event.getY(); counter++; firstClick = true; repaint(); } } public void paintComponent(Graphics g) { if (firstClick) { g.drawString("X: " + x + ",Y: " + y + "", x, y); g.drawString("Click #: " + counter, x, y + 16); } } int x; int y; private static int counter = 0; }
![]() |
Similar Threads
- Just a blank screen = ( (Monitors, Displays and Video Cards)
- Passing data from one JFrame to another (Java)
- Need Help with JFrame (Java)
- having some trouble with my project (Java)
- new window in Internet Explorer is always blank (Web Browsers)
- Abstract Error Message (Java)
- HELP!! comp froze and when i restarted it was blank! (Troubleshooting Dead Machines)
- IE 6 start page getting changed to about:blank (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Even Odd number synchronization using Threads
- Next Thread: how to display private methods in javadoc?
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free ftp game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linked linux list loan machine map method methods migrate mobile netbeans objects oriented output phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext scanner se server service set sms software sort sql string swing test textfield threads tree trolltech ubuntu utility windows






