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
Hi Experts, I have created an alert in SBO and selected email and internal option for same. I get internal message however email is not getting delivered, I checked the Event Viewer of Windows and below error is shown for SBOMail. The description for [More]
Hello. My problem is as follows. I built up a ConnectionPool with OracleDataSource over JNDI, which was found and worked fine. Since I installed OpenEJB and created a stateful session bean, the datasource is not found anymore. I'am using Tomcat 5.5, [More]
Hi, I'm getting puzzled. I have: New Airport Time Capsule MBP 15 Retina MBP 15 2008y ATV3 Broadband with wifi SkyHub router iPhone 5s To use the Capsule as wireless external Hard drive does it needs to be always WAN cabled to SkyHub wifi broadband ro [More]
I recent bought a 15" Macbook Pro (2012 Model without the Retina Display). I love it, but I notice there is a little 1/2" slit on the right front edge of the laptop. Looking on the Tec Specs Page that lists all the ports and connections it is no [More]
Thanks for looking, I have this menu I made: http://www.ivoog.com/link4 How can I make the background image fit? You can see that its kindof messed up. How do I make it fit? Please explain or show. Thanks!As explained in previous posts, you cannot do [More]
I need help setting up an apple id for my son's phone. It seems his email address has been taken and/or we cant get the password.Two different things If you forgot the password If you forgot your Apple ID password - Apple Support If the email ID you [More]
Dear IT Experts, I maintain a SUN Server. It is SUN Fire F4800. To login , I must use HyperTerminal, serial cable , and Laptop. I have a plan to shutdown this machine for about 30 minutes for hardware maintenance checking. But, I can not shutdown it [More]
why the two-step verification is not found in my account?roberto e., You may need to change the language or time zone used for your iCloud account as detailed in the article linked below. iCloud: Change the language or time zone on iCloud.com http:// [More]
Hi,     I have configured the vendor evaluation to look at the rejection/complaints sub criteria with the scoring method as 8 = Automatic Determ. from Quality Stat.: Quality Notification. The first time I create a notification, the score drops from 1 [More]
Hi All , we have sun java messaging server 7.3 directory server 6.2.we have migrated the all mail box send mail messaging server,Now how can we migrate the address book. Regards kamalkkdas wrote: we have sun java messaging server 7.3 directory server [More]