I would like to draw 50 random dots within a given circle. The problem is the dots are not contained in the circle. Here is a runnable example:
package mygraphicsshapehomework;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class MyGraphicsShapeHomeWork extends JFrame {
public static void main(String[] args) {
new MyGraphicsShapeHomeWork();
}
public MyGraphicsShapeHomeWork() {
super("Title");
setBounds(600, 400, 700, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawOval(40, 40, 90, 90);
Color newColor = new Color(255, 0, 0);
g2.setColor(newColor);
for (int i = 0; i < 50; i++) {
int x = (int) Math.ceil(Math.random() * 10);
int y = (int) Math.ceil(Math.random() * 10);
g2.fillOval(i+x, i+y, 3, 3); // ???
}
}
}
Here is the result it produces:
How can I draw the dots within the circle only?
To get a random point in a circle with radius R find a random angle and a random radius:
double a = random() * 2 * PI;
double r = R * sqrt(random());
Then the coordinates of the point are:
double x = r * cos(a)
double y = r * sin(a)
Here are some notes about the drawing part. You should not paint directly on top level container such as JFrame. Instead, use JComponent or JPanel. Override paintComponent() for painting rather than paint() and don't forget to call super.paintComponent(g)
Take a look at Performing Custom Painting tutorial for more information.
Do not use setBounds(), override panel's getPreferredSize() and pack() the frame. Also, you rarely need to extend JFrame.
Here is a basic example that demonstrates drawing with a sub-pixel precision:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestDots extends JPanel{
public static final int POINTS_NUM = 1000;
public static final Color POINT_COLOR = Color.RED;
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
double padding = 10;
double radius = Math.min(this.getWidth(), this.getHeight()) / 2 - padding * 2;
g2.draw(new Ellipse2D.Double(padding, padding, radius * 2, radius * 2));
g2.setColor(POINT_COLOR);
for (int i = 0; i < POINTS_NUM; i++) {
double a = Math.random() * 2 * Math.PI;
double r = radius * Math.sqrt(Math.random());
double x = r * Math.cos(a) + radius + padding;
double y = r * Math.sin(a) + radius + padding;
g2.draw(new Ellipse2D.Double(x, y, 1, 1));
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("TestDots");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.add(new TestDots());
frame.pack();
frame.setVisible(true);
}
});
}
}
Here is a result:
For the position of the dots, generate random coordinates within the bounds of the outer circle. In order to generate these coordinates, the radius of the point from the center of the circle must be less than that of the outer circle. Get a random angle using
float a = Math.random() * Math.PI * 2;
Then, subtract a random value from the outer radius:
outerR - (Math.sqrt(Math.random()) * outerR)
and assign the positions to:
double x = Math.cos(a)*newR;
double y = Math.sin(a)*newR;
I'm sure there is a more mathematical approach to this, but this was the simplest in my opinion.
Related
I tried to create a code that printed n random circles in a 500 x 500 frame but it didn't work.
Can somebody tell me why this code isn't running?
When I run this code, it lets me enter the number of random circles I want but the frame always appear to be empty - no circles are drawn.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Scanner;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class RandomCircles extends JComponent
{
private int n;
public RandomCircles(int N)
{
n = N;
}
public void PaintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
double x = Math.random() * 500;
double y = Math.random() * 500;
double diameter = Math.random() * 500;
// Making sure the circle stays within the frame
for (int i = 0; i < n; i++)
{
while(x + diameter <= 500 || y + diameter <= 500)
{
Ellipse2D.Double circle
= new Ellipse2D.Double(x, y, diameter, diameter);
g2.draw(circle);
}
}
}
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter number of circles here: ");
int n = in.nextInt();
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setTitle("Random Circles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RandomCircles circle = new RandomCircles(n);
frame.add(circle);
// Add PaintComponent method somewhere here?
frame.setVisible(true);
}
}
I have a feeling that I need to add in the public void PaintComponent(Graphics g) somewhere to print it out, but I am not sure how.
The problem is on this line:
public void PaintComponent(Graphics g)
You are attempting to override the paintComponent(Graphics) method. You need to be careful that you get the name and the parameters right. Notice you spelled your method with an upper case P.
It is advisable that you add the annotation #Override on methods that are supposed to override a super class' method. That way you get a notification if you get the signature wrong.
So your method should look like this:
#Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
...
}
== Edit after comments ==
The while loop is also causing an issue. Try doing this instead.
for (int i = 0; i < n; i++)
{
double x = Math.random() * 500;
double y = Math.random() * 500;
double diameter = Math.random() * 500;
Ellipse2D.Double circle
= new Ellipse2D.Double(x, y, diameter, diameter);
g2.draw(circle);
}
Notice that I moved the random number generation inside the loop. This does not guarantee the circle fitting in the frame but that is something you can modify later.
A and C images are far away.
At this time, B image is created from the coordinate values of A
For each iteration, x, y must be moved to arrive at the C image coordinate value.
For example,
A (100,100), C (300,300)
Starting at B (100,100),
Each time it is repeated, x, y must be moved to reach
B (300,300).
This is the method for entering the current moving source.
Public void Attack () {
int x1 = a.getX ();
int y1 = a.getY ();
int x2 = c.getX ();
int y2 = c.getY ();
if(b.getX ()==c.getX&&b.getY () == c.getY())'
{
system.out.println ("ok");'
}else {
b.setbounds (help1,help2,100,50)
}
}
Here, I want to know the code to enter help1 help2.
Pythagorean formula
Between A and C images
I want to know how to make the B image move along a virtual straight line.
Like a tower defense game.
A image is a tower
B image is bullet
The C image is the enemy.
I want the bullets fired from the tower to move to enemy locations.
I am Korean.
I used a translator
The following code is an mre of using a straight line equation y = mx + c to draw a moving object along such line.
To test the code copy the entire code into MoveAlongStraightLine.java and run, or run it online:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class MoveAlongStraightLine extends JFrame {
public MoveAlongStraightLine(){
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ShootinBoard shootingBoard = new ShootinBoard();
JButton fire = new JButton("Fire");
fire.addActionListener(e->shootingBoard.fire());
add(shootingBoard);
add(fire, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[]args){
SwingUtilities.invokeLater(()->new MoveAlongStraightLine());
}
}
class ShootinBoard extends JPanel{
//use constants for better readability
private static final double W = 600, H = 400;
private static final int DOT_SIZE = 10, SPEED = 2;
private final int dX = 1; //x increment
private final Timer timer;
private final Point2D.Double shooter, target;
private Point2D.Double bullet;
public ShootinBoard() {
setPreferredSize(new Dimension((int)W, (int)H));
shooter = new Point2D.Double(50,350);
bullet = shooter; //place bullet at start point
target = new Point2D.Double(550,50);
timer = new Timer(SPEED, e->moveBullet());
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(3));
//draw source
g2.setColor(Color.blue);
g2.draw(new Ellipse2D.Double(shooter.getX(), shooter.getY(),DOT_SIZE , DOT_SIZE));
//draw bullet
g2.setColor(Color.black);
g2.draw(new Ellipse2D.Double(bullet.getX(), bullet.getY(),DOT_SIZE , DOT_SIZE));
//draw target
g2.setColor(Color.red);
g2.draw(new Ellipse2D.Double(target.getX(), target.getY(),DOT_SIZE , DOT_SIZE));
}
void fire(){
timer.stop();
bullet = shooter; //place bullet at start point
timer.start();
}
void moveBullet() {
if(target.x == bullet.x && target.y == bullet.y) {
timer.stop();
}
//y = mx + c for more details see https://www.usingmaths.com/senior_secondary/java/straightline.php
double m = (target.y - bullet.y)/ (target.x - bullet.x);//slope
double c = (target.x * bullet.y - bullet.x * target.y)/(target.x - bullet.x);
double newBulletX = bullet.x+dX; //increment x
double newBulletY = m * newBulletX + c; //calculate new y
bullet = new Point2D.Double(newBulletX,newBulletY);
repaint();
}
}
For the purposes of my project, I'm trying to simulate a phyllotaxis pattern by creating multiple circles in real time using the formulas given.
So recently, I've decided to try out GUI programming in Java using JFrame and swing, and I've hit a wall trying to figure out how to get everything running properly. My idea was to slowly print out circle after circle with their x and y coordinates being calculated from the "r = cos/sin(theta)" formulas documented in the phyllotaxis instructions. Unfortunately, while the x and y values are constantly changing, only one circle is printed. Is there something I am missing?
package gExample;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class GraphicsExample extends Canvas implements ActionListener {
private final static int HEIGHT = 600;
private final static int WIDTH = 600;
private int n = 0;
private int x, y;
Timer t = new Timer(20, this);
public static void main(String args[]) {
JFrame frame = new JFrame();
GraphicsExample canvas = new GraphicsExample();
canvas.setSize(WIDTH, HEIGHT);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.black);
}
public void paint(Graphics g){
Random rand = new Random();
Color col = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g.setColor(col);
/*each time paint() is called, I expect a new circle to be printed in the
x and y position that was updated by actionPerformed(), but only one inital circle is created. */
g.fillOval(x, y, 8, 8);
t.start();
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int c = 9;
double r = c * Math.sqrt(n);
double angle = n * 137.5;
//here, every time the method is called, the x and y values are updated,
//which will be used to fill in a new circle
int x = (int) (r * Math.cos(angle * (Math.PI / 180) )) + (WIDTH / 2);
int y = (int) (r * Math.sin(angle * (Math.PI / 180) )) + (HEIGHT / 2);
//when the program is running, this line of code is executed multiple times.
System.out.println("x: " + x + " y: " + y);
n++;
}
}
I'm currently working on a program which enables user to draw various geometric shapes. However, I got some issues on calculating and placing the angle objects onto my Canvas panel accurately. The angle object is basically an extension of the Arc2D object, which provides a additional method called computeStartAndExtent(). Inside my Angle class, this method computes and finds the necessary starting and extension angle values:
private void computeStartAndExtent()
{
double ang1 = Math.toDegrees(Math.atan2(b1.getY2() - b1.getY1(), b1.getX2() - b1.getX1()));
double ang2 = Math.toDegrees(Math.atan2(b2.getY2() - b2.getY1(), b2.getX2() - b2.getX1()));
if(ang2 < ang1)
{
start = Math.abs(180 - ang2);
extent = ang1 - ang2;
}
else
{
start = Math.abs(180 - ang1);
extent = ang2 - ang1;
}
start -= extent;
}
It is a bit buggy code that only works when I connect two lines to each other, however, when I connect a third one to make a triangle, the result is like the following,
As you see the ADB angle is the only one that is placed correctly. I couldn't figure how to overcome this. If you need some additional info/code please let me know.
EDIT: b1 and b2 are Line2D objects in computeStartAndExtent() method.
Thank you.
There are some of things that can be made to simplify the calculation:
Keep the vertices ordered, so that it is always clear how to calculate the vertex angles pointing away from the corner
Furthermore, always draw the polygon to the same direction; then you can always draw the angles to the same direction. The example below assumes the polygon is drawn clockwise. The same angle calculation would result in the arcs drawn outside given a polygon drawn counterclockwise.
Example code; is not quite the same as yours as I don't have your code, but has similar functionality:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Polygon extends JPanel {
private static final int RADIUS = 20;
private final int[] xpoints = {
10, 150, 80, 60
};
private final int[] ypoints = {
10, 10, 150, 60
};
final Arc2D[] arcs;
Polygon() {
arcs = new Arc2D[xpoints.length];
for (int i = 0; i < arcs.length; i++) {
// Indices of previous and next corners
int prev = (i + arcs.length - 1) % arcs.length;
int next = (i + arcs.length + 1) % arcs.length;
// angles of sides, pointing outwards from the corner
double ang1 = Math.toDegrees(Math.atan2(-(ypoints[prev] - ypoints[i]), xpoints[prev] - xpoints[i]));
double ang2 = Math.toDegrees(Math.atan2(-(ypoints[next] - ypoints[i]), xpoints[next] - xpoints[i]));
int start = (int) ang1;
int extent = (int) (ang2 - ang1);
// always draw to positive direction, limit the angle <= 360
extent = (extent + 360) % 360;
arcs[i] = new Arc2D.Float(xpoints[i] - RADIUS, ypoints[i] - RADIUS, 2 * RADIUS, 2 * RADIUS, start, extent, Arc2D.OPEN);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(160, 160);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawPolygon(xpoints, ypoints, xpoints.length);
Graphics2D g2d = (Graphics2D) g;
for (Shape s : arcs) {
g2d.draw(s);
}
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Polygon());
frame.pack();
frame.setVisible(true);
}
});
}
}
Results in:
I'm really stuck on how to go about programming this. How to draw a circle in Java with a radius and points around the edge?
I need to draw a circle within a JFrame with a radius and points around the circumference. i can mathematically calculate how to find the coordinates of the point around the edge but i cant seem to be able to program the circle. I am currently using a Ellipse2D method but that doesn't seem to work and doesn't return a radius, as under my understanding, it doesn't draw the circle from the center rather from a starting coordinate using a height and width.
My current code is on a separate frame but I need to add it to my existing frame.
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class circle extends JFrame {
public circle() {
super("circle");
setSize(410, 435);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel sp = new Panel();
Container content = getContentPane();
content.add(sp);
setContentPane(content);
setVisible(true);
}
public static void main (String args[]){
circle sign = new circle();
}
}
class Panel extends JPanel {
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;
comp2D.setColor(Color.red);
Ellipse2D.Float sign1 = new Ellipse2D.Float(0F, 0F, 350F, 350F);
comp2D.fill(sign1);
}
}
Points on a circle may be specified as a function of the angle θ:
x = a + r cos(θ)
y = b + r sin(θ)
Here, increments of 2π/8 are shown.
Addendum: As suggested in a comment by #Christoffer Hammarström, this revised example reduces the number of magic numbers in the original. The desired number of points becomes a parameter to the constructor. It also adapts the rendering to the container's size.
/** #see https://stackoverflow.com/questions/2508704 */
public class CircleTest extends JPanel {
private static final int SIZE = 256;
private int a = SIZE / 2;
private int b = a;
private int r = 4 * SIZE / 5;
private int n;
/** #param n the desired number of circles. */
public CircleTest(int n) {
super(true);
this.setPreferredSize(new Dimension(SIZE, SIZE));
this.n = n;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
a = getWidth() / 2;
b = getHeight() / 2;
int m = Math.min(a, b);
r = 4 * m / 5;
int r2 = Math.abs(m - r) / 2;
g2d.drawOval(a - r, b - r, 2 * r, 2 * r);
g2d.setColor(Color.blue);
for (int i = 0; i < n; i++) {
double t = 2 * Math.PI * i / n;
int x = (int) Math.round(a + r * Math.cos(t));
int y = (int) Math.round(b + r * Math.sin(t));
g2d.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);
}
}
private static void create() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new CircleTest(9));
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
create();
}
});
}
}
Try something like this:
public class CirclePanel extends JPanel
{
public static void main(String[] args) throws Exception
{
JFrame f = new JFrame();
f.setContentPane(new CirclePanel());
f.setSize(700,500);
f.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
//Draws the line
g.drawOval(0,0,this.getWidth(), this.getHeight());
//draws filled circle
g.setColor(Color.red);
g.fillOval(0,0,this.getWidth(), this.getHeight());
}
}
You can also override the paint method in the frame class, but then the you would have to calculate in the size of the window decorations and it gets dirty there...
I recommend to take some time to review the "midpoint circle algorithm or Bresenham's circle algorithm". The accepted solution is based on very costly math operations like float multiplication and trigonometric functions.