Created
April 26, 2019 16:29
-
-
Save ahmadmysra/09a8aecf81dcbaeee4188714fade0ce7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 the.cockroach.manuever; | |
import java.awt.BasicStroke; | |
import java.awt.Graphics2D; | |
import java.awt.geom.Point2D; | |
import java.awt.geom.QuadCurve2D; | |
import java.awt.geom.Rectangle2D; | |
import javax.swing.JComponent; | |
import java.awt.geom.Ellipse2D; | |
import java.awt.geom.Line2D; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
/** | |
* | |
* @author beatris | |
*/ | |
public class TheCockroach extends JComponent { | |
//variables to keep track of passes for each tile and number of passed tiles: needs a list,an array? | |
public int totalTilesCount = 11, tilesPassesCount = 10; | |
//the coordinates of the points used for drawing | |
public double eclipseX = 40, eclipseY = 75; | |
public int rtEyeX = 63, rtEyeY = 83, ltEyeX = 53, ltEyeY = 83; | |
public Point2D p1 = new Point2D.Double(56, 16); | |
public Point2D p2 = new Point2D.Double(56, 75); | |
public Point2D p3 = new Point2D.Double(43, 80); | |
public Point2D p4 = new Point2D.Double(74, 80); | |
public Point2D p5 = new Point2D.Double(86, 55); | |
public Point2D p6 = new Point2D.Double(31, 55); | |
public Point2D p7 = new Point2D.Double(40, 75); | |
public Point2D p8 = new Point2D.Double(35, 25); | |
public Point2D p9 = new Point2D.Double(63, 95); | |
public Point2D p10 = new Point2D.Double(53, 95); | |
public Point2D p11 = new Point2D.Double(80, 125); | |
public Point2D p12 = new Point2D.Double(44, 124); | |
public Point2D p13 = new Point2D.Double(81, 68); | |
public Point2D p14 = new Point2D.Double(79, 36); | |
public Point2D p15 = new Point2D.Double(39, 75); | |
public Point2D p16 = new Point2D.Double(36, 31); | |
public Point2D p17 = new Point2D.Double(89, 82); | |
public Point2D p18 = new Point2D.Double(92, 63); | |
public Point2D p19 = new Point2D.Double(92, 44); | |
public Point2D p20 = new Point2D.Double(35, 91); | |
public Point2D p21 = new Point2D.Double(27, 65); | |
public Point2D p22 = new Point2D.Double(23, 39); | |
public Point2D p23 = new Point2D.Double(104, 52); | |
public Point2D p24 = new Point2D.Double(105, 36); | |
public Point2D p25 = new Point2D.Double(90, 16); | |
public Point2D p26 = new Point2D.Double(12, 56); | |
public Point2D p27 = new Point2D.Double(10, 36); | |
public Point2D p28 = new Point2D.Double(24, 16); | |
public Point2D p29 = new Point2D.Double(63, 83); | |
public Point2D p30 = new Point2D.Double(10, 10); | |
public Point2D p31 = new Point2D.Double(53, 83); | |
public Point2D p32 = new Point2D.Double(10, 10); | |
//array to facilitate the controlled changing of values | |
//List<Point2D> pointsArrayList = new ArrayList<Point2D>(); | |
//ArrayList<Point2D> pointsArrayList= new ArrayList<Point2D>(Arrays.asList(pA)); | |
public Point2D[] pA = {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32}; | |
//variables for creating controlled random coordinates | |
public double random, randX, randY; | |
//the moving method | |
//point 11 has max x coordinate | |
//point 24 has max y coordinate | |
// it is still moving out of the grid! WHY! | |
public void moveIt() { | |
//more ensuring to prevent coordinates more than 800 for x or 600 for y, YET it doesn't work | |
while (totalTilesCount >= tilesPassesCount && !(pA[10].getY() > 600) && !(pA[23].getX() > 800)) { | |
random = (int) (Math.random() * 801); | |
randX = random; | |
random = (int) (Math.random() * 601); | |
randY = random; | |
//substract when dimension is more than max window size, 800 for x and 600 for y | |
if (pA[10].getY() > 600 || pA[23].getX() > 800) { | |
//ensure that substraction doesn't yield negative | |
if (pA[26].getX() - randX >= 0 && pA[0].getY() - randY >= 0) { | |
for (Point2D coordinate : pA) { | |
coordinate.setLocation(coordinate.getX() - randX, coordinate.getY() - randY); | |
//repaint(); | |
} | |
tilesPassesCount++; | |
eclipseX = eclipseX - randX; | |
eclipseY = eclipseY - randY; | |
rtEyeX = rtEyeX - (int) randX; | |
rtEyeY = rtEyeY - (int) randY; | |
ltEyeX = ltEyeX - (int) randX; | |
ltEyeY = ltEyeY - (int) randY; | |
//when x coordinate is near the edge | |
} else if (pA[26].getX() - randX < 0 && pA[0].getY() - randY >= 0) { | |
for (Point2D coordinate : pA) { | |
coordinate.setLocation(coordinate.getX(), coordinate.getY() - randY); | |
//repaint(); | |
} | |
tilesPassesCount++; | |
//eclipseX = eclipseX; | |
eclipseY = eclipseY - randY; | |
//rtEyeX = rtEyeX; | |
rtEyeY = rtEyeY - (int) randY; | |
///ltEyeX = ltEyeX; | |
ltEyeY = ltEyeY - (int) randY; | |
//when y coordinate is near the edge | |
} else { | |
for (Point2D coordinate : pA) { | |
coordinate.setLocation(coordinate.getX() - randX, coordinate.getY()); | |
//repaint(); | |
} | |
tilesPassesCount++; | |
eclipseX = eclipseX - randX; | |
//eclipseY = eclipseY; | |
rtEyeX = rtEyeX - (int) randX; | |
//rtEyeY = rtEyeY; | |
ltEyeX = ltEyeX - (int) randX; | |
//ltEyeY = ltEyeY; | |
} | |
////add when dimension is less than max window size, 800 for x and 600 for y | |
} else { | |
if (pA[10].getY() + randY <= 600 || pA[23].getX() + randX <= 800) { | |
for (Point2D coordinate : pA) { | |
coordinate.setLocation(coordinate.getX() + randX, coordinate.getY() + randY); | |
//repaint(); | |
} | |
tilesPassesCount++; | |
eclipseX = eclipseX + randX; | |
eclipseY = eclipseY + randY; | |
rtEyeX = rtEyeX + (int) randX; | |
rtEyeY = rtEyeY + (int) randY; | |
ltEyeX = ltEyeX + (int) randX; | |
ltEyeY = ltEyeY + (int) randY; | |
} | |
} | |
} | |
} | |
@Override | |
public void paint(Graphics g) { | |
//moveIt(); | |
Graphics2D g2 = (Graphics2D) g; | |
//The grid | |
class GridDraw { | |
void gridDraw() { | |
int sqX = 0, sqY = 0, sqW = 50, sqH = 50; | |
for (int i = 0; i < 800; i += 50) { | |
for (int k = 0; k < 600; k += 50) { | |
g2.draw(new Rectangle2D.Double(sqX, sqY, sqW, sqH)); | |
sqY += 50; | |
totalTilesCount += 1; // supposed to be 800*600/50/50. was a bit more at first but disappeard probably after setting size dirctly not through new dimension | |
} | |
sqX += 50; | |
sqY = 0; | |
} | |
} | |
} | |
//drawing The cockroach | |
class CockroachDraw { | |
void cockroachDraw() { | |
g2.setStroke(new BasicStroke(2f)); | |
g2.setColor(Color.RED); | |
//the central line | |
g2.draw(new Line2D.Double(pA[0], pA[1])); | |
//the right legs | |
g2.draw(new Line2D.Double(pA[12], pA[16])); | |
g2.draw(new Line2D.Double(pA[16], pA[22])); | |
g2.draw(new Line2D.Double(pA[4], pA[17])); | |
g2.draw(new Line2D.Double(pA[17], pA[23])); | |
g2.draw(new Line2D.Double(pA[13], pA[18])); | |
g2.draw(new Line2D.Double(pA[18], pA[24])); | |
//the left legs | |
g2.draw(new Line2D.Double(pA[14], pA[19])); | |
g2.draw(new Line2D.Double(pA[19], pA[25])); | |
g2.draw(new Line2D.Double(pA[5], pA[20])); | |
g2.draw(new Line2D.Double(pA[20], pA[26])); | |
g2.draw(new Line2D.Double(pA[15], pA[21])); | |
g2.draw(new Line2D.Double(pA[21], pA[27])); | |
//the antennas | |
g2.draw(new Line2D.Double(pA[8], pA[10])); | |
g2.draw(new Line2D.Double(pA[9], pA[11])); | |
//the head | |
g2.draw(new Ellipse2D.Double(eclipseX, eclipseY, 35, 25)); | |
//the right side | |
QuadCurve2D qr = new QuadCurve2D.Double(); | |
qr.setCurve(p1, p5, p4); | |
g2.draw(qr); | |
//the left side | |
QuadCurve2D ql = new QuadCurve2D.Double(); | |
ql.setCurve(p1, p6, p3); | |
g2.draw(ql); | |
//the eyes | |
g.fillOval(rtEyeX, rtEyeY, 10, 10); | |
g.fillOval(ltEyeX, ltEyeY, 10, 10); | |
} | |
} | |
GridDraw grid = new GridDraw(); | |
grid.gridDraw(); | |
CockroachDraw cockroach = new CockroachDraw(); | |
cockroach.cockroachDraw(); | |
} | |
// collision detection method | |
/* public void collision() { | |
if (true) { | |
tilesPassesCount++; | |
changeTilesColor(); | |
moveIt(); | |
repaint(); | |
} | |
} */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 the.cockroach.manuever; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.Timer; | |
/** | |
* | |
* @author beatris | |
*/ | |
public class TheCockroachManuever extends JPanel implements ActionListener { | |
/** | |
* | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
TheCockroach hhhhh = new TheCockroach(); | |
//setting up the timer, but ALAS it only fires once? why? | |
Timer timer; | |
timer = new Timer(1000, new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
hhhhh.moveIt(); | |
hhhhh.repaint(); | |
} | |
}); | |
//how to add those buttons | |
JButton start = new JButton("Start"); | |
start.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
timer.start(); | |
} | |
}); | |
JButton reset = new JButton("Reset"); | |
reset.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
timer.restart(); | |
} | |
}); | |
JFrame frame = new JFrame("The Cockroach Manuever"); | |
//hhhhh.collision(); | |
frame.add(hhhhh); | |
//frame.add(start); // adding them like this makes them fill the entire window! | |
//frame.add(reset); | |
//manual starting of the timer | |
timer.start(); | |
//hhhhh.moveIt(); | |
//hhhhh.repaint(); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
//frame.getContentPane().add(new TheCockroach()); | |
//frame.pack(); //is it needed here? what does it do anyway? check later! | |
frame.setSize(800, 600); | |
//frame.setResizable(false); | |
frame.setVisible(true); | |
} | |
/*//what is wrong? inserted automatically by netbeans to resolve the error: | |
"TheCockroachManuever is not abstract and doesn't override abstract method actionPerformed(ActionEvent) in ActionListener" | |
*/ | |
@Override | |
public void actionPerformed(ActionEvent arg0) { | |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment