Hello guys,
I have a class that extends JComponent. My problem is i can't draw line ( such a pencil ) by dragging mouse -- there is only one dot moved. Give me reasons ! Thank you !
Printable View
Hello guys,
I have a class that extends JComponent. My problem is i can't draw line ( such a pencil ) by dragging mouse -- there is only one dot moved. Give me reasons ! Thank you !
probably because you would like the compiler to read your mind as you would like us to read your mind when you don't post any code! :P
OK ! this is the parts of code to process like a pencil
class DrawingArea extends JComponent implements MouseListener, MouseMotionListener {
....
ArrayList<Shape> shapes = new ArrayList<Shape>();
Point startPencilDrag, endPencilDrag;
....
public void mousePressed(MouseEvent me) {
// pencil
if(cases == 6) {
startPencilDrag = new Point(me.getX(),me.getY());
endPencilDrag = startPencilDrag;
}
....
public void mouseReleased(MouseEvent me) {
// pencil
if( cases == 6) {
Shape pencil = makeLine(startPencilDrag.x,startPencilDrag.y,me.ge tX(),me.getY());
pencils.add(pencil);
shapes.add(pencil);
startPencilDrag = null;
endPencilDrag = null;
}
...
public void mouseDragged(MouseEvent me) {
// pencil
if( cases == 6) {
startPencilDrag = endPencilDrag;
endPencilDrag = new Point(me.getX(),me.getY());
}
....
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
case 6: // pencil
{
if( startPencilDrag != null && endPencilDrag != null) {
Shape p = makeLine(startPencilDrag.x,startPencilDrag.y,
endPencilDrag.x,endPencilDrag.y);
g2.draw(p);
startPencilDrag = endPencilDrag;
}
//for(Shape s: pencils) {
for(Shape s: shapes) {
g2.draw(s);
}
...
private Line2D.Float makeLine(int x1,int y1,int x2,int y2) {
return new Line2D.Float(x1,y1,x2,y2);
}
that is all codes i do to draw on JComponent like a pencil ! I can't post all code of my program because it is too long ! Thanks !
Mouse drag event fires off after an interval. So one one drag of the mouse, the mouse event can be fired off many times all at short distances. Unless you drag your mouse fast, your line will only be a few pixels long.
It might be better to store the start point when you initially click the mouse and have the end point the current position of the drag. Or you could store all the points along the drag and draw them all.
Edit:
Try modifying to this:
Code:public void mouseDragged(MouseEvent me) {
// pencil
if( cases == 6) {
//startPencilDrag = endPencilDrag; //leave the start alone.
endPencilDrag = new Point(me.getX(),me.getY());
}
Custom Painting Approaches gives a working example you should be able to customize.
I did try this but still not succeed.. as the same code when i use the class extends JPanel it runs well but because i have shapes like rectangles or circles which do not run wel on JPanel. So i need to keep it extend from JComponent ? help me with other approachs on JComponent.
Best to show us the code and the specific problem.
Huh? This doesn't make much sense.Quote:
as the same code when i use the class extends JPanel it runs well but because i have shapes like rectangles or circles which do not run wel on JPanel. So i need to keep it extend from JComponent
Correction: best to show us compilable code, an SSCCE (check out the link). And please don't forget the code tags.
OK. this is shortened code but it is compilable
}Code:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.geom.*;
import java.util.*;
class DrawingArea extends JComponent implements MouseListener, MouseMotionListener {
ArrayList<Shape> shapes = new ArrayList<Shape>();
Point startLineDrag,endLineDrag;
DrawingArea() {
startLineDrag = null; endLineDrag = null;
}
public void mousePressed(MouseEvent me) {
// pencil
if(cases == 6) {
startPencilDrag = new Point(me.getX(),me.getY());
endPencilDrag = startPencilDrag;
}
}
public void mouseReleased(MouseEvent me) {
// pencil
if( cases == 6) {
Shape pencil = makeLine(startPencilDrag.x,startPencilDrag.y,me.getX(),me.getY());
pencils.add(pencil);
shapes.add(pencil);
startPencilDrag = null;
endPencilDrag = null;
}
}
public void mouseDragged(MouseEvent me) {
// cap nhat cho pencil
if( cases == 6) {
startPencilDrag = endPencilDrag;
endPencilDrag = new Point(me.getX(),me.getY());
}
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
switch(cases) {
case 6: // pencil
{
for(Shape s: shapes) {
g2.draw(s);
}
if( startPencilDrag != null && endPencilDrag != null) {
Shape p = makeLine(startPencilDrag.x,startPencilDrag.y,
endPencilDrag.x,endPencilDrag.y);
g2.draw(p);
startPencilDrag = endPencilDrag;
}
}
private Line2D.Float makeLine(int x1,int y1,int x2,int y2) {
return new Line2D.Float(x1,y1,x2,y2);
}
}
OK. this is shortened code but it is compilable
}Code:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.geom.*;
import java.util.*;
class DrawingArea extends JComponent implements MouseListener, MouseMotionListener {
ArrayList<Shape> shapes = new ArrayList<Shape>();
Point startLineDrag,endLineDrag;
DrawingArea() {
startLineDrag = null; endLineDrag = null;
}
public void mousePressed(MouseEvent me) {
// pencil
if(cases == 6) {
startPencilDrag = new Point(me.getX(),me.getY());
endPencilDrag = startPencilDrag;
}
}
public void mouseReleased(MouseEvent me) {
// pencil
if( cases == 6) {
Shape pencil = makeLine(startPencilDrag.x,startPencilDrag.y,me.getX(),me.getY());
pencils.add(pencil);
shapes.add(pencil);
startPencilDrag = null;
endPencilDrag = null;
}
}
public void mouseDragged(MouseEvent me) {
// pencil
if( cases == 6) {
startPencilDrag = endPencilDrag;
endPencilDrag = new Point(me.getX(),me.getY());
}
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
switch(cases) {
case 6: // pencil
{
for(Shape s: shapes) {
g2.draw(s);
}
if( startPencilDrag != null && endPencilDrag != null) {
Shape p = makeLine(startPencilDrag.x,startPencilDrag.y,
endPencilDrag.x,endPencilDrag.y);
g2.draw(p);
startPencilDrag = endPencilDrag;
}
}
private Line2D.Float makeLine(int x1,int y1,int x2,int y2) {
return new Line2D.Float(x1,y1,x2,y2);
}
}
That is not a SSCCE.
I gave you a working example of drawing rectangle on a JPanel. Apparently you didn't bother to read that link or the link on how to create a SSCCE.Quote:
i have shapes like rectangles or circles which do not run wel on JPanel. So i need to keep it extend from JComponent ? help me with other approachs on JComponent.
thank all ! I have fixed my problem ! that is because i used the wrong algorithm !
[QUOTE=camickr; gives a working example you should be able to customize.[/QUOTE]
I follow your guide , it works perfectly in my program ( i am developing a program like MS Paint). But problem is when i fill color , i have to use BufferedImage and then i use BufferedImage graphics context to draw. At this moment it works wrong like draw a lot of rectangles from start point. When I use normal Graphis2D context it works on the unfilled area and hide on the filled area. Please camickr help me. I don't have much time anymore.