Results 1 to 15 of 15
Thread: Paint program
- 05-06-2011, 12:09 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Paint program
I am writing a program where if the user clicks, it should draw a point. The following code does not seem to work properly. If I click, it draws a point but vanishes later on. I tried using paint and paint component but I am not able to use them. Please help!!
Java Code:import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class GUIClass extends JFrame{ /** * */ private static final long serialVersionUID = 1L; public Container c; JButton jbtnStart; JButton jbtnEnd; JPanel panel; Border border; StartListener start; ClickHandler click; EndListener end; Point point; int width,height,x,y; Image backbuffer; Graphics bg; public static void main (String[] args) { new GUIClass(); } GUIClass() { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setSize(screenSize); setResizable (false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c = getRootPane(); c.setLayout(null); panel = new JPanel(); panel.setBounds(10,10,screenSize.width-30,screenSize.height-100); border = BorderFactory.createLineBorder(Color.black,4); panel.setBackground(Color.WHITE); panel.setBorder(border); click = new ClickHandler(); panel.addMouseListener(click); c.add(panel); jbtnStart = new JButton("START"); jbtnStart.setBounds(750, screenSize.height-80, 100, 50); start = new StartListener(); jbtnStart.addActionListener(start); c.add(jbtnStart); jbtnEnd = new JButton("END"); jbtnEnd.setBounds(900, screenSize.height-80, 100, 50); end = new EndListener(); jbtnEnd.addActionListener(end); c.add(jbtnEnd); setVisible(true); } class StartListener implements ActionListener { public void actionPerformed(ActionEvent e1) { } } class EndListener implements ActionListener { public void actionPerformed(ActionEvent e1) { System.exit(0); } } /*public void update(Graphics g) { bg.fillOval(x-5, y-5, 10, 10); } public void paintComponent (Graphics g) { super.paint(g); bg.fillOval(x-5, y-5, 10, 10); update(g); }*/ class ClickHandler implements MouseListener { @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub x = e.getX(); y = e.getY(); System.out.println(x+" "+y); bg = panel.getGraphics(); bg.fillOval(x, y, 50, 50); repaint(); e.consume(); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } } }
- 05-06-2011, 12:13 AM #2
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
what do you mean by "disappears later on"?
- 05-06-2011, 12:20 AM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
means when i click, a point appears and immediately vanishes... i mean i can see a point being placed on the the JPanel where I click however it does not get drawn there. If you could try running the code you will know what i mean.
- 05-06-2011, 12:32 AM #4
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
getContentPane().repaint();
that is all.
youre welcomeLast edited by wassat676; 05-06-2011 at 12:45 AM.
- 05-06-2011, 04:13 AM #5
Won't help since the OP is wrongly using getGraphics() and not performing custom painting in the correct way.
Never use getGraphics() of a Component in client code.
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
- 05-06-2011, 05:43 AM #6
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
true story. The OP is not what I would have done, but it seemed to fix the problem in this case.4
*Figuring out a mistake is rewarding despite how its implemented.Last edited by wassat676; 05-06-2011 at 05:55 AM.
-
There's a big difference between a fix and a kludge. I fix solves the problem, end of story. A kludge puts a band aid on the wound, packs it dresses it, and allows for functional use, but the wound still remains and still can fester and explode at the most inopportune times. Your suggestion is a kludge and doesn't address the real underlying issue as Darryl has described.
- 05-06-2011, 05:57 AM #8
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
alright so Darryl wins -1 wassat676 congratulations.
-
- 05-06-2011, 06:05 AM #10
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
glad i could help
-
- 05-06-2011, 06:19 AM #12
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
lived and learned
- 05-06-2011, 06:34 AM #13
Some other comments for the OP:
-- Declare variables in the tightest possible scope. All those variables don't need to be instance fields.
-- Most of the time, instance fields should be declared as private, unless ther is a clear requirement of direct access from outside the class.
-- Use meaningful variable names. Leave aside others, even you may be puzzled when you revisit the code after 6 months or so.
-- Always construct, manipulate and query all Swing components only on the EDT.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Threads and Swing
The Last Word in Swing Threads
db
edit
-- Don't play with the layout of the rootPane
-- Don't add anything directly to the rootPane
edit2
-- By extending MouseAdapter instead of implementing MouseListener, you could eliminate the empty methods.
-- Don't use a null layout. Learn to use layout managers
http://download.oracle.com/javase/tu...out/index.html
Heck, just go through the entire trail and learn all about coding a Swing GUI!
http://download.oracle.com/javase/tu...ing/index.htmlLast edited by DarrylBurke; 05-06-2011 at 06:40 AM.
- 05-06-2011, 06:46 AM #14
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
I guess since were all here, can I ask why its bad to use a null layout?
i.e. my thread below
- 05-06-2011, 06:55 AM #15
Similar Threads
-
paint program
By dewdadamnthang in forum New To JavaReplies: 4Last Post: 03-30-2011, 11:40 AM -
Help with paint program
By michcan in forum Java 2DReplies: 1Last Post: 02-04-2011, 06:26 AM -
Paint Program Help
By ngiannini in forum AWT / SwingReplies: 12Last Post: 05-10-2010, 04:24 PM -
Simple Paint program question
By StressaJune in forum New To JavaReplies: 1Last Post: 03-30-2009, 08:46 PM -
just started paint program. am i going the right direction?
By diggitydoggz in forum New To JavaReplies: 3Last Post: 12-31-2008, 05:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks