Checking the Sides of a Diagonal Line in Java
Consider the moving body (circle) from the previous demonstration (lesson).
We could put a straight diagonal line in front of the ball (circle)
and perform a certain action (like changing the colour of
the ball) just after the ball has crossed the diagonal line
moving from left to right.
Code to Detect Crossing (Trespass) over Straight Line Region in Java
To check for when our ball crosses the straight line,
we will continually check the x position (mid-point)
of the ball against the x position gotten using the
equation of the straight line at the same y position
to as that of the ball.
If we designate the coordinates of the ball as
(xb, yb), and those of
the line at the same height as the ball as
(xl, yl),
then,
xl = myd + c
Ball (mid-point) has crossed line when
xd >= xl.
Create 2 new classes; File, New.
Call them PanelsStraightLineRegion and StraightLineRegion.
Type out the adjoining Java code for detecting the instance a travelling
body crosses the path of a straight line.
Canvas/Button class
package dymetric;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ButtonandCanvasPanels implements ActionListener {
public JPanel button_panel, canvas_panel;
public JButton motion_bttn;
public StraightLineRegion boundary;
public ButtonandCanvasPanels() {
button_panel = new JPanel();
button_panel.setBackground(Color.PINK);
button_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
motion_bttn = new JButton("Move");
motion_bttn.setBackground(new Color(255, 0, 255));
motion_bttn.addActionListener(this);
button_panel.add(motion_bttn);
canvas_panel = new JPanel();
canvas_panel.setLayout(new BorderLayout());
boundary = new StraightLineRegion();
canvas_panel.add(boundary, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt) {
boundary.checkBoundary();
}
}
Java code for StraightLineRegion class
package dymetric;
import java.awt.*;
public class StraightLineRegion extends Canvas {
protected Color ball_colour;
protected int x_ball = 50;
protected int y_ball = 100;
protected int previous_x = x_ball;
protected int previous_y = y_ball;
protected int x1 = 400;
protected int x2 = 600;
protected int y1 = 310;
protected int y2 = 10;
protected double x_line;
protected double m, c;
protected final int aWIDTH, aHEIGHT;
public StraightLineRegion() {
setBackground(Color.LIGHT_GRAY);
ball_colour = Color.YELLOW;
aWIDTH = aHEIGHT = 80;
m = (double) (y2 - y1) / (x2 - x1);
c = (double) (x2 * y1 - x1 * y2) / (x2 - x1);
x_line = (double) (y_ball - c) / m;
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(x1, y1, x2, y2);
g.setColor(Color.LIGHT_GRAY);
g.fillOval(previous_x, previous_y, aWIDTH, aHEIGHT);
g.setColor(ball_colour);
g.fillOval(x_ball, y_ball, aWIDTH, aHEIGHT);
previous_x = x_ball;
}
public void checkBoundary() {
while (x_ball <= 650) {
if (x_ball >= x_line) {
ball_colour = Color.GREEN;
}
paint(this.getGraphics());
x_ball += 10;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}