Java - Objects follow each other, when given diffrent tasks - java

I have been working on this java application.
So far it has no meaning, just a randomly colored ball bouncing around.
But now, when i wanted to add another ball to the bouncing app, the balls followed each other.
This is my code so far.
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JPanel implements Runnable {
Color color = Color.red;
int dia = 60;
Diameter of the objects.
long delay = 20;
Delay time.
private int x = (int)Math.floor(Math.random() * 580);
private int y = (int)Math.floor(Math.random() * 900);
private int xx = (int)Math.floor(Math.random() * 580);
private int yy = (int)Math.floor(Math.random() * 900);
Above is the objects position.
private int dx = (int)Math.floor(Math.random() * 7);
private int dy = (int)Math.floor(Math.random() * 7);
private int dxx = (int)Math.floor(Math.random() * 7);
private int dyy = (int)Math.floor(Math.random() * 7);
Above is object speed.
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.fillOval(x,y,60,60);
g.setColor(color);
g.fillOval(xx,yy,60,60);
}
The graphics.
And below is just calculations, thread.sleep and the JFrame.
public void run() {
while(isVisible()) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
move();
repaint();
}
}
public void move() {
if(x + dx < 0 || x + dia + dx > getWidth()) {
dx *= -1;
color = getColor();
}
if(y + dy < 0 || y + dia + dy > getHeight()) {
dy *= -1;
color = getColor();
}
if(xx + dxx < 0 || xx + dia + dxx > getWidth()) {
dxx *= -1;
color = getColor();
}
if(yy + dyy < 0 || yy + dia + dyy > getHeight()) {
dyy *= -1;
color = getColor();
}
x += dx;
y += dy;
xx += dx;
yy += dy;
}
private Color getColor() {
int rval = (int)Math.floor(Math.random() * 256);
int gval = (int)Math.floor(Math.random() * 256);
int bval = (int)Math.floor(Math.random() * 256);
return new Color(rval, gval, bval);
}
private void start() {
while(!isVisible()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
System.exit(1);
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
public static void main(String[] args) {
MainFrame test = new MainFrame();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(640, 960);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation(dim.width/2-f.getSize().width/2, dim.height/2-f.getSize().height/2);
f.setVisible(true);
test.start();
}
}
I just can not figure it out.
I know the answer is going to be simple.

You should define a single class Ball and create two instances of it rather than repeating the variables and have an x,y co-ordinate and velocity dx and dy inside that class.
The two follow each other ebcause you add the same velocity to both balls all the time:
x += dx;
y += dy;
xx += dx;
yy += dy;

Related

Java - paint() called on Windows 10 but not on OSX 10.10.5

So I wrote a program to display Julia sets on my Windows machine, but the paint() method isn't being called when I transferred the exact same code to my MacBook.
The whole class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class JuliaSet extends JPanel implements MouseWheelListener {
public double width;
public double height;
public double pixToCoord;
public Complex c;
public double iterations = 100;
double xMin = -2;
double xMax = 2;
double centerY = 0;
public JuliaSet(double width, double height) {
this.width = width;
this.height = height + 31;
pixToCoord = (xMax - xMin) / width;
c = new Complex(0.285, 0.01);
}
public void setC(double x, double y) {
c.x = x;
c.y = y;
}
public void paint(Graphics g) {
pixToCoord = (xMax - xMin) / width;
double yMin = centerY - (height * pixToCoord / 2.0);
Graphics2D g2 = (Graphics2D) g;
for (double y = 0; y <= height - 31; y++) {
for (double x = 1; x <= width; x++) {
Complex z = new Complex(x * pixToCoord + xMin, (-y + height) * pixToCoord + yMin);
double count = 0;
for (count = 0; count < iterations && z.magnitude() < 2; count++) {
z = z.multiply(z).add(c);
}
g2.setColor(Color.getHSBColor((float) (count / iterations), (float) 1.0, (float) (1.0 - (count / iterations))));
g2.drawLine((int) x, (int) y - 31, (int) x, (int) y - 31);
}
}
}
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
double x = (double) e.getX();
double y = (double) e.getY() - 31;
x = x * pixToCoord + xMin;
double yMin = centerY - (height * pixToCoord / 2.0);
double yMax = centerY + (height * pixToCoord / 2.0);
y = (-y + height) * pixToCoord + yMin;
if (e.getWheelRotation() < 0) {
xMin += 0.25 * (x - xMin);
xMax -= 0.25 * (xMax - x);
yMin += 0.25 * (y - yMin);
yMax -= 0.25 * (yMax - y);
} else {
xMin -= (x - xMin) / 3.0;
xMax += (xMax - x) / 3.0;
yMin -= (y - yMin) / 3.0;
yMax += (yMax - y) / 3.0;
}
centerY = (yMax + yMin) / 2.0;
this.repaint();
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame();
frame.setTitle("Julia Set");
frame.setBounds(0, 0, 600, 631);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JuliaSet p = new JuliaSet((double) frame.getWidth(), (double) frame.getHeight());
frame.add(p);
//frame.addMouseWheelListener(p);
double angle = 0;
long time = System.currentTimeMillis();
while (true) {
{Thread.sleep(16);}
angle += Math.PI / 960.0;
if (angle >= Math.PI * 2.0) angle = 0;
p.setC(0.775 * Math.cos(angle), 0.775 * Math.sin(angle));
p.repaint();
System.out.println((int) (1000.0 / (double) (System.currentTimeMillis() - time)));
time = System.currentTimeMillis();
}
}
}
Is there any reason that this code would work on Windows and not OSX?
Your code ignores Swing threading rules with use of while (true) and Thread.sleep as well as Swing component mutational changes within possible background threads, so the question you should be asking is why it worked at all on any system.
Suggestions:
Use a SwingWorker to create your background thread and to allow you to make mutational changes on Swing components on the event thread.
Draw within paintComponent, not paint, and call the super's painting method within your override, here super.paintComponent(g) if you correctly override this method.
Do complex math or any CPU- or time-intensive processing within the background thread, and avoid doing it within the painting method. These methods should be for painting and painting only.
For example, here's a sample program that uses a SwingWorker and graphics to calculate and draw sections of the Mandelbrot set (sorry, don't have a Julia Set implementation yet):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
#SuppressWarnings("serial")
public class Mandel2 extends JPanel {
private static final int GUI_HEIGHT = 600;
private static final int GUI_WIDTH = 600;
private static final int MAX_ITERS = 50000;
private BufferedImage image = new BufferedImage(GUI_WIDTH, GUI_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
private Rectangle zoomRect;
private double myX0 = -2.5;
private double myY0 = -2.0;
private double myX1 = 1.5;
private double myY1 = 2.0;
private JDialog waitDialog;
public Mandel2() {
final MyMouse myMouse = new MyMouse();
int delayStartingCalc = 2 * 1000; // 2 second delay
Timer timer = new Timer(delayStartingCalc, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
Rectangle myRect = new Rectangle(0, 0, GUI_WIDTH, GUI_HEIGHT);
createMandel(myRect);
}
});
timer.setRepeats(false);
timer.start();
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(GUI_WIDTH, GUI_HEIGHT);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, this);
}
Graphics2D g2 = (Graphics2D) g;
if (zoomRect == null) {
return;
}
g2.setXORMode(Color.gray);
g2.draw(zoomRect);
}
private double screenToLogicalX(double screenX) {
return myX0 + (screenX * (myX1 - myX0)) / GUI_WIDTH;
}
private double screenToLogicalY(double screenY) {
return myY0 + ((GUI_HEIGHT - screenY) * (myY1 - myY0)) / GUI_HEIGHT;
}
private void createMandel(Rectangle myRect) {
double x0 = screenToLogicalX(myRect.x);
double y0 = screenToLogicalY(myRect.y + myRect.height);
double x1 = screenToLogicalX(myRect.x + myRect.width);
double y1 = screenToLogicalY(myRect.y);
myX0 = x0;
myY0 = y0;
myX1 = x1;
myY1 = y1;
MandelWorker mandelWorker = new MandelWorker(MAX_ITERS, x0, y0, x1, y1);
mandelWorker.addPropertyChangeListener(new MandelWorkerListener());
mandelWorker.execute();
if (waitDialog == null) {
Window win = SwingUtilities.getWindowAncestor(Mandel2.this);
JProgressBar jProgressBar = new JProgressBar();
jProgressBar.setIndeterminate(true);
waitDialog = new JDialog(win, "Please Wait", ModalityType.APPLICATION_MODAL);
waitDialog.add(jProgressBar);
waitDialog.pack();
waitDialog.setLocationRelativeTo(win);
}
waitDialog.setVisible(true);
}
private class MyMouse extends MouseAdapter {
private Point p;
#Override
public void mousePressed(MouseEvent e) {
p = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
zoomRect = createRect(e);
repaint();
};
#Override
public void mouseReleased(MouseEvent e) {
zoomRect = createRect(e);
repaint();
createMandel(zoomRect);
}
private Rectangle createRect(MouseEvent e) {
int x = Math.min(p.x, e.getX());
int y = Math.min(p.y, e.getY());
int width = Math.abs(p.x - e.getX());
int height = Math.abs(p.y - e.getY());
return new Rectangle(x, y, width, height);
}
}
private class MandelWorkerListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
waitDialog.setVisible(false);
waitDialog.dispose();
MandelWorker worker = (MandelWorker) evt.getSource();
try {
image = worker.get();
zoomRect = null;
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
private class MandelWorker extends SwingWorker<BufferedImage, Void> {
private int maxIters;
private double x1;
private double y1;
private double x2;
private double y2;
public MandelWorker(int maxIters, double x1, double y1, double x2, double y2) {
this.maxIters = maxIters;
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
#Override
protected BufferedImage doInBackground() throws Exception {
int[][] iterGrid = new int[GUI_HEIGHT][GUI_WIDTH];
for (int i = 0; i < GUI_HEIGHT; i++) {
double y = y1 + i * (y2 - y1) / GUI_HEIGHT;
for (int j = 0; j < GUI_WIDTH; j++) {
double x = x1 + j * (x2 - x1) / GUI_WIDTH;
int iIndex = GUI_HEIGHT - i - 1;
iterGrid[iIndex][j] = calcMandel(x, y);
}
}
return render(iterGrid);
}
private BufferedImage render(int[][] iterGrid) {
int w = GUI_WIDTH;
int h = GUI_HEIGHT;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
if (iterGrid[i][j] < maxIters) {
String hexCode = String.format("#%06x", (0xFFFFFF & (32 * iterGrid[i][j])));
g2.setColor(Color.decode(hexCode));
} else {
g2.setColor(Color.CYAN);
}
g2.drawLine(j, i, j, i);
}
}
g2.dispose();
return img;
}
private int calcMandel(double x, double y) {
Complex c = new Complex(x, y);
Complex z = new Complex();
int iters = 0;
while (z.getMagnitude() < 2 && iters <= maxIters) {
z = z.multiply(z).add(c);
iters++;
}
return iters;
}
}
private class Complex {
private double real, imag;
// Constructors
public Complex() {
real = 0.0;
imag = 0.0;
}
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
// add given complex number to this one, returning the Complex result
public Complex add(Complex other) {
return new Complex(this.real + other.real, this.imag + other.imag);
}
// multiply given complex number by this one, returning the Complex
// result
public Complex multiply(Complex other) {
return new Complex((this.real * other.real) - (this.imag * other.imag),
(this.imag * other.real) + (this.real * other.imag));
}
// get the magnitude of this complex number
public double getMagnitude() {
return Math.sqrt((real * real) + (imag * imag));
}
}
private static void createAndShowGui() {
Mandel2 mainPanel = new Mandel2();
JFrame frame = new JFrame("Mandel2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setResizable(false);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Asteroids Ball Angle Calculation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Right now I am working on making the classic game Asteroids. I have gotten the ship to move 360 degrees using the arrow keys. However, when I try to shoot the balls out of the ship at that angle, they only leave at 45 degree increments. I think that means there is something wrong with my math in the ball class:
public Ball(double ballAngle){
angle = ballAngle;
xRatio = Math.cos((angle + 90) * 3.14 / 180);
yRatio = Math.sin((angle + 90) * 3.14 / 180);
xChange = xRatio * speed;
yChange = yRatio * speed;
}
public void update(){
x = (int) Math.round(x + xChange);
y = (int) Math.round(y + yChange);
}
However, I can't seem to find what is going on. I have tried debugging it by printing the values an following them step by step, but still can't solve it. Here is the rest of my code incase you need to see it.
Main Game Class:
package Asteroids;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.ArrayList;
#SuppressWarnings("serial")
public class Asteroids extends JPanel implements KeyListener{
public final static int BOX_WIDTH = 600;
public final static int BOX_HEIGHT = 600;
public final static int UPDATE_RATE = 300;
public final static double shipSpeed = 1;
ArrayList<Ball> balls = new ArrayList<Ball>();
int i = 0;
int ballCount = 0;
boolean spawn = false;
Ship ship = new Ship(BOX_WIDTH / 2,BOX_HEIGHT / 2);
public Asteroids(){
//Set window size
setPreferredSize(new Dimension(BOX_WIDTH,BOX_HEIGHT));
//Start game thread
Thread gameThread = new Thread() {
public void run(){
while(true){
ship.update();
for (int j=0; j<ballCount; j++){
balls.get(j).update();
}
if(spawn){
i++;
}
if(i % 100 == 0){
balls.add(new Ball(ship.getAngle()));
ballCount ++;
}
repaint();
try {Thread.sleep(1000 / UPDATE_RATE);}
catch (InterruptedException ex) {}
}
}
};
gameThread.start();
}
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
for (int j=0; j<ballCount; j++){
balls.get(j).draw(g);
}
ship.draw(g);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT){
ship.setSpeed(-shipSpeed);
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
ship.setSpeed(shipSpeed);
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){
spawn = true;
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT){
ship.setSpeed(0);
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
ship.setSpeed(0);
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){
spawn = false;
}
}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Create Frame
JFrame frame = new JFrame("ASTEROIDS");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Asteroids asteroids = new Asteroids();
frame.setContentPane(asteroids);
frame.setSize(BOX_WIDTH,BOX_HEIGHT);
frame.pack();
frame.addKeyListener(asteroids);
frame.setVisible(true);
}
});
}
public int getBoxHeight(){
return BOX_HEIGHT;
}
public int getBoxWidth(){
return BOX_WIDTH;
}
}
Ship Class:
package Asteroids;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Ship {
Random rng = new Random();
int r = rng.nextInt(256);
int g = rng.nextInt(256);
int b = rng.nextInt(256);
Color color = new Color(r,g,b);
double speed = 0;
double angle = 0;
final static int shipLength = 20;
final static int shipAngle = 40;
static int x;
static int y;
int point1x;
int point2x;
int point3x;
int point1y;
int point2y;
int point3y;
public Ship(int xPos, int yPos){
x = xPos;
y = yPos;
point1x = x;
point2x = (int) Math.round( x - (Math.tan(shipAngle * 6.28 / 360) * shipLength));
point3x = (int) Math.round( x + (Math.tan(shipAngle * 6.28 / 360) * shipLength));
point1y = y - shipLength;
point2y = y + shipLength;
point3y = y + shipLength;
}
public void update(){
angle = angle + speed;
if(angle < 0){
angle = angle + 360;
}
else if(angle >= 360){
angle = angle - 360;
}
}
public void draw(Graphics g) {
g.setColor(color);
point1x = x - (int) Math.round( Math.sin(angle * 6.28 / 360) * shipLength);
point2x = x + (int) Math.round( Math.sin((angle + shipAngle) * 6.28 / 360) * shipLength);
point3x = x + (int) Math.round( Math.sin((angle - shipAngle) * 6.28 / 360) * shipLength);
point1y = y + (int) Math.round( Math.cos(angle * 6.28 / 360) * shipLength);
point2y = y + (int) Math.round( Math.cos((angle + 180 + shipAngle) * 6.28 / 360) * shipLength);
point3y = y + (int) Math.round( Math.cos((angle - 180 - shipAngle) * 6.28 / 360) * shipLength);
int xpoints[] = {point1x, point2x, point3x};
int ypoints[] = {point1y, point2y, point3y};
int npoints = 3;
g.fillPolygon(xpoints, ypoints, npoints);
}
public void setSpeed(double s){
speed = s;
}
public static int getX(){
return x;
}
public static int getY(){
return y;
}
public double getAngle(){
return angle;
}
}
Ball Class:
package Asteroids;
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
int x = Ship.getX();
int y = Ship.getY();
double angle;
double xRatio;
double yRatio;
double xChange;
double yChange;
final static int speed = 1;
final static int diameter = 5;
public Ball(double ballAngle){
angle = ballAngle;
xRatio = Math.cos((angle + 90) * 3.14 / 180);
yRatio = Math.sin((angle + 90) * 3.14 / 180);
xChange = xRatio * speed;
yChange = yRatio * speed;
}
public void update(){
x = (int) Math.round(x + xChange);
y = (int) Math.round(y + yChange);
}
public void draw(Graphics g) {
g.setColor(Color.white);
g.fillOval(x - diameter/2, y - diameter/2, diameter, diameter);
}
}
I would suspect its a rounding error. You're storing x and y as int primitives, but then adding doubles to it, storing it back in the int. If you changed x and y to be doubles, and then round them only before you display the location, you shouldn't have this problem

Timer is not starting in JPanel

I am trying to move an image in JPanel and the static variable x and y are the coordinates of red color changed in an other class using opencv. Here the timer is not starting. Please tell me where I am mistaken.
class ImageFollowingPanel extends JPanel
{
private final BufferedImage image;
private Point imagePosition = new Point(150, 150);
private Point redPoint;
private double imageAngleRad = 0;
public static int x, y;
public ImageFollowingPanel()
{
BufferedImage i = null;
try
{
i = ImageIO.read(new File("forward.png"));
}
catch (IOException e)
{
e.printStackTrace();
}
image = i;
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if (redPoint != null)
{
int centerX = imagePosition.x + (image.getWidth() / 2);
int centerY = imagePosition.y + (image.getHeight() / 2);
if (redPoint.x != centerX)
{
imagePosition.x += redPoint.x < centerX ? -1 : 1;
}
if (redPoint.y != centerY)
{
imagePosition.y += redPoint.y < centerY ? -1 : 1;
}
System.out.println("mouse::: x : " + x + "y :" + y);
redPoint.x = x;
redPoint.y = y;
double dx = x - imagePosition.getX();
double dy = y - imagePosition.getY();
imageAngleRad = Math.atan2(dy, dx);
repaint();
}
}
};
Timer timer = new Timer(1000, taskPerformer);
timer.start();
}
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
int cx = image.getWidth() / 2;
int cy = image.getHeight() / 2;
AffineTransform oldAT = g.getTransform();
g.translate(cx + imagePosition.x, cy + imagePosition.y);
g.rotate(imageAngleRad);
g.translate(-cx, -cy);
g.drawImage(image, 0, 0, null);
g.setTransform(oldAT);
}
}
Everytime the timer fires the action event, you check if redPoint is not null, if it's null you just do nothing. But in your code redPoint is always null, that's why nothing happens.

Midpoint Ellipse Algorithm in Java

I am trying to write a program to allow a user to draw an ellipse by using clicks. The user left-clicks at first to select the radius, then right-clicks to select the horizontal radius, then right-clicks again to select the vertical radius. Nothing is drawn after clicking. I don't understand where the error is.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ellipse extends JPanel implements MouseListener{
Graphics P;
public Ellipse()
{
addMouseListener(this);
}
static int Radius = 0;
int CenterX, CenterY, RadiusX, RadiusY;
public void paintComponent(Graphics g)
{
P=g;
EllipseMidpoint(CenterX, CenterY, RadiusX, RadiusY);
}
public void EllipseMidpoint(int Cx, int Cy, int Rx, int Ry)
{
int Rx2 = Rx * Rx;
int Ry2 = Ry * Ry;
int twoRx2 = 2 * Rx2;
int twoRy2 = 2 * Ry2;
int x = 0;
int y = Ry;
int p;
int px= 0;
int py = twoRx2 * y;
PlotEllipsePoint(Cx, Cy, x, y);
//Region 1
p = (int)(Ry2 - (Rx2 * Ry) + (0.25 + Rx2));
while (px < py)
{
x = x + 1;
px = twoRy2 + px;
if (p < 0)
{
p = Ry2 + px + p;
}
else
{
y = y - 1;
py = twoRx2 - py;
p = Ry2 + px - py + p;
}
PlotEllipsePoint(Cx, Cy, x, y);
}
//Region2
p = (int)(Ry2 * (x + 0.5) * (x + 0.5) + Rx2 * (y - 1) * (y - 1) - Rx2 * Ry2);
while (y > 0)
{
y = y - 1;
py = twoRx2 - py;
if (p > 0)
{
p = Rx2 - py + p;
}
else
{
x = x + 1;
px = twoRy2 + px;
p = Rx2 + px - py + p;
}
PlotEllipsePoint(Cx, Cy, x, y);
}
}
public void PlotEllipsePoint(int CX, int CY, int X, int Y)
{
drawPixel(CX + X, CY + Y);
drawPixel(CX - X, CY + Y);
drawPixel(CX + X, CY - Y);
drawPixel(CX - X, CY - Y);
}
public void drawPixel(int x, int y)
{
P.fillOval(x, y, 5, 5);
}
public void mousePressed(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON1)
{
CenterX = e.getX();
CenterY = e.getY();
}
else if (e.getButton() == MouseEvent.BUTTON3)
{
Radius = Radius + 1;
if (Radius == 1)
{
RadiusX = (int) Math.pow((Math.pow((e.getX() - CenterX), 2) + Math.pow((e.getY() - CenterY), 2)), 0.5);
}
else if (Radius == 2)
{
RadiusY = (int) Math.pow((Math.pow((e.getX() - CenterX), 2) + Math.pow((e.getY() - CenterY), 2)), 0.5);
}
PlotEllipsePoint(CenterX, CenterY, RadiusX, RadiusY);
}
}
public static void main(String[] args)
{
JFrame JF = new JFrame("Ellipse");
JF.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JF.setSize(500,500);
Ellipse E = new Ellipse();
JF.getContentPane().add(E);
JF.setVisible(true);
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
Three things immediately jump out...
You should be calling super.paintComponent before doing any custom painting (and there is no need for paintComponent to be public), see Performing Custom Painting and Painting in AWT and Swing for more details
NEVER, EVER maintain a reference to the graphics context, you want something painted, you make a request to the repaint manager and you wait until one of your paint methods is called. Painting is controlled by the repaint manager an repaints may occur at any time, most of the time without yout knowledge or interaction.
You never call repaint to ask the repaint manager to repaint your component...At the end of your mousePressed method, call repaint()...

Java Animations

I've started to take interest with making animations(slideshows, backgrounds etc) in Java. I know that JavaFX is much better for doing this, but I'm just to stubborn to bother switching over.
Here is what I got so far.
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BlurredLightCells extends JPanel {
private static final long serialVersionUID = 4610174943257637060L;
private Random random = new Random();
private ArrayList<LightCell> lightcells;
private float[] blurData = new float[500];
public static void main(String[] args) {
JFrame frame = new JFrame("Swing animated bubbles");
frame.setSize(1000, 750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new BlurredLightCells(60));
frame.setVisible(true);
}
public BlurredLightCells(int amtOfBCells) {
setSize(1000, 750);
/**
* Below we initiate all the cells that are going to be drawn on screen
*/
Arrays.fill(blurData, 1f / 20f);
lightcells = new ArrayList<LightCell>(amtOfBCells);
for (int i = 0; i < amtOfBCells; i++) {
/**
* Below we generate all the values for each cell(SHOULD be random for each one)
*/
int baseSpeed = random(0, 3);
int xSpeed = (int) Math.floor((Math.random() * (baseSpeed - -baseSpeed + baseSpeed)) + -baseSpeed);
int ySpeed = (int) Math.round((Math.random() * baseSpeed) + 0.5);
int radius = random(25, 100);
int x = (int) Math.floor(Math.random() * getWidth());
int y = (int) Math.floor(Math.random() * getHeight());
int blurrAmount = (int) (Math.floor(Math.random() * 10) + 5);
int alpha = (int) ((Math.random() * 15) + 3);
/**
* Now we draw a image, and apply transparency and a slight blur to it
*/
Kernel kernel = new Kernel(blurrAmount, blurrAmount, blurData);
BufferedImageOp op = new ConvolveOp(kernel);
BufferedImage circle = new BufferedImage(150, 150, BufferedImage.TYPE_INT_ARGB);
Graphics2D circlegfx = circle.createGraphics();
circlegfx.setColor(new Color(255, 255, 255, alpha));
circlegfx.fillOval(20, 20, radius, radius);
circle = op.filter(circle, null);
LightCell bubble = new LightCell(x, y, xSpeed, ySpeed, radius, getDirection(random.nextInt(3)), circle);
lightcells.add(bubble);
}
}
public int random(int min, int max) {
final int n = Math.abs(max - min);
return Math.min(min, max) + (n == 0 ? 0 : random.nextInt(n));
}
#Override
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
final Graphics2D g2 = (Graphics2D) g;
GradientPaint gp = new GradientPaint(-w, -h, Color.LIGHT_GRAY, w, h, Color.DARK_GRAY);
g2.setPaint(gp);
g2.fillRect(0, 0, w, h);
long start = System.currentTimeMillis();
for (int i = 0; i < lightcells.size(); i++) {
LightCell cell = lightcells.get(i);
cell.process(g2);
}
System.out.println("Took " + (System.currentTimeMillis() - start) + " milliseconds to draw ALL cells.");
repaint();
}
public String getDirection(int i) {
switch (i) {
case 0:
return "right";
case 1:
return "left";
case 2:
return "up";
case 3:
return "down";
}
return "";
}
private class LightCell {
private int x, y, xSpeed, ySpeed, radius;
private String direction;
private BufferedImage image;
public LightCell(int x, int y, int xSpeed, int ySpeed, int radius, String direction, BufferedImage image) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.radius = radius;
this.direction = direction;
this.image = image;
}
public void process(Graphics g) {
switch (direction) {
case "right":
moveRight();
break;
case "left":
moveLeft();
break;
case "up":
moveUp();
break;
case "down":
moveDown();
break;
}
g.drawImage(image, x, y, null);
}
private void moveUp() {
x += xSpeed;
y -= ySpeed;
if (y + (radius / 2) < 0) {
y = getHeight() + (radius / 2);
x = (int) Math.floor(Math.random() * getWidth());
}
if ((x + radius / 2) < 0 || (x - radius / 2) > getWidth()) {
y = radius + (radius / 2);
x = (int) Math.floor(Math.random() * getWidth());
}
}
private void moveDown() {
x += xSpeed;
y += ySpeed;
if (y - (radius / 2) > getHeight()) {
y = 0 - (radius / 2);
x = (int) Math.floor(Math.random() * getWidth());
}
if ((x + radius / 2) < 0 || (x - radius / 2) > getWidth()) {
y = getHeight() + (radius / 2);
x = (int) Math.floor(Math.random() * getWidth());
}
}
private void moveRight() {
x += ySpeed;
y += xSpeed;
if (y - (radius / 2) > getHeight() || y + (radius / 2) < 0) {
x = 0 - (radius / 2);
y = (int) Math.floor(Math.random() * getHeight());
}
if ((x - radius / 2) > getWidth()) {
x = 0 - (radius / 2);
y = (int) Math.floor(Math.random() * getWidth());
}
}
private void moveLeft() {
x -= ySpeed;
y -= xSpeed;
if (y - (radius / 2) > getHeight() || y + (radius / 2) < 0) {
x = getWidth() + (radius / 2);
y = (int) Math.floor(Math.random() * getHeight());
}
if ((x + radius / 2) < 0) {
x = getWidth() + (radius / 2);
y = (int) Math.floor(Math.random() * getWidth());
}
}
}
}
If you run that, you will see the cells move at a very high speed, and if you look through the code, you see I call repaint() in the paint method in which I override. I know thats not good to do. But my question is, is their any other way in which I could draw each cell outside of the repaint() loop I have right now, because that causes other components to flash/flicker when I use this in a JFrame with other components.
FYI: Aventually I'd like to achieve something similar to this: Click Here
Thanks!
The issue of flicker is to do with the fact that top level containers are not double buffered. Instead of extending from JFrame (or other top level containers), you should consider using something more like JPanel and override it's paintComponent.
nb- Had it in my head that the OP was extending from JFrame...
Two issues could be causing the flickering. The first is overriding paint, the second is not calling super.paint(g) and the time between the updates. A better solution would be to override paintComponent and make sure you are calling super.paintComponent. Also using something like a javax.swing.Timer to schedule updates and regular intervals would also help...
Only call repaint when you want to encourage the RepaintManager to update you component. Don't call repaint from within any paintXxx method, this will cause a never ending loop of paint requests to schedule onto the event queue, eventually consuiming your CPU
I would avoid doing anything in your paintXxx methods that might take time to perform, this will slow down the rendering process. Instead, I would use a javax.swing.Timer for simple updates or for more complicated processing, a Thread which could be used to update the model before it is rendered to the screen.
This is an example of some simple optimisation process I did to take animation of 500 objects to 4500 with only a slight degration in the overall performance.
Updated
I changed you code slight and it works fine...
I changed your paint method to paintComponent and added super.paintComponent(g)
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
final Graphics2D g2 = (Graphics2D) g;
GradientPaint gp = new GradientPaint(-w, -h, Color.LIGHT_GRAY, w, h, Color.DARK_GRAY);
g2.setPaint(gp);
g2.fillRect(0, 0, w, h);
for (int i = 0; i < lightcells.size(); i++) {
LightCell cell = lightcells.get(i);
cell.process(g2);
}
}
And at the end of your constructor I added...
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
timer.start();
To update the UI on a regular bases...

Categories

Resources