• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to shrink/grow lines

 
Giri Regu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi To all

i am developing a paint application in applet.
i am very strange for this applications.
i am facing lot of problems in that.
that is listed below.
1.how to select a particular from morethan one line.
2.how to move line around panel easily
3.how to grow and shrink the selected lines.

please give a solution for the above my questions.

i am developing some initial code for this application thst is below


using this code we can draw lines and gow also but its not full furnished
.
please help me.

thanks in advance

regards
Giri

[ UD: added code tags ]
[ March 09, 2007: Message edited by: Ulf Dittmer ]
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the code do now, and what do you want it to do? Where are you stuck implementing the changes?
 
Giri Regu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
* <applet code="DrawLine.class" height=500 width=500>
* </applet>
*/

public class DrawLine extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
int width, height;
int x, y; // the coordinates of the upper-left corner of the box
int mx, my; // the most recently recorded mouse coordinates
boolean isMouseDragging = false;
Graphics g;
int flag=0;

public void init() {
width = getSize().width;
height = getSize().height;

setBackground( Color.black );

x = width/2 - 40 ;
y = height/2 - 40;

Button crt_btn = new Button("Create Line");
add(crt_btn);
crt_btn.addActionListener(this);
addMouseListener( this );
addMouseMotionListener( this );
}


public void actionPerformed(ActionEvent e)
{

if(e.getActionCommand()== "Create Line")
{
flag=1;
System.out.println("btn clicked");
repaint();
}
}

public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ){}
public void mousePressed( MouseEvent e ) {
mx = e.getX();
my = e.getY();
if ( mx > x && mx < x+80 && my == y) {
isMouseDragging = true;
}
e.consume();
}
public void mouseReleased( MouseEvent e ) {
isMouseDragging = false;
e.consume();
}
public void mouseMoved( MouseEvent e ) { }
public void mouseDragged( MouseEvent e ) {
if ( isMouseDragging ) {
// get the latest mouse position
int new_mx = e.getX();
int new_my = e.getY();

// displace the box by the distance the mouse moved since the last event
// Note that "x += ...;" is just shorthand for "x = x + ...;"
x += new_mx - mx;
y += new_my - my;

// update our data
mx = new_mx;
my = new_my;

repaint();
e.consume();
}
}

public void paint( Graphics g ) {
g.setColor( Color.blue );
if(flag==1)
{
g.drawLine( x, y, x+80, y );
}
}
public void update(Graphics g ) {

paint(g);
}
}

this is my program .
in this program i can draw line when click the createline button.
wnen i try to move the line so many lines created in th emoving path.
this is
my first problem
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your applet contains antique event code. Here is a link into the java tutorial where you
can learn how to write modern event handling code for your applet: Lesson: Writing Event Listeners.
1.how to select a particular from morethan one line.
2.how to move line around panel easily

I modified your code to demonstrate one (of many possible) way(s) to do these two things.
For the resizing of lines you will have to decide what kind of resizing behavior you want
and how you want the user to interact with your applet to get the desired behavior.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(e.getActionCommand()== "Create Line")

Don't ever compare Strings with "==" - use the "equals" method.
 
Giri Regu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanks craig thanks a lot.
your code is working properly.but i am using jdk1.18 version so the graphics2d and methods in Rectangle class insersectLine() ans setFrameCenter() is not available.
its my mistake to notice about version which i am working. anyway i am trying to convert in lower version . i am unable to understand the usage of rectangle class methods(mentioned above).
if you dont mine please tell me the logic of your program.
sorry for the inconvenence.

thanks and regards
Giridaran.r
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oi am unable to understand the usage of rectangle class methods


Well, put a little effort into it. Learning how to read and understand code is an important part of learning to program.

Are you familiar with the Rectangle class? Take a look at the 4 points used to define it - what do those stand for in the code? Why does that area need to be repainted (which is all that happens with the Rectangle)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic