Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Drawing Problem

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!");
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic