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
We're using ASP.net with .Net 4, developing with Visual Studio 2012.  We use SSRS 2012sp1 with local reports, so exporting the report ourselves instead of using a reportviewer control (and not using the SQL Reporting service).  The reports are execut [More]
How can I transfer music from my old iphone 4 to the new iphone 5S without having the music on the old phone synced to my laptop (e.g. my itunes library on the laptop is empty) ? I tried the "Transfer Purchases" option - however this only worked [More]
Trying to burn a audio cd on itunes keep getting error code 4265 please helpOh man it looks like a problem with the web server. Its an error that happens on SAP, even my dad reported an error like this to me but i wasn't able to solve it. What i find [More]
Hi experts, I created a BADI for feeding a virtual characteristicand when I launch the query, this message appears: Subroutine call failed: The index specified had the value 6.288, which was either too large or too small An exception with the type CX [More]
i have a server running server standard 2012 r2 recently upgraded from server standard 2003 it is a dc,dchp,dns. i am trying to install exchange server 2013 standard during the installation i get the following error message. and am unable to continue [More]
Hi, IS there some FM or another posibility to set get sum of all invoces of one month for selectes Cost Center?   regarda, Joannasee the following FM CALL FUNCTION 'CO_DOCUMENT_RECORD' OR   select kostl     into table itbseg     from bseg    for all [More]
My site looks great when i preview (firefox & Safari) in dreamweaver, but when i go to the internet it's not showing my corrections anymore.  Why is that?Here is the coding for my site the link is http://www.amethystenvironmentalllc.com.  I re-did th [More]
Hello folks: One of my customers send a access database *.mdb file. Rigth now, a developer takes these file, open it, take the specific table and save its content into a csv file, and finally import its content into a temporary table, to process the [More]
I just downloaded and installed DWCC and found that my workspace options dropdown only contains Compact and Expanded Options. No Classic, Designer, etc. I am on an IMac running OS 10.8.4. Is this correct? If not, how do I get the other options?I don' [More]
So, I'm the world's biggest idiot, and I was trying to reformat my hard drive so that I could bring the documents from my Mac to my PC. All of the docs were perfectly organized in folders and subfolders a long time ago, and once that had been done, I [More]