Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
// this is a program of a user of this forum
// right click mean drag the point location
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Task1e extends JPanel
implements MouseMotionListener , MouseListener
{
final static int max=1000;
Point[] arrayPoints= new Point[max];
boolean pointSelected[] = new boolean[max];
int ncount=0, startDragX=0, startDragY=0, endDragX=0, endDragY=0;
boolean dragJustStart = false;
public Task1e(){
addMouseListener(this); addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
public void mousePressed(MouseEvent e){
if(e.isMetaDown()){}//right
//if(e.isAltDown () )
else{ if(ncount<arrayPoints.length)
{
if (!pointOccupy(e.getX(),e.getY(),false))
{
arrayPoints[ncount++]=new Point(e.getX(),e.getY());
//System.out.println(" "+arrayPoints[ncount-1]);
}
}
}
}
public void mouseReleased(MouseEvent event)
{
if (dragJustStart)
{
dragJustStart = false;
if (!pointOccupy(endDragX, endDragY, false)) // drag a point to position without point
{
for (int i=0; i<ncount; i++)
{
if (pointSelected[i])
{
arrayPoints[i] = new Point(endDragX, endDragY);
}
}
}
for (int i=0; i<ncount; i++)
{
pointSelected[i] = false;
}
}
repaint();
}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
if (!dragJustStart && e.getModifiers()==MouseEvent.META_MASK && pointOccupy(e.getX(),e.getY(),true))
{
dragJustStart = true;
startDragX = e.getX();
startDragY = e.getY();
}
if (dragJustStart && e.getModifiers()==MouseEvent.META_MASK)
{
endDragX = e.getX();
endDragY = e.getY();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Index Count = "+ncount,10,20);
for(int i=0; i<ncount;i++){
g.drawRect(arrayPoints[i].x-3 ,arrayPoints[i].y-3,6,6);
g.drawString("Index = " +i+ " ["+arrayPoints[i].x+" , "+arrayPoints[i].y+"]" , arrayPoints[i].x, arrayPoints[i].y-5);
}
for(int i=0; i<ncount;i++){
for(int j=i+1;j<ncount;j++){ // j=i+1
//g.setColor(new Color((int)(Math.floor(Math.random()*0xffffff) )) );
//System.out.print(".");
//g.drawLine(0,0, 100,100);
g.drawLine(arrayPoints[i].x,arrayPoints[i].y, arrayPoints[j].x,arrayPoints[j].y);
}
}
}
public boolean pointOccupy(int tempX, int tempY, boolean toSelectPoint)
{
for (int i=0; i<ncount; i++)
{
if (Math.abs(tempX-arrayPoints[i].x)<=5 && Math.abs(tempY-arrayPoints[i].y)<=5)
{
if (toSelectPoint)
pointSelected[i] = true;
return true;
}
}
return false;
}
public static void main(String[] args) {
JFrame f = new JFrame("Task1e");
// Make the application exit when the window is closed.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.getContentPane( ).add(new Task1e());
f.setVisible(true);
}
}