| | |
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 19 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: 53
Reputation:
Solved Threads: 1
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.
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 |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






