So, I've started to make a grand strategy game in Java using Swing and I want to create a world map which looped. (So if you reach the west end of the worldmap, the system will start drawing the east side and vice verse, like in HOI4 or EU4.)
I have no idea how to do that.
I tried to create 3 map and if you reach the end the system drop back to the middle, but this method ate my computer.
Or if it's easier to understand, I want to create a cylinder, and draw a part from its wall.
(I'm planning to switch to libgdx, especially if there I can make this much easier.)
Here's my WorldMap class:
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import me.fiveship.waw.objects.Area;
import me.fiveship.waw.objects.Point;
public class WorldMap extends JComponent {
private static final long serialVersionUID = -4823224592445587979L;
public static int WIDTH = 1280;
public static int HEIGHT = 768;
public WorldMap() {
setBounds(0, 0, WIDTH, HEIGHT);
}
public Point location = new Point(0, 0);
public double zoomLevel = 3;
protected java.awt.Point p;
private static boolean settedUp = false;
private static BufferedImage areaMap = null;
private static BufferedImage countryMap = null;
private static BufferedImage regionMap = null;
public static void createPreMaps() {
Point max = Area.max();
areaMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
countryMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
regionMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
// AREA MAP
Graphics g = areaMap.createGraphics();
for (Area area : Area.areas()) {
g.setColor(area.color());
for (Point p : area.points) {
g.fillRect(p.X, p.Y, 1, 1);
}
g.setColor(area.color().darker());
/*
* for (Border b : area.borders) { g.fillRect(b.p.X, b.p.Y, 1, 1); }
*/
}
// COUNTRY MAP
// g = countryMap.createGraphics();
// REGION MAP
// g = regionMap.createGraphics();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (!settedUp) {
settedUp = true;
createPreMaps();
}
Rectangle r = new Rectangle((int) (location.X * zoomLevel), (int) (location.Y * zoomLevel),
(int) (areaMap.getWidth() * zoomLevel), (int) (areaMap.getHeight() * zoomLevel));
g.drawImage(areaMap, r.x, r.y, r.width, r.height, null);
}
}
Okay, so I found out someting. When I had tried the "three map" method, the problem had been that I had wanted to draw two different images (for the base and for the other drawings). Now I made it again and it works fine. (Now I need only two image.)
If anyone need the code:
The paint method of the world map:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (!settedUp) {
settedUp = true;
createPreMaps();
}
Rectangle r = new Rectangle((int) (location.X * zoomLevel), (int) (location.Y * zoomLevel),
(int) (areaMap.getWidth() * zoomLevel), (int) (areaMap.getHeight() * zoomLevel));
g.drawImage(areaMap, r.x, r.y, r.width, r.height, null);
g.drawImage(areaMap, r.x - r.width, r.y, r.width, r.height, null);
}
The mouse listeners (to move and zoom on the map):
addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
if (map != null) {
if (SwingUtilities.isMiddleMouseButton(e)) {
// System.out.println("Pressed");
Point point = e.getPoint();
int validX = (int) (point.x / map.zoomLevel);
int validY = (int) (point.y / map.zoomLevel);
map.p = new Point((int) (validX), (int) (validY));
// System.out.println(e.getX() + ";" + e.getY());
}
}
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseClicked(MouseEvent e) {
}
});
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent event) {
}
#Override
public void mouseDragged(MouseEvent event) {
if (map != null) {
if (SwingUtilities.isMiddleMouseButton(event)) {
Point point = event.getPoint();
int validX = (int) (point.x / map.zoomLevel);
int validY = (int) (point.y / map.zoomLevel);
// System.out.println("Dragged");
int thisX = (int) (map.location.X);
int thisY = (int) (map.location.Y);
// System.out.println("Dragged" + e.getX() + ";" + e.getY());
// Determine how much the mouse moved since the initial click
int xMoved = (thisX + validX) - (thisX + map.p.x);
int yMoved = (thisY + validY) - (thisY + map.p.y);
xMoved *= speed;
yMoved *= speed;
// Move picture to this position
int X = thisX + xMoved;
int Y = thisY + yMoved;
map.location = new me.fiveship.waw.objects.Point(X, Y);
if (map.location.Y > 0) {
map.location.Y = 0;
}
double a = ((-Area.max().Y + map.getBounds().getHeight() / map.zoomLevel));
if (a > map.location.Y) {
map.location.Y = (int) a;
}
int w = Area.max().X;
if (map.location.X > w) {
map.location.X = 0;
}
if (map.location.X < -w + map.getWidth()) {
map.location.X = map.getWidth();
}
// System.out.println(map.location.X);
repaint();
}
}
}
});
addMouseWheelListener(new MouseWheelListener() {
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
// System.out.println(map);
if (map != null) {
double delta = 0.05d * e.getPreciseWheelRotation();
map.zoomLevel -= delta;
if (map.zoomLevel <= 1) {
map.zoomLevel = 1;
} else if (map.zoomLevel >= Consts.c().MaxZoom) {
map.zoomLevel = Consts.c().MaxZoom;
}
// System.out.println(map.zoomLevel);
if (map.location.Y > 0) {
map.location.Y = 0;
}
double a = ((-Area.max().Y + map.getBounds().getHeight() / map.zoomLevel));
if (a > map.location.Y) {
map.location.Y = (int) a;
}
int w = Area.max().X;
if (map.location.X > w) {
map.location.X = 0;
}
if (map.location.X < -w + map.getWidth()) {
map.location.X = map.getWidth();
}
map.repaint();
}
}
});
Related
I am trying to make a Mandelbrot program that allows zooming, but the zoom doesn't seem to be working, and i don't see what is wrong with the way i have implemented the zoom.I am using eclipse and the program doesn't return any errors. Here is my code:
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Mandelbrot extends JFrame {
private final int MAX_ITER = 570;
private static double ZOOM = 200;
private BufferedImage I;
private double zx, zy, cX, cY, tmp;
public static boolean zooming = false;
public Mandelbrot()
{
super("MandelbrotSet");
setBounds(100, 100, 800, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
zx = zy = 0;
cX = (x - 400) / ZOOM;
cY = (y - 300) / ZOOM;
int iter = MAX_ITER;
while (zx * zx + zy * zy < 4 && iter > 0) {
tmp = zx * zx - zy * zy + cX;
zy = 2.0 * zx * zy + cY;
zx = tmp;
iter--;
}
I.setRGB(x, y, iter | (iter << 8));
}
}
setVisible(true);
while(1>0)
{
if(zooming)
{
revalidate();
repaint();
System.out.println("zooming");
zooming = false;
}
} }
#Override
public void paint(Graphics g) {
g.drawImage(I, 0, 0, this);
}
public static void main(String[] args) {
new Mandelbrot().addMouseWheelListener(new MouseWheelListener(){ public void mouseWheelMoved(MouseWheelEvent
e) {
if (e.getWheelRotation() < 0) {
ZOOM=ZOOM+100;
zooming = true;
} else
{
ZOOM=ZOOM-100;
zooming = true;
} } });
} }
Your constructor contains an endless loop. It therefore never returns and your MouseWheelListener is never added to the frame.
You calculate the BufferedImage exactly once (before the endless loop), so even if you would attach the MouseWheelListener before the loop it would have no effect.
I would move the calculation of the picture into its own method, call this method once from the constructor and once from your MouseWheelListener and remove the endless loop from the constructor.
public Mandelbrot()
{
super("MandelbrotSet");
//...
I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
calculatePicture();
setVisible(true);
}
public void calculatePicture() {
for (int y = 0; y < getHeight(); y++) {
//...
}
repaint();
}
public static void main(String[] args) {
new Mandelbrot().addMouseWheelListener(new MouseWheelListener(){
public void mouseWheelMoved(MouseWheelEvent
e) {
//...
calculatePicture();
}
});
}
I am creating a simple game where shapes fall and the player shoots them, but I am having problems creating bullet at every click of the mouse. I have tried various logic with no help, so am just going to put the code up here so you guys can take a look at it and help me out.
The bullet I created is not been created on every click just one is created and it moves on every click which is wrong........I want one bullet to be created per click.
// My main class: mousework2
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
public class mousework2 extends JFrame
{
public static int Width = 300;
public static int Height = 400;
private JPanel p1;
private Image pixMage,gunMage;
public mousework2()
{
super("shoot-em-up");
this.setSize(Width, Height);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension pos = Toolkit.getDefaultToolkit().getScreenSize();
int x = (pos.width - Width) / 2;
int y = (pos.height - Height) / 2;
this.setLocation(x, y);
p1 = new CreateImage();
this.add(p1);
this.getContentPane();
Thread t = new recMove(this);
t.start();
}
class recMove extends Thread
{
JFrame b;
public recMove(JFrame b)
{
this.b = b;
}
public void run()
{
while (true) {
b.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}
class CreateImage extends JPanel implements MouseListener
{
ArrayList<fallShape> rect = new ArrayList<fallShape>();
int x_pos = mousework.Width / 2;
int y_pos = mousework.Height - 50;
int bx_pos = mousework.Width / 2;
int by_pos = mousework.Height;
int y_speed = -10;
boolean clicked;
public CreateImage()
{
for (int i = 0; i < 10; i++) {
rect.add(new fallShape(15, 15, rect));
}
Toolkit picx = Toolkit.getDefaultToolkit();
gunMage = picx.getImage("gunner.jpg");
gunMage = gunMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
Toolkit pic = Toolkit.getDefaultToolkit();
pixMage = pic.getImage("ballfall3.jpg");
pixMage = pixMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
addMouseListener(this);
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseMoved(MouseEvent e)
{
x_pos = e.getX() - 5;
}
});
}
public void mousePressed(MouseEvent e)
{
if (e.getButton() == 1) {
clicked = true;
}
}
public void mouseReleased(MouseEvent e)
{
if (e.getButton() == 1) {
clicked = false;
}
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(pixMage, 0, 0, Width, Height, null);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(gunMage,x_pos,y_pos,10,20,null);
if (clicked) {
by_pos += y_speed;
Shape bullet = new Rectangle2D.Float(bx_pos, by_pos, 3, 10);
g2.setColor(Color.BLACK);
g2.fill(bullet);
g2.draw(bullet);
}
g2.setColor(Color.RED);
for (fallShape b : rect) {
b.move();
g2.fill(b);
}
}
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run()
{
new mousework2().setVisible(true);
}
});
}
}
And:
// My falling shapes class: fallShape
import java.awt.geom.*;
import java.util.*;
public class fallShape extends Rectangle2D.Float
{
public int x_speed, y_speed;
public int l, b;
public int height = mousework.Height;
public int width = mousework.Width;
public ArrayList<fallShape> fall;
public fallShape(int breadth, int length, ArrayList<fallShape> fall)
{
super((int) (Math.random() * (mousework.Width - 20) + 1), 0, breadth, length);
this.b = breadth;
this.l = length;
this.x_speed = (int) Math.random() * (10) + 1;
this.y_speed = (int) Math.random() * (10) + 1;
this.fall = fall;
}
public void move()
{
Rectangle2D rec = new Rectangle2D.Float(super.x, super.y, b, l);
for (fallShape f : fall) {
if (f != this && f.intersects(rec)) {
int rxspeed = x_speed;
int ryspeed = y_speed;
x_speed = f.x_speed;
y_speed = f.y_speed;
f.x_speed = rxspeed;
f.y_speed = ryspeed;
}
}
if (super.x < 0) {
super.x =+ super.x;
//super.y =+ super.y;
x_speed = Math.abs(x_speed);
}
if (super.x> mousework.Width - 30) {
super.x =+ super.x;
super.y =+ super.y;
x_speed =- Math.abs(x_speed);
}
if (super.y < 0) {
super.y = 0;
y_speed = Math.abs(y_speed);
}
super.x += x_speed;
super.y += y_speed;
}
}
if(clicked){
by_pos+=y_speed;
This code only draws the bullet when the mouse is down. This is because you are setting clicked to false in your mouseReleased method:
public void mouseReleased(MouseEvent e){
if(e.getButton()==1)
clicked=false;
}
If you were to remove the body of the mouseReleased method, your bullet would move properly.
However, say you wanted to have more than just one bullet. Currently, your paint method only draws one bullet at a time. To draw multiple bullets, you would need to create a list of the coordinates of the bullets, and add a new coordinate pair to the list whenever you click. Then, in the paint method, just update each position in a for loop.
ArrayList<Integer> by_poss = new ArrayList<>();
by_poss is the list of all the y-positions of your bullets.
public void mousePressed(MouseEvent e){
if(e.getButton() == 1)
by_poss.add(mousework.Height);
}
The mousePressed method adds a new "bullet", in the form of a y-position, to the coordinates.
public void mouseReleased(MouseEvent e){
//do nothing
}
Nothing needs to happen in the mouseReleased method.
//update the bullets
public void paint(Graphics g){
...
g2.setColor(Color.BLACK);
Shape bullet;
for(int i = 0; i < by_poss.size(); i++){
by_poss.set(i, by_poss.get(i) + y_speed); //move the bullet
bullet = new Rectangle2D.Float(bx_pos, by_poss.get(i), 3, 10);
g2.fill(bullet);
g2.draw(bullet);
}
...
}
The for loop in your paint method draws all the bullets, one by one, usin g the y-positions from the by_poss list.
I have a program that makes an ellipse when you click somewhere on a JPanel. When I did a test run, it just made slanted lines to the right of where I clicked. Can anyone find the problem? Thanks, here is the code:
This is the code for the click:
final SpriteField mSpritePanel = new SpriteField();
mSpritePanel.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
float tX = e.getX();
float tY = e.getY();
int tIntWidth;
int tIntHeight;
int tIntRotate = 0;
if(tTextWidth.getText() == null)
{
tTextWidth.setText("50");
}
try
{
tIntWidth = Integer.parseInt(tTextWidth.getText());
}
catch(NumberFormatException ex)
{
tIntWidth = 50;
}
if(tIntWidth == 0)
{
tIntWidth = 50;
}
if(tTextHeight.getText() == null)
{
tTextHeight.setText("50");
}
try
{
tIntHeight = Integer.parseInt(tTextHeight.getText());
}
catch(NumberFormatException ex)
{
tIntHeight = 50;
}
if(tIntHeight == 0)
{
tIntHeight = 50;
}
if(tTextRotation.getText() == null)
{
tTextRotation.setText("0");
}
try
{
tIntRotate = Integer.parseInt(tTextRotation.getText());
}
catch(NumberFormatException ex)
{
tIntRotate = 50;
}
mSpritePanel.CreateSpriteAt(tX, tY, tIntWidth, tIntHeight, tIntRotate);
mSpritePanel.repaint();
}
});
This is the code for my SpriteField class:
public class SpriteField extends JPanel
{
final List<RoundSprite> mSpriteList = new ArrayList<RoundSprite>();
public void CreateSpriteAt(float tX, float tY, int tWidth, int tHeight, int tRotation)
{
RoundSprite mSprite = new RoundSprite();
mSprite.SetPosition(tX, tY);
mSprite.SetSpriteWidth(tWidth);
mSprite.SetSpriteHeight(tHeight);
mSprite.SetSpriteRotation(tRotation);
mSpriteList.add(mSprite);
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
AffineTransform originalTransform = g2.getTransform();
for (RoundSprite mSprite : mSpriteList)
{
mSprite.DrawSprite(g2);
g2.setTransform(originalTransform);
}
}
}
And this is the code for my RoundSprite class:
public class RoundSprite
{
private float mX;
private float mY;
int mWidth;
int mHeight;
int mRotate;
Color mColor;
void DrawSprite(Graphics2D g2)
{
AffineTransform tOldTransform = g2.getTransform();
g2.setColor(mColor);
g2.translate(mX, mY);
g2.rotate(mRotate);
g2.translate(mX - (mWidth / 2), mY - (mHeight / 2));
g2.draw(new Ellipse2D.Double(0, 0, mWidth, mHeight));
g2.setTransform(tOldTransform);
}
public void SetSpriteWidth(int tWidth)
{
mWidth = tWidth;
}
public void SetSpriteHeight(int tHeight)
{
mWidth = tHeight;
}
public void SetSpriteColor(Color tColor)
{
mColor = tColor;
}
public void SetPosition(float x, float y)
{
mX = x;
mY = y;
}
public void SetSpriteRotation(int tRotate)
{
mRotate = tRotate;
}
}
You've got a copy-paste error in your setters for RoundSprite:
public void SetSpriteWidth(int tWidth)
{
mWidth = tWidth; // set the width
}
public void SetSpriteHeight(int tHeight)
{
mWidth = tHeight; // set the width again, leaving mHeight forever 0...
}
This leaves all of your ellipses one-dimensional, so they render as a line.
Just make it mHeight = tHeight instead and your program will work.
Update to respond to the comment on ellipse location
In the RoundSprite#DrawSprite() method you are calling g2.translate() twice. g2.translate() is a relative, not absolute position change, so you don't want to give the mouse coordinates the second time you call it.
replace this:
g2.translate(mX - (mWidth / 2), mY - (mHeight / 2));
with this:
g2.translate(-mWidth/2, -mHeight/2);
to center the ellipse around the mouseclick location.
I already have implemented display of binary search tree . Here's the code , which paints the
binary tree in a jpanel .
public void paint(Graphics g) {
super.paint(g);
System.out.println(" in paint");
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int num = bst.size;
int y = 25;
int nodes = 1;
int level = 1;
int length = getWidth();
Queue<Node> q = new LinkedList<Node>();
Queue<Integer> q2 = new LinkedList<Integer>();
q.add(bst.root);
while (num > 0) {
int pX = (int) Math.round(length / (2.0 * nodes));
int x = pX;
for (int i = 0; i < nodes; i++) {
Node n = q.poll();
//
if (n != null) {
num--;
System.out.println(x);
g2.setColor(Color.BLUE);
String str = n.value + "";
System.out.println(str);
//Font f = Font.getFont(str);
int width = str.length();
g2.setColor(Color.YELLOW);
g2.fillOval(x, y, (30 - 2 * level)+width*3, (30 - 2 * level));
g2.setColor(Color.black);
g2.drawString(n.value + "", x + 10 - level, y + 15);
g2.setColor(Color.black);
if (n.left == null)
q.add(null);
else
q.add(n.left);
if (n.right == null)
q.add(null);
else
q.add(n.right);
if (level != 1) {
int xx = q2.poll();
int yy = q2.poll();
g2.drawLine(xx+width*2, yy, x + (15 - 1 * level)+width*2, y);
}
} else {
q2.poll();
q2.poll();
q.add(null);
q.add(null);
}
q2.add(x);
q2.add(y + 15 - level);
q2.add(x + 30 - 2 * level);
q2.add(y + 15 - level);
x += 2 * pX;
}
y += 40;
nodes = 1 << level;
level++;
}
Now as i insert nodes into my tree , I want the parent nodes to change color of the new node progressively , and then ultimate join as a child .
or
the new node to be inserted moves along the path of it's parent . or something similar
Here's an example :
I have no idea how to accomplish that , with timer or likewise .
Okay, this took a little longer then I wanted (10 month olds don't have any patience)
The basic concept revolves around the idea that you need to change from one state to another over a period of time.
Given a start time and the current time, we can calculate the amount of time that the animation has been running, and given the total animation time, the current progress.
With this (and some clever maths) we can calculate the current state from our start state towards our target state.
I've done movement as well, so that may be a little over kill, but the basic premise remains the same.
I place stateful information about the nodes that need to change in animation properties class and use a javax.swing.Timer to tick over the animation (at a reasonably steady rate). I then update the state of each node as required and repaint the screen.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.management.StringValueExp;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class AnimateNode {
public static void main(String[] args) {
new AnimateNode();
}
public AnimateNode() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new NodePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public interface Node {
public void paint(JComponent parent, Graphics2D g2d);
public void setColor(Color color);
public Color getColor();
public Node getParent();
public Node getLeft();
public Node getRight();
public void setLeftNode(Node node);
public void setRightNode(Node node);
public Point getLocation();
public void setLocation(Point p);
}
public class DefaultNode implements Node {
private int number;
private Node parent;
private Node left;
private Node right;
private Point location;
private Color color;
public DefaultNode(int number, Node parent) {
this.parent = parent;
color = UIManager.getColor("Panel.background");
this.number = number;
}
public void setLeftNode(Node left) {
this.left = left;
}
public void setRightNode(Node right) {
this.right = right;
}
public Node getParent() {
return parent;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
#Override
public Point getLocation() {
return location;
}
#Override
public void setLocation(Point location) {
this.location = location;
}
#Override
public void paint(JComponent parent, Graphics2D g2d) {
FontMetrics fm = g2d.getFontMetrics();
int radius = fm.getHeight();
Point p = getLocation();
int x = p.x - (radius / 2);
int y = p.y - (radius / 2);
Ellipse2D node = new Ellipse2D.Float(x, y, radius, radius);
g2d.setColor(getColor());
g2d.fill(node);
g2d.setColor(Color.GRAY);
g2d.draw(node);
String text = String.valueOf(number);
x = x + ((radius - fm.stringWidth(text)) / 2);
y = y + (((radius - fm.getHeight()) / 2) + fm.getAscent());
g2d.drawString(text, x, y);
}
#Override
public void setColor(Color color) {
this.color = color;
}
#Override
public Color getColor() {
return color;
}
#Override
public String toString() {
return number + " # " + getLocation();
}
}
public class AnimationProperties {
private Point startPoint;
private Point targetPoint;
private Color startColor;
private Color endColor;
private Node node;
public AnimationProperties(Node node) {
this.node = node;
}
public Node getNode() {
return node;
}
public void setTargetColor(Color endColor) {
this.endColor = endColor;
}
public void setStartColor(Color startColor) {
this.startColor = startColor;
}
public void setStartPoint(Point startPoint) {
this.startPoint = startPoint;
}
public void setTargetPoint(Point targetPoint) {
this.targetPoint = targetPoint;
}
public Color getTargetColor() {
return endColor;
}
public Color getStartColor() {
return startColor;
}
public Point getStartPoint() {
return startPoint;
}
public Point getTargetPoint() {
return targetPoint;
}
public Point getLocation(float progress) {
return calculateProgress(getStartPoint(), getTargetPoint(), progress);
}
public Color getColor(float progress) {
return blend(getStartColor(), getTargetColor(), 1f - progress);
}
public void update(float progress) {
node.setLocation(getLocation(progress));
node.setColor(getColor(progress));
}
}
public class NodePane extends JPanel {
private int number;
private Node root;
private Map<Node, AnimationProperties> aniProperties;
private Timer animationTimer;
private Timer startTimer;
private long startTime;
private int runTime = 1000;
public NodePane() {
aniProperties = new HashMap<>(25);
root = addLeftNode(null);
root.setColor(getBackground());
addMouseListener(new MouseAdapter() {
private Random rand;
#Override
public void mouseClicked(MouseEvent e) {
generateNextNode(root);
revalidate();
// repaint();
}
protected void generateNextNode(Node parent) {
Node child = null;
if (rand == null) {
rand = new Random(System.currentTimeMillis());
}
boolean left = rand.nextBoolean();
if (left) {
child = parent.getLeft();
} else {
child = parent.getRight();
}
if (child == null) {
if (left) {
addLeftNode(parent);
} else {
addRightNode(parent);
}
} else {
generateNextNode(child);
}
}
});
startTimer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
stopAnimation();
startTime = -1;
animationTimer.start();
}
});
startTimer.setRepeats(false);
animationTimer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
float progress = 1f;
long duration = System.currentTimeMillis() - startTime;
if (duration >= runTime) {
((Timer) e.getSource()).stop();
} else {
progress = (float) duration / (float) runTime;
}
for (AnimationProperties ap : aniProperties.values()) {
ap.update(progress);
}
repaint();
if (progress == 1f) {
aniProperties.clear();
}
}
});
animationTimer.setRepeats(true);
animationTimer.setCoalesce(true);
}
protected void stopAnimation() {
if (animationTimer.isRunning()) {
animationTimer.stop();
for (AnimationProperties ap : aniProperties.values()) {
Node node = ap.getNode();
ap.setStartColor(node.getColor());
ap.setStartPoint(node.getLocation());
}
}
}
public Point getStartPoint(Node node) {
Point startPoint = node.getLocation();
while (startPoint == null) {
node = node.getParent();
startPoint = node.getLocation();
}
return startPoint;
}
protected void layoutNode(Node node, int x, int y) {
if (node != null) {
FontMetrics fm = getFontMetrics(getFont());
int nodeHeight = fm.getHeight();
if (node.getParent() != null) {
Point p = new Point(x, y);
Point sp = getStartPoint(node);
if (node.getLocation() == null) {
System.out.println("new node " + node);
}
if (node.getLocation() == null || !p.equals(node.getLocation())) {
AnimationProperties ap = new AnimationProperties(node);
ap.setStartColor(node.getColor());
ap.setTargetColor(getBackground());
ap.setStartPoint(sp);
ap.setTargetPoint(new Point(x, y));
node.setLocation(sp);
aniProperties.put(node, ap);
System.out.println("New Node to " + node);
} else {
aniProperties.remove(node);
}
} else {
nodeHeight *= 2;
}
layoutNode(node.getLeft(), x - nodeHeight, y + nodeHeight);
layoutNode(node.getRight(), x + nodeHeight, y + nodeHeight);
}
}
#Override
public void doLayout() {
System.out.println("DoLayout");
stopAnimation();
FontMetrics fm = getFontMetrics(getFont());
int nodeHeight = fm.getHeight();
int x = getWidth() / 2;
int y = nodeHeight;
if (root != null) {
root.setLocation(new Point(x, y));
layoutNode(root, x, y);
// Node node = root.getLeft();
// while (node != null) {
// x -= nodeHeight;
// y += nodeHeight;
// layout(node, x, y);
// node = node.getLeft();
// }
// node = root.getRight();
// x = getWidth() / 2;
// y = nodeHeight;
// while (node != null) {
// x += nodeHeight;
// y += nodeHeight;
// layout(node, x, y);
// node = node.getRight();
// }
}
startTimer.restart();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected Node createNode(Node parent) {
DefaultNode child = new DefaultNode(++number, parent);
child.setColor(Color.GREEN);
System.out.println("Create new node " + child);
return child;
}
protected Node addLeftNode(Node parent) {
Node node = createNode(parent);
if (parent != null) {
System.out.println("Add " + node + " to left of " + parent);
parent.setLeftNode(node);
}
return node;
}
protected Node addRightNode(Node parent) {
Node node = createNode(parent);
if (parent != null) {
System.out.println("Add " + node + " to right of " + parent);
parent.setRightNode(node);
}
return node;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (root != null) {
Graphics2D g2d = (Graphics2D) g.create();
paintConnectors(root, g2d);
paintNode(root, g2d);
g2d.dispose();
}
}
protected void paintNode(Node node, Graphics2D g2d) {
if (node != null && node.getLocation() != null) {
node.paint(this, g2d);
paintNode(node.getLeft(), g2d);
paintNode(node.getRight(), g2d);
}
}
protected void paintConnectors(Node node, Graphics2D g2d) {
if (node != null && node.getLocation() != null) {
Node parent = node.getParent();
if (parent != null) {
g2d.setColor(Color.GRAY);
if (parent.getLocation() != null && node.getLocation() != null) {
g2d.draw(new Line2D.Float(parent.getLocation(), node.getLocation()));
}
}
paintConnectors(node.getLeft(), g2d);
paintConnectors(node.getRight(), g2d);
}
}
}
public static Point calculateProgress(Point startPoint, Point targetPoint, double progress) {
Point point = new Point();
if (startPoint != null && targetPoint != null) {
point.x = calculateProgress(startPoint.x, targetPoint.x, progress);
point.y = calculateProgress(startPoint.y, targetPoint.y, progress);
}
return point;
}
public static int calculateProgress(int startValue, int endValue, double fraction) {
int value = 0;
int distance = endValue - startValue;
value = (int) Math.round((double) distance * fraction);
value += startValue;
return value;
}
public static Color calculateProgress(Color start, Color target, double progress) {
return blend(start, target, progress);
}
public static Color blend(Color color1, Color color2, double ratio) {
float r = (float) ratio;
float ir = (float) 1.0 - r;
float rgb1[] = new float[3];
float rgb2[] = new float[3];
color1.getColorComponents(rgb1);
color2.getColorComponents(rgb2);
float red = rgb1[0] * r + rgb2[0] * ir;
float green = rgb1[1] * r + rgb2[1] * ir;
float blue = rgb1[2] * r + rgb2[2] * ir;
if (red < 0) {
red = 0;
} else if (red > 255) {
red = 255;
}
if (green < 0) {
green = 0;
} else if (green > 255) {
green = 255;
}
if (blue < 0) {
blue = 0;
} else if (blue > 255) {
blue = 255;
}
Color color = null;
try {
color = new Color(red, green, blue);
} catch (IllegalArgumentException exp) {
NumberFormat nf = NumberFormat.getNumberInstance();
System.err.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));
}
return color;
}
}
Update with simple example ;)
Okay, this is a simple example. Basically, it just blinks the node...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BlinkNode {
public static void main(String[] args) {
new BlinkNode();
}
public BlinkNode() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
// Animation stuff
private Timer aniTimer;
// The amount of time that each animation cycle plays for
// in millis
private int aniRunTime = 1000;
// The time the animation was started
private long startTime = -1;
// Our color ranges, where to start and where
// we want to get to and the current state...
private Color startColor;
private Color targetColor;
private Color color;
public TestPane() {
// Initial state
startColor = getBackground();
targetColor = Color.GREEN;
color = startColor;
aniTimer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Set the start time it hasn't already
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
// We're always finished if we run over time...
float progress = 1f;
// Calculate the duration of play
long duration = System.currentTimeMillis() - startTime;
// Have we reached the end yet??
if (duration >= aniRunTime) {
// Reset the start time, this allows the
// animation to cycle. Normally you would stop
// the timer, see the previous example
startTime = -1;
// Swap the start and target colors...
Color tmp = startColor;
startColor = targetColor;
targetColor = tmp;
color = startColor;
} else {
// Calculate the progress
progress = (float) duration / (float) aniRunTime;
// Blend the colors
color = blend(startColor, targetColor, 1f - progress);
}
// update the ui
repaint();
}
});
aniTimer.setRepeats(true);
aniTimer.setCoalesce(true);
aniTimer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - 20) / 2;
int y = (getHeight() - 20) / 2;
g2d.setColor(color);
Ellipse2D node = new Ellipse2D.Float(x, y, 20, 20);
g2d.fill(node);
g2d.setColor(Color.GRAY);
g2d.draw(node);
g2d.dispose();
}
}
public static Color blend(Color color1, Color color2, double ratio) {
float r = (float) ratio;
float ir = (float) 1.0 - r;
float rgb1[] = new float[3];
float rgb2[] = new float[3];
color1.getColorComponents(rgb1);
color2.getColorComponents(rgb2);
float red = rgb1[0] * r + rgb2[0] * ir;
float green = rgb1[1] * r + rgb2[1] * ir;
float blue = rgb1[2] * r + rgb2[2] * ir;
if (red < 0) {
red = 0;
} else if (red > 255) {
red = 255;
}
if (green < 0) {
green = 0;
} else if (green > 255) {
green = 255;
}
if (blue < 0) {
blue = 0;
} else if (blue > 255) {
blue = 255;
}
Color color = null;
try {
color = new Color(red, green, blue);
} catch (IllegalArgumentException exp) {
NumberFormat nf = NumberFormat.getNumberInstance();
System.err.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));
}
return color;
}
}
Conceptually, if each node should have a specific color then each Node instance should have a Color attribute. In the example cited here, the class Node has a number of static updateXxx() methods that traverse the program's (simpler) model, updating nodes as specified. In particular, updateColor() sets each element's Color field to the specified color. Your implementation of paintComponent() can do something similar.
Addendum: As #MadP comments, javax.swing.Timer is well suited to periodic GUI updates, as the timer's actionPerformed() method is executed on the EDT. In this example, the model is updated with each invocation, and the new state is rendered when repaint() (indirectly) calls paintComponent().
This is my class which I found on the Internet. It was originally an applet but I don't want to use it as an applet so I changed some methods (such as init() to a constructor).
However, it doesn't work. Would you please help me?
SignInFrame Frame:
public class SignInFrame extends javax.swing.JFrame {
Panel panel;
/** Creates new form SignInFrame */
public SignInFrame() {
initComponents();
}
public void init() {
getContentPane().add(panel = new Panel());
}
public void start() {
panel.start();
}
public void stop() {
panel.stop();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SignInFrame().setVisible(true);
}
});
}}
Panel Dialog:
package ClientGUI;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
/**
*
* #author ICC
*/
public class Panel extends javax.swing.JPanel implements Runnable{
private Thread thread;
private BufferedImage bimg;
private static final int NUMPTS = 6;
// solid line stoke
protected BasicStroke solid = new BasicStroke(10.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
// dashed line stroke
protected BasicStroke dashed = new BasicStroke(10.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10, new float[] {5}, 0);
private float animpts[] = new float[NUMPTS * 2];
private float deltas[] = new float[NUMPTS * 2];
protected Paint fillPaint, drawPaint;
// indicates whether or not to fill shape
protected boolean doFill = true;
// indicates whether or not to draw shape
protected boolean doDraw = true;
protected GradientPaint gradient;
protected BasicStroke stroke;
public Panel() {
setBackground(Color.white);
gradient = new GradientPaint(0,0,Color.red,200,200,Color.yellow);
fillPaint = gradient;
drawPaint = Color.blue;
stroke = solid;
}
// generates new points for the path
public void animate(float[] pts, float[] deltas, int i, int limit) {
float newpt = pts[i] + deltas[i];
if (newpt <= 0) {
newpt = -newpt;
deltas[i] = (float) (Math.random() * 4.0 + 2.0);
} else if (newpt >= (float) limit) {
newpt = 2.0f * limit - newpt;
deltas[i] = - (float) (Math.random() * 4.0 + 2.0);
}
pts[i] = newpt;
}
/*
* generates random points with the specified surface width
* and height for the path
*/
public void reset(int w, int h) {
for (int i = 0; i < animpts.length; i += 2) {
animpts[i + 0] = (float) (Math.random() * w);
animpts[i + 1] = (float) (Math.random() * h);
deltas[i + 0] = (float) (Math.random() * 6.0 + 4.0);
deltas[i + 1] = (float) (Math.random() * 6.0 + 4.0);
if (animpts[i + 0] > w / 2.0f) {
deltas[i + 0] = -deltas[i + 0];
}
if (animpts[i + 1] > h / 2.0f) {
deltas[i + 1] = -deltas[i + 1];
}
}
gradient = new GradientPaint(0,0,Color.red,w*.7f,h*.7f,Color.yellow);
}
// calls animate for every point in animpts
public void step(int w, int h) {
for (int i = 0; i < animpts.length; i += 2) {
animate(animpts, deltas, i + 0, w);
animate(animpts, deltas, i + 1, h);
}
}
// sets the points of the path and draws and fills the path
public void drawDemo(int w, int h, Graphics2D g2) {
float[] ctrlpts = animpts;
int len = ctrlpts.length;
float prevx = ctrlpts[len - 2];
float prevy = ctrlpts[len - 1];
float curx = ctrlpts[0];
float cury = ctrlpts[1];
float midx = (curx + prevx) / 2.0f;
float midy = (cury + prevy) / 2.0f;
GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
gp.moveTo(midx, midy);
for (int i = 2; i <= ctrlpts.length; i += 2) {
float x1 = (midx + curx) / 2.0f;
float y1 = (midy + cury) / 2.0f;
prevx = curx;
prevy = cury;
if (i < ctrlpts.length) {
curx = ctrlpts[i + 0];
cury = ctrlpts[i + 1];
} else {
curx = ctrlpts[0];
cury = ctrlpts[1];
}
midx = (curx + prevx) / 2.0f;
midy = (cury + prevy) / 2.0f;
float x2 = (prevx + midx) / 2.0f;
float y2 = (prevy + midy) / 2.0f;
gp.curveTo(x1, y1, x2, y2, midx, midy);
}
gp.closePath();
if (doDraw) {
g2.setPaint(drawPaint);
g2.setStroke(stroke);
g2.draw(gp);
}
if (doFill) {
if (fillPaint instanceof GradientPaint) {
fillPaint = gradient;
}
g2.setPaint(fillPaint);
g2.fill(gp);
}
}
public Graphics2D createGraphics2D(int w, int h) {
Graphics2D g2 = null;
if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
bimg = (BufferedImage) createImage(w, h);
reset(w, h);
}
g2 = bimg.createGraphics();
g2.setBackground(getBackground());
g2.clearRect(0, 0, w, h);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
return g2;
}
public void paint(Graphics g) {
Dimension d = getSize();
step(d.width, d.height);
Graphics2D g2 = createGraphics2D(d.width, d.height);
drawDemo(d.width, d.height, g2);
g2.dispose();
if (bimg != null) {
g.drawImage(bimg, 0, 0, this);
}
}
public void start() {
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
public synchronized void stop() {
thread = null;
}
public void run() {
Thread me = Thread.currentThread();
while (thread == me) {
repaint();
try {
Thread.sleep(10);
} catch (Exception e) { break; }
}
thread = null;
}
public static void main(String argv[]) {
SignInFrame n = new SignInFrame();
n.start();
}}
In your SignInFrame constructor, you call initComponents(), but that does not exist. I think you mean to call init(). Also your JFrame does not have a size set, when I ran this under linux (Java 1.6), it worked but was tiny, you should add a setSize call.
Try it with these edits:
public class SignInFrame extends javax.swing.JFrame {
Panel panel;
/** Creates new form SignInFrame */
public SignInFrame() {
setSize (600,600);
init();
}
public void init() {
getContentPane().add(panel = new Panel());
start();
}
public void start() {
panel.start();
}
public void stop() {
panel.stop();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SignInFrame().setVisible(true);
}
});
}
}