Notes
Notes - notes.io |
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication5;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
/**
*
* @author USER PC
*/
public class LabTest extends Applet implements ActionListener,MouseListener{
Button r_clock=new Button(),
r_aclock=new Button(),
clear=new Button(),
undo=new Button(),
animation=new Button();
ArrayList<Point> elp=new ArrayList<>(),
plot_pt=new ArrayList<>();
private static final int grid_diff=1,offset=35;
@Override
public void init()
{
setSize(800,600);
setBackground(Color.pink);
addMouseListener(this);
r_clock.setLabel("Rotate Clockwise");
r_clock.addActionListener(this);
r_aclock.setLabel("Rotate Anti-Clockwise");
r_aclock.addActionListener(this);
clear.setLabel("Clear");
clear.addActionListener(this);
undo.setLabel("Undo");
undo.addActionListener(this);
animation.setLabel("Animate");
animation.addActionListener(this);
add(r_aclock);
add(r_clock);
add(clear);
add(undo);
add(animation);
}
@Override
public void paint(Graphics g)
{
for(int i=0;i<elp.size();i+=3)
{
try
{
drawLine(g, elp.get(i), elp.get(i+1));
drawLine(g, elp.get(i), elp.get(i+2));
drawMidPointCircle(g, elp.get(i), elp.get(i+2));
drawMidpointEllipse(g, elp.get(i), elp.get(i+1), elp.get(i+2));
double ang=elp.get(i).getAngle(elp.get(i+1));
plot_pt=Transform(plot_pt, elp.get(i), ang);
for(Point p : plot_pt)
drawPoint(g, p.getX(), p.getY());
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.toString());
}
plot_pt.clear();
}
}
private ArrayList<Point> Transform(ArrayList<Point> pts,Point p,double deg)
{
ArrayList<Point> fpt=new ArrayList<>();
for(Point pm : pts)
{
int x=(int) (p.getX()+(pm.getX()-p.getX())*Math.cos(deg)+(pm.getY()-p.getY())*Math.sin(deg));
int y=(int) (p.getY()-(pm.getX()-p.getX())*Math.sin(deg)+(pm.getY()-p.getY())*Math.cos(deg));
fpt.add(new Point(x,y));
}
return fpt;
}
public void drawMidpointEllipse(Graphics g, Point p, Point p2, Point p3) {
int x=(int) p.getDistance(p2);
int y=(int) p.getDistance(p3);
g.setColor(Color.BLACK);
double xsq = x * x;
double ysq = y * y;
int xplot = 0, yplot = y;
double px = 0, py = 2 * xsq * yplot;
plotElipsePoints(g, p, new Point(xplot, yplot));
double pdes = ysq - (xsq * y) + 0.25 * xsq;
while (py > px) {
xplot += grid_diff;
px += 2 * ysq;
if (pdes < 0) {
pdes = pdes + ysq + px;
} else {
yplot -= grid_diff;
py -= 2 * xsq;
pdes = pdes + ysq + px - py;
}
plotElipsePoints(g, p, new Point(xplot, yplot));
}
pdes = ysq * (xplot + 0.5) * (xplot + 0.5) + xsq * (yplot - 1) * (yplot - 1) - xsq * ysq;
while (yplot > 0) {
yplot -= grid_diff;
py = py - 2 * xsq;
if (pdes > 0) {
pdes = pdes + xsq - py;
} else {
xplot += grid_diff;
px += 2 * ysq;
pdes = pdes + xsq - py + px;
}
plotElipsePoints(g, p, new Point(xplot, yplot));
}
}
public void plotElipsePoints(Graphics g, Point p1, Point p2) {
plot_pt.add(new Point( p1.getX() + p2.getX(), p1.getY() + p2.getY()));
plot_pt.add(new Point ( p1.getX() - p2.getX(), p1.getY() + p2.getY()));
plot_pt.add(new Point ( p1.getX() + p2.getX(), p1.getY() - p2.getY()));
plot_pt.add(new Point ( p1.getX() - p2.getX(), p1.getY() - p2.getY()));
}
public void drawMidPointCircle(Graphics g,Point c,Point xrad)
{
g.setColor(Color.black);
int x=0;
int rad=(int) c.getDistance(xrad);
int y=rad;
int p=1-rad;
drawCirclepoints(g,c.getX(),c.getY(),x,y);
g.setColor(Color.black);
while(x<y){
x+=grid_diff;
if(p<0){
p+=2*x+1;
}
else{
y-=grid_diff;
p+=2*(x-y)+1;
}
drawCirclepoints(g,c.getX(),c.getY(),x,y);
}
}
public void drawCirclepoints(Graphics g,int xcenter,int ycenter,int x,int y){
drawPoint(g,xcenter+x,ycenter+y);
drawPoint(g,xcenter-x,ycenter+y);
drawPoint(g,xcenter+x,ycenter-y);
drawPoint(g,xcenter-x,ycenter-y);
drawPoint(g,xcenter+y,ycenter+x);
drawPoint(g,xcenter-y,ycenter+x);
drawPoint(g,xcenter+y,ycenter-x);
drawPoint(g,xcenter-y,ycenter-x);
}
public void drawLine(Graphics g,Point p1,Point p2)
{
int sign;
g.setColor(Color.GRAY);
double dx=p1.getX()-p2.getX();
double dy=(p1.getY()-p2.getY());
double m = (dy/dx);
double xput=p1.getX(),yput=p1.getY();
int steps=0;
if(Math.abs(p1.getX()-p2.getX())>Math.abs(p1.getY()-p2.getY()))
{
steps=Math.abs(p1.getX()-p2.getX());
sign=(int) -(dx/Math.abs(dx));
}
else
{
steps=Math.abs(p1.getY()-p2.getY());
sign=(int) -(dy/Math.abs(dy));
}
if(Math.abs(m)<=1)
{
dx=sign*grid_diff;
dy= (m*dx);
}
else
{
dy=sign*grid_diff;
dx= (1.0/m*dy);
}
for(int i=0;i<=steps;i+=grid_diff)
{
xput+=dx;
yput+=dy;
drawPoint(g, (int)Math.round(xput), (int)Math.round(yput));
}
}
void drawPoint(Graphics g,int xpos,int ypos)
{
g.fillRect(((int)Math.round(xpos*1.0f)/grid_diff)*grid_diff, ((int)(ypos-offset)/grid_diff)*grid_diff+offset, grid_diff, grid_diff);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==r_aclock)
{
for(int i=0;i<elp.size();i+=3)
{
double deg=Math.toRadians(30);
int x=(int) (elp.get(i).getX()+(elp.get(i+1).getX()-elp.get(i).getX())*Math.cos(deg)+(elp.get(i+1).getY()-elp.get(i).getY())*Math.sin(deg));
int y=(int) (elp.get(i).getY()-(elp.get(i+1).getX()-elp.get(i).getX())*Math.sin(deg)+(elp.get(i+1).getY()-elp.get(i).getY())*Math.cos(deg));
Point npt1=new Point(x,y);
elp.set(i+1, npt1);
x=(int) (elp.get(i).getX()+(elp.get(i+2).getX()-elp.get(i).getX())*Math.cos(deg)+(elp.get(i+2).getY()-elp.get(i).getY())*Math.sin(deg));
y=(int) (elp.get(i).getY()-(elp.get(i+2).getX()-elp.get(i).getX())*Math.sin(deg)+(elp.get(i+2).getY()-elp.get(i).getY())*Math.cos(deg));
Point npt2=new Point(x,y);
elp.set(i+2, npt2);
}
repaint();
}
else if(ae.getSource()==r_clock)
{
for(int i=0;i<elp.size();i+=3)
{
double deg=-1*Math.toRadians(30);
int x=(int) (elp.get(i).getX()+(elp.get(i+2).getX()-elp.get(i).getX())*Math.cos(deg)+(elp.get(i+2).getY()-elp.get(i).getY())*Math.sin(deg));
int y=(int) (elp.get(i).getY()-(elp.get(i+2).getX()-elp.get(i).getX())*Math.sin(deg)+(elp.get(i+2).getY()-elp.get(i).getY())*Math.cos(deg));
Point npt1=new Point(x,y);
elp.set(i+2, npt1);
x=(int) (elp.get(i).getX()+(elp.get(i+1).getX()-elp.get(i).getX())*Math.cos(deg)+(elp.get(i+1).getY()-elp.get(i).getY())*Math.sin(deg));
y=(int) (elp.get(i).getY()-(elp.get(i+1).getX()-elp.get(i).getX())*Math.sin(deg)+(elp.get(i+1).getY()-elp.get(i).getY())*Math.cos(deg));
Point npt2=new Point(x,y);
elp.set(i+1, npt2);
}
repaint();
}
else if(ae.getSource()==clear)
{
elp.clear();
repaint();
}
else if(ae.getSource()==undo)
{
try
{
for(int i=0;i<3;i++)
elp.remove(elp.size()-1);
}
catch(ArrayIndexOutOfBoundsException e)
{
//e.printStackTrace();
System.out.println(e.toString());
}
repaint();
}
else if(ae.getSource()==animation)
{
}
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
elp.add(new Point(me.getX(),me.getY()));
repaint();
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team