Sitemap

Amicuk Programming Answers

Write a program that draws a line from one mouse click spot to the next.

-0001-11-30   Views:0

Advertisement

WHERE AM I GOING WRONG? import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.geom.*; public class Week8Lab extends Applet implements MouseListener      private int[] x;      private int[] y;      private int count;      p

WHERE AM I GOING WRONG?
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.geom.*;
public class Week8Lab extends Applet implements MouseListener
     private int[] x;
     private int[] y;
     private int count;
     public void init()
          x = new int[10];
          y = new int[10];
          count = 0;
          this.addMouseListener(this);
     public void paint(Graphics g)
          for(int i = 0; i < 10; i++)
               g.drawLine(x[0], y[0], x[i]+1, y[i]+1);               
     public void mouseEntered(MouseEvent e) {}
     public void mouseExited(MouseEvent e) {}
     public void mousePressed(MouseEvent e)
          x[count % 10] = e.getX();
          y[count % 10] = e.getY();     
          count++;
          repaint();
System.out.println ("Mouse X =" + e.getX() + "Mouse Y =" + e.getY());
     public void mouseReleased(MouseEvent e) {}
     public void mouseClicked(MouseEvent e) {}
WHERE AM I GOING WRONG?

The replay answer
Advertisement
Try this:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.geom.*;
import javax.swing.*;
public class Week8Lab extends JFrame implements MouseListener {
     private int lastX = -1;
     private int lastY = -1;     
     private int newX = 0;
     private int newY = 0;     
     public Week8Lab(){
          this.addMouseListener(this);
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     public void paint(Graphics g){
          if(lastX < 1){
               lastX = newX;
               lastY = newY;
               return;
          g.drawLine(lastX, lastY, newX, newY);
          lastX = newX;
          lastY = newY;
     public void mouseEntered(MouseEvent e) {}
     public void mouseExited(MouseEvent e) {}
     public void mousePressed(MouseEvent e){
          newX = e.getX();
          newY = e.getY();
          repaint();
     public void mouseReleased(MouseEvent e) {}
     public void mouseClicked(MouseEvent e) {}
     public static void main(String[] args){
          JFrame fr = new Week8Lab();
          fr.setSize(300, 300);
          fr.setVisible(true);
}

Go to See the other 5 answers

Write a program that draws a line from one mouse click spot to the next.

Category:DefaultRelease time:-0001-11-30Views:130

WHERE AM I GOING WRONG? import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.geom.*; public class Week8Lab extends Applet implements MouseListener      private int[] x;      private int[] y;      private int count;      p[More]

Drawing a Line in JAVA

Category:DefaultRelease time:2015-10-11Views:130

Well Ive got my program to draw a line, but when I drag the mouse across the screen I'd like it to show its actual progress. Ill try to explain this better, if I click my mouse on one point on the screen and drag it to the enxt the line doesnt show u[More]

Drawing Perpendicular Lines...

Category:DefaultRelease time:-0001-11-30Views:130

All, I have a program that draws simple lines using the Line2D class. As I draw the line, I bisect it to get a point in the middle of the line. Now I have 3 points on my line. From here I am trying to draw a short perpendicular line originating from[More]

Problem in drawing a line in Java Application Program

Category:DefaultRelease time:2015-10-11Views:130

Hi, I am trying to draw a line after a message is displayed on the screen. But the line is not at all coming on the screen while running. but I can see a red line is appearing on the screen first and and it is getting overridden. There is something w[More]

How to draw a line on ADF page between two nodes  for mapping.

Category:DefaultRelease time:2015-10-11Views:130

Hi everyone, Does anyone have a solution that how can I wiring two points by drawing a line on ADF pages. My scenario is user want to do a mapping between two xml files. We will build an ADF faces page. This page have two parts, left part contains on[More]

Draw a line it creates a path???

Category:DefaultRelease time:-0001-11-30Views:130

Hi Just installed Fireworks 8 and everytime I draw a basic line it creates a path Im used to fireworks four so is this normal ? Surely it should just draw the line on the image and not create seperate paths for each one ..far as I know I never select[More]

Draw a Line

Category:DefaultRelease time:-0001-11-30Views:130

I want to draw a line for a given co-ordinate values (x, y). i developed one program for that line. But if i give small numbers the line is appearing verry small. But i want the line for small and big numbers. please help me.I have a related question[More]

Draw a line on a CWImaqViewer durinq a continuos aCquisition

Category:DefaultRelease time:-0001-11-30Views:130

Can I Draw a line on a CWImaqViewer during a Continuos Acquisition. I Retrieve even the same error:"the object has been locked by an ongoing acquisition and may not be modified"Hi, I'm using NI-IMAQ 2.6.1, Vision 6.1 and a PCI-1407. I ran the fo[More]

When im in photoshop drawing and i stop it starts drawing striaght lines

Category:DefaultRelease time:-0001-11-30Views:130

okay, When I go into photoshop to draw ill be drawing and then stop to do something like change the color, the size of the brush, what kind of brush, ect. and go back to drawing it doesn't free draw it just draws straight lines. I have another progra[More]

Illustrator drawing straight lines crooked

Category:DefaultRelease time:-0001-11-30Views:130

All straight lines in Illustrator appear crooked on my screen. This is hard to explain. I've been using AI since 1991 and have never seen this problem, which started happening out of the blue a few months ago, even though I have not changed anything[More]

How to draw multiple lines on same panel??

Category:DefaultRelease time:2015-10-11Views:130

hiya i would like to know how can I draw multiple lines on the same panel ?? I have already use repaint(); but it just come out the lastest line (say line 3) i draw .......those previous lines(say line 1 and 2) are disappear ........ Thanks for your[More]

Hot
My line of code is - ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream)reportClientDoc.getPrintOutputController().export(ReportExportFormat.PDF); I used Eclipse to generate the .jar file and that line of code worked. I am switching fr [More]
Hi Can anyone help please? I can't send any files from my Mac! The bluetooth works, I can receive files but I cannot open the bluetooth file exchange to send files from my Mac! I use a Macbook with OS 10.4.11. Thank you!Hi Can anyone help please? I c [More]
Hi Frnds, I made a vendor payment by T.code f-53 ( i credited the SBI Bank), but i assigned the check number using fch5 and fchu of other bank ie. HDFC. So accordingly i reset and reversed the payment document. And i again made payment to the vendor. [More]
Hi I have configured on JDBC reciever Adapater...... and i have given the paramenter like following JDBC Drvier : com.microsoft.jdbc.sqlserver.SQLServerDriver Connection : jdbc:microsoft:sqlserver://sapep:1433;DatabaseName=DataBaseName; Persistence : [More]
I'm presuming my old iPod id dead meat, but after 24 charging a get the Apple Logo followed by an image that looks like a file folder with an exclamation point. Is there anyway we can resurrect all trhe music on it & transfer into a new iPod?You can [More]
I need to change the phone number that is displayed but when I tried, my account wants to send a text. I do not want to use a moble number. How can I do this?wande wrote: I need to change the phone number that is displayed but when I tried, my accoun [More]
Hi, We have recently upgraded from SAP 4.7 to SAP ECC 6.0. For client requirements, they have developed ZFORM16 in 2006 by copying the standard form HINCF160. But after upgradation, when we execute ZFORM16  for 2006 data we are getting the error mess [More]
Hi, apologies for what might seem a trivial question, but I'm stuck. I have 3 tables, one called Project, one called Issues, and one called Risks.  Each contains a row called ProjectGUID. I want a query that will return the Project Name, Issue Name, [More]
Dear Team, I developed workflow for PO release, but i am not getting the attachment with the work items. To get a attachment i did like this SWO1> selected the Methodsingle release>go to Basic data> default tab--> Method display                [More]
After the latest update, the buttons on the screen of the phone while telephoning are far too sensitive. For instance, it goes to Facetime acccidently by being too near to my cheek/ear. Is there some way to make the buttons less sensitive? Thank youH [More]