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
I've cleared almost 30 gig off of my hard drive in the past 2 weeks, and it will temporarily show that in the Get Info box.  But hours later, I am still getting a disk full error and all of the memory has disappeared.  I have cleared my backup logs f [More]
I have a MacBook Pro 2.4GHz Intel Core 2 Duo with 2GB of RAM running 10.5.7 and every time Time Machine starts a backup (about once an hour) my computer becomes unresponsive. I can't type, copy, paste, switch applications without a huge hang up. For [More]
I was told by my cable company (RCN) that the copy of Firefox I have would need to be removed (I have v. 4.0.) They gave me a microsoft # to call, in order to deal with removing the copy. MS could not help, gave me a # for Firefox, but it was incorre [More]
I have an Android LG phone, I normally connect the cord to my computer and a box pops up to name and import the pics from my phone to My Pictures on computer.  Today the bottom right box is not coming up.  How do I get it back?  I have HP desktop Win [More]
Hi all, Upgraded to Mountain Lion yesterday and to ITunes 11. My Bose Soundlink is "seen" by iTunes but no sound when connected. Volumes / Mute button checked just in case. Router restarted. MacBook Pro restarted. Any ideas ??Update: Just took t [More]
Hi all I am going to shortly join the Mac ranks by purchasing an iMac, but I have a question I cant seem to find an answer for. I know it possible to hook up my iMac to a TV and then output Frontrow on to that to view my media. However is it also pos [More]
Hi , I have a question in Oracle. I am using backend as Oracle and frontend as Java. I am designing applications in java and connecting to the databse using JDBC. or middleware(WEBLOGIC). i want to generate reports client side. Is there any third par [More]
Hi all, I'm trying to run a process flow in oem (using oem_exec_template.sql script), but it keeps failing with error "nms-1013: sqlplus does not exist". I tried running a map too and it failed with same message. Any idea anyone? Thanks! YanetHi [More]
Hello all, I have a global atttribute in my Implementation class. How can I access this attribute from the getter setter method of an attribute in my Context node? Regards, Debolinahi , I discovered that we cannot access the child nodes from parent c [More]
Hi everyone, I am confused on the link between Capacities tab and Scheduling tab when setting up the Work Center. When I run MRP and set it to Lead time scheduling and capacity planning, which figures (for setup, processing, teardown, etc time) are u [More]