473,486 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

drawing array of lines

56 New Member
i am trying to draw an array of lines where the user will be able to draw multiple lines
here is the code but it is not working well and i don't know why

Expand|Select|Wrap|Line Numbers
  1. import java.applet.Applet;
  2. import java.awt.Graphics;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.Color;
  5. import java.awt.event.MouseMotionListener; 
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8.  
  9.  
  10. public class MultipleLineClass extends Applet{
  11.     int x1,y1,x2,y2;
  12.     MyLine[] obj = new MyLine[5];
  13.     int i=0;
  14.     public void init(){
  15.         addMouseListener(new MyButtonListener());
  16.         addMouseMotionListener(new MyButtonListener());    
  17.  
  18.     }
  19.     public void paint(Graphics g){
  20.         for(int j=0 ; j<i ; j++){
  21.             g.setColor(Color.red);
  22.             g.drawLine(x1,y1,x2,y2);
  23.         }
  24.     }
  25.     class MyButtonListener extends MouseAdapter implements MouseMotionListener,MouseListener{
  26.         public void mousePressed(MouseEvent e){ 
  27.             x1 = e.getX();
  28.             y1 = e.getY();
  29.         }
  30.         public void mouseDragged(MouseEvent e){
  31.             x2 = e.getX();
  32.             y2 = e.getY();
  33.             obj[i] = new MyLine(x1,y1,x2,y2);
  34.             i+=1;
  35.             repaint();
  36.         }
  37.         public void mouseMoved(MouseEvent e){}
  38.     }
  39.  
  40.     class MyLine{
  41.  
  42.         MyLine(int xx1, int yy1, int xx2, int yy2){
  43.             x1=xx1;
  44.             x2=xx2;
  45.             y1=yy1;
  46.             y2=yy2;
  47.         }
  48.  
  49.     }
  50.  
  51. }
Jan 7 '15 #1
1 4175
Norgy
56 New Member
i have modified the code but still not working and i want to know the solution!
Expand|Select|Wrap|Line Numbers
  1. import java.applet.Applet;
  2. import java.awt.Graphics;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.Color;
  5. import java.awt.event.MouseMotionListener; 
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8.  
  9.  
  10. public class MultipleLineClass extends Applet{
  11.     int x1,y1,x2,y2;;
  12.     MyLine[] obj = new MyLine[10];
  13.  
  14.     int i=0;
  15.     public void init(){
  16.         addMouseListener(new MyButtonListener());
  17.         addMouseMotionListener(new MyButtonListener());    
  18.  
  19.     }
  20.     public void paint(Graphics g){
  21.         if(i<10){
  22.             for(int j=0 ; j<i ; j++){
  23.                 g.setColor(Color.red);
  24.                 g.drawLine(obj[j].getX1(),obj[j].getY1(),obj[j].getX2(),obj[j].getY2());
  25.             }
  26.         }
  27.     }
  28.     class MyButtonListener extends MouseAdapter implements MouseMotionListener,MouseListener{
  29.         public void mousePressed(MouseEvent e){ 
  30.             x1 = e.getX();
  31.             y1 = e.getY();
  32.  
  33.         }
  34.         public void mouseDragged(MouseEvent e){
  35.             x2 = e.getX();
  36.             y2 = e.getY();
  37.             obj[i] = new MyLine(x1,y1,x2,y2);
  38.  
  39.             repaint();
  40.         }
  41.  
  42.         public void mouseMoved(MouseEvent e){}
  43.     }
  44.  
  45.     class MyLine{
  46.  
  47.         MyLine(int xx1, int yy1, int xx2, int yy2){
  48.             x1=xx1;
  49.             x2=xx2;
  50.             y1=yy1;
  51.             y2=yy2;
  52.         }
  53.         public int getX1(){return x1;}
  54.         public int getX2(){return x2;}
  55.         public int getY1(){return y1;}
  56.         public int getY2(){return y2;}
  57.  
  58.  
  59.     }
  60.  
  61. }
Jan 8 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
48001
by: Stefan Burger | last post by:
Those of you who are in need for drawing vector lines might be interested in the following code. DrawLine( x1, y1, x2, y2, color ) draws a vector line from any Point x1,y1 to any Point x2,y2 in...
9
3261
by: John Baro | last post by:
I wish to draw a line say 10px wide at a 45 degree angle (or any other angle) and have it shaded say from gray to red to gray again (or any other color). I can get the line by using a...
3
1518
by: peshrad | last post by:
Hi ! I'm using ASP.NET 1.1 and C# . For Microsoft Internet Explorer 6.0, the following code suppresses the drawing of horizontal and vertical lines in the web page:
2
1153
by: Eric Sabine | last post by:
Below is a sub I have which contains 4 lines of code. I would like to reduce it to 1. My problem is that it takes 3 lines to create the array to hold the sql parameter and since there's only one...
5
2359
by: Manuel Daponte | last post by:
I found this code in this newsgroup and used it, but the lines drawn are composed of point too separated when the mouse moves at medium or fast speed. How can I fix it? Thanks in advance !!! ...
11
4347
by: Galen Somerville | last post by:
In my opinion, one of the worst failures of Net (VB2005 Pro) was not providing an XOR line draw feature, as in VB6. A major feature of my app is allowing the user to draw Markers (vertical...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.