This commit is contained in:
ldy
2026-03-01 23:18:55 -05:00
commit 67f753a5d1
3087 changed files with 218259 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Question2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@@ -0,0 +1,159 @@
import java.awt.Color;
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius;
//constructor
public Bubble(int x, int y) {
super(x, y);
this.radius = 10.0;
}
@Override
public boolean isVisible(int w, int h) {
double x = getX();
double y = getY();
double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x));
double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y));
double dx = wx - x;
double dy = wy - y;
return dx * dx + dy * dy <= radius * radius;
}
@Override
public boolean isIn(int x, int y) {
double dx = x - getX();
double dy = y - getY();
return dx * dx + dy * dy <= radius * radius;
}
public void draw(Graphics g) {
super.draw(g); // Call the superclass's draw method
int x = (int)Math.round(getX() - radius);
int y = (int)Math.round(getY() - radius);
int diameter = (int)Math.round(radius * 2);
g.drawOval(x, y, diameter, diameter);
}
//test
public static void testBubble() {
Bubble b = new Bubble(20, 30);
System.out.println(b.getX() == 20);
System.out.println(b.getY() == 30);
b.setX(40);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 30);
b.setY(60);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 60);
// circle fully inside window
System.out.println(b.isVisible(100, 100) == true);
// circle center on window top edge
b.setX(50);
b.setY(0);
System.out.println(b.isVisible(100, 100) == true);
// circle center on window right edge
b.setX(99);
b.setY(50);
System.out.println(b.isVisible(100, 100) == true);
// circle center on window bottom edge
b.setX(50);
b.setY(99);
System.out.println(b.isVisible(100, 100) == true);
// circle center on window left edge
b.setX(0);
b.setY(50);
System.out.println(b.isVisible(100, 100) == true);
// circle center on window top left corner
b.setX(0);
b.setY(0);
System.out.println(b.isVisible(100, 100) == true);
// circle center on window top right corner
b.setX(99);
b.setY(0);
System.out.println(b.isVisible(100, 100) == true);
// circle center on window bottom right corner
b.setX(99);
b.setY(99);
System.out.println(b.isVisible(100, 100) == true);
// circle center on window bottom left corner
b.setX(0);
b.setY(99);
System.out.println(b.isVisible(100, 100) == true);
// circle bottom edge on window top edge
b.setX(50);
b.setY(-10);
System.out.println(b.isVisible(100, 100) == true);
// circle left edge on window right edge
b.setX(109);
b.setY(50);
System.out.println(b.isVisible(100, 100) == true);
// circle top edge on window bottom edge
b.setX(50);
b.setY(109);
System.out.println(b.isVisible(100, 100) == true);
// circle right edge on window left edge
b.setX(-10);
b.setY(50);
System.out.println(b.isVisible(100, 100) == true);
// circle bottom right edge on window top left corner
b.setX(-7);
b.setY(-7);
System.out.println(b.isVisible(100, 100) == true);
// circle bottom left edge on window top right corner
b.setX(106);
b.setY(-7);
System.out.println(b.isVisible(100, 100) == true);
// circle top left edge on window bottom right corner
b.setX(106);
b.setY(106);
System.out.println(b.isVisible(100, 100) == true);
// circle top right edge on window bottom left corner
b.setX(-7);
b.setY(106);
System.out.println(b.isVisible(100, 100) == true);
// circle bottom edge beyond window top edge
b.setX(50);
b.setY(-11);
System.out.println(b.isVisible(100, 100) == false);
// circle left edge beyond window right edge
b.setX(110);
b.setY(50);
System.out.println(b.isVisible(100, 100) == false);
// circle top edge beyond window bottom edge
b.setX(50);
b.setY(110);
System.out.println(b.isVisible(100, 100) == false);
// circle right edge beyond window left edge
b.setX(-11);
b.setY(50);
System.out.println(b.isVisible(100, 100) == false);
// circle bottom right edge beyond window top left corner
b.setX(-8);
b.setY(-8);
System.out.println(b.isVisible(100, 100) == false);
// circle bottom left edge beyond window top right corner
b.setX(107);
b.setY(-8);
System.out.println(b.isVisible(100, 100) == false);
// circle top left edge beyond window bottom right corner
b.setX(107);
b.setY(107);
System.out.println(b.isVisible(100, 100) == false);
// circle top right edge beyond window bottom left corner
b.setX(-8);
b.setY(107);
System.out.println(b.isVisible(100, 100) == false);
b.setX(0);
b.setY(0);
System.out.println(b.isIn(2, 2) == true);
System.out.println(b.isIn(10, 0) == true);
System.out.println(b.isIn(0, 10) == true);
System.out.println(b.isIn(7, 7) == true);
System.out.println(b.isIn(11, 0) == false);
System.out.println(b.isIn(0, 11) == false);
System.out.println(b.isIn(8, 8) == false);
}
}

View File

@@ -0,0 +1,11 @@
import java.awt.Graphics;
public interface IShape {
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
public void draw(Graphics g);
}

View File

@@ -0,0 +1,49 @@
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape implements IShape{
//instance variables
private int x;
private int y;
private Color color;
//constructor
public Shape(int x, int y) {
this.x = x;
this.y = y;
this.color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
}
@Override
public int getX() {
return this.x;
}
@Override
public int getY() {
return this.y;
}
@Override
public void setX(int x) {
this.x = x;
}
@Override
public void setY(int y) {
this.y = y;
}
@Override
public abstract boolean isVisible(int w, int h);
@Override
public abstract boolean isIn(int x, int y);
@Override
public void draw(Graphics g) {
g.setColor(this.color);
}
public static void testShape() {}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Bubble.testBubble();
}
}