| | |
Blank JFrame
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
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 Nov 7th, 2009
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); } }
•
•
Join Date: May 2008
Posts: 54
Reputation:
Solved Threads: 1
0
#4 Nov 7th, 2009
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 |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor






