• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to shrink/grow lines

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
reply
    Bookmark Topic Watch Topic
  • New Topic