473,549 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ 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 4180
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
48015
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 any color (string format #RRGGBB) by using div-tags. regards Stefan ###############code starts here#########################
9
3271
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 GraphicsPath object but cannot work out the shading part. Is this also possible for say a circle with an outside line width of 10px to be shaded from red...
3
1523
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
1161
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 dimension in the array, I'd like to do something more like what's at the bottom of this post, but a lack of understanding prevents me from doing it...
5
2365
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 !!! Dim bTracking As Boolean Dim blackPen As Pen Dim g As Graphics
11
4356
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 lines) on a trace so they can measure the time difference between markers. Once they click on a desired location, on a trace, arrow buttons are...
0
7527
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7485
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1953
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 we have to send another system
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.