Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing Problem  RSS feed

 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Respected Sirs,

I am trying to develope a program , which includes drawing 'Lines' and 'Ovals' etc. The co-ordinates taking when Mouse Events trigerring.
This is working with clearly as we expected. But when minimaizing the window the contents are destroyed. I am trying with JPanel and JFrame. But the probelm is same. How to solve this type of problems. Please help me. Thanks in advance.

Regards Francis.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the component that is doing the drawing *storing* the shapes you want to draw, so that it can repaint them when needed, for example, after iconifying and deiconifying? Or are you doing your shape drawing in mouse events, which is usually a bad idea. My rule of thumb is: never call Component's getGraphics(), since what you then render will not be retained.
 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Respected Sir,
I can't understand what do you mean. Without MouseEvents how to get (x,y) co-ordinates.I want to draw shapes arbitarly and desirable locations. Please give some sample codes. Please helpe me,it is urgent. Here is my program.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Draw extends JFrame implements MouseListener
{
int x,y;

Draw()
{
addMouseListener(this);
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
}

public void mouseReleased(MouseEvent me)
{
int x1=me.getX();
int y1=me.getY();
Graphics g=this.getGraphics();
g.drawLine(x,y,x1,y1);

}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}

public static void main(String[] args)
{
Draw dw=new Draw();
dw.setSize(300,300);
dw.setVisible(true);
System.out.println("Hello World!");
}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a simple example (reply #6)

http://forum.java.sun.com/thread.jspa?threadID=705313

it just draws lines, but should be enough to give you an idea
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!