im trying to draw 20 rectangles on screen at once using two different classes, one for the frame and one for each rectangle, However only 1 is being drawn. here is the window class
private final List<Vehicle> vehicles = new ArrayList<>();
public Window() {
this.initWindow();
this.populateVehicles();
}
private void initWindow() {
this.setSize(1440, 920);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Steering");
this.setVisible(true);
}
public void populateVehicles() {
for (int i = 0; i < 20; i++) {
int x = Util.getRandomInt(10, 1000);
int y = Util.getRandomInt(20, 800);
Vehicle v = new Vehicle(x, y);
this.add(v);
vehicles.add(v);
System.out.print("printing new rect # " + x + " : " + y + "\n");
}
}
private void repopulateVehicles() {
if (this.vehicles.isEmpty()) return;
for (Vehicle v : vehicles) {
v.repaint();
}
}
public void refresh(boolean hard) {
this.repopulateVehicles();
if (hard) {
SwingUtilities.updateComponentTreeUI(this);
}
}
}
and here is the rectangle class
public class Vehicle extends JPanel {
private int x;
private int y;
private int speed;
private final int width;
private final int height;
public Vehicle(int x, int y) {
this.x = x;
this.y = y;
this.width = 25;
this.height = 10;
}
public void moveVehicle(int x_, int y_) {
this.x += x_;
this.y += y_;
this.repaint(x, y, x_, y_);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Color c = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
g2d.setColor(c);
g2d.rotate(Math.toRadians(30));
g2d.fillRect(this.x, this.y, this.width, this.height);
}
public Dimension getPreferredSize() {
return new Dimension(this.width, this.height);
}
}
the output is just 1 rectangle being drawn on the frame, while is should be that 20 rectangles are being drawn. the debug print statement shows that each is being drawn at different coords but they arent being shown.
The default layout manager for the content pane of a JFrame is the BorderLayout.
By default each component is added to the CENTER of the BorderLayout. Only the last component added will be given a size/location. So only the last component will be visible.
The solution is:
to display components at random locations you will need to use a null layout on the content pane.
you will need to set the size of each component equal to the preferred size, otherwise the default size will still be (0, 0) and there is nothing to paint.
you will need to set the size of each location, otherwise the default will be (0, 0)
the "random" color of the Vehicle should be assigned in the constructor, not the painting method. A painting method should only paint the current state, not change the state of the component.
Note:
This answer only addresses the issues of using a JPanel to represent your Vehicle. You need to understand how a layout manager works to give components a size/location. In this case because you are randomly positioning components on the panel you would need to use a null layout and set the size/location yourself.
This is not the preferred approach. The better approach is to do custom painting yourself of all the Vehicles as demonstrated by Gilbert.
I rearranged your code to create this GUI. I shrank it down to fit better in the answer.
It's a really good idea to separate your code into a model / view / controller (MVC) pattern. This allows you to separate your concerns and focus on one part of the application at a time.
I made your Vehicle class a plain Java getter / setter class that holds one vehicle.
I created a Roadway class to hold the List of Vehicle instances. The Roadway class is another plain Java getter / setter class.
I renamed your Window class DrawingPanel and made it a drawing JPanel. The DrawingPanel class just draws the List of Vehicle instances from the Roadway class. Period.
I created a VehicleListener class to move the vehicles.
I put the JFrame code into a separate class that calls all the other classes.
I made all the classes inner classes so I could post the code as one block.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class VehicleGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new VehicleGUI());
}
private final Dimension drawingPanelDimension;
private DrawingPanel drawingPanel;
private final Roadway roadway;
public VehicleGUI() {
this.drawingPanelDimension = new Dimension(400, 200);
this.roadway = new Roadway(drawingPanelDimension);
}
#Override
public void run() {
JFrame frame = new JFrame("Steering");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.drawingPanel = new DrawingPanel(roadway,
drawingPanelDimension);
frame.add(drawingPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
Timer timer = new Timer (100, new VehicleListener(this, roadway));
timer.setInitialDelay(3000);
timer.start();
}
public void repaint() {
drawingPanel.repaint();
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Roadway roadway;
public DrawingPanel(Roadway roadway,
Dimension drawingPanelDimension) {
this.roadway = roadway;
this.setBackground(Color.WHITE);
this.setPreferredSize(drawingPanelDimension);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Vehicle vehicle : roadway.getVehicles()) {
g2d.setColor(vehicle.getColor());
g2d.fillRect(vehicle.getX(), vehicle.getY(),
vehicle.getWidth(), vehicle.getHeight());
}
}
}
public class VehicleListener implements ActionListener {
private final Roadway roadway;
private final VehicleGUI frame;
public VehicleListener(VehicleGUI frame, Roadway roadway) {
this.frame = frame;
this.roadway = roadway;
}
#Override
public void actionPerformed(ActionEvent event) {
for (Vehicle vehicle : roadway.getVehicles()) {
vehicle.moveVehicle();
}
frame.repaint();
}
}
public class Roadway {
private final Dimension drawingPanelDimension;
private final List<Vehicle> vehicles;
private final Random random;
public Roadway(Dimension drawingPanelDimension) {
this.drawingPanelDimension = drawingPanelDimension;
this.vehicles = new ArrayList<>();
this.random = new Random();
populateVehicles();
}
private void populateVehicles() {
int width = drawingPanelDimension.width;
int height = drawingPanelDimension.height;
for (int i = 0; i < 20; i++) {
int x = random.nextInt(width - 40) + 20;
int y = random.nextInt(height - 40) + 20;
Vehicle v = new Vehicle(x, y, drawingPanelDimension);
vehicles.add(v);
}
}
public List<Vehicle> getVehicles() {
return vehicles;
}
}
public class Vehicle {
private double x;
private double y;
private final int width;
private final int height;
private double speed;
private final Color color;
private final Dimension drawingPanelDimension;
public Vehicle(int x, int y, Dimension drawingPanelDimension) {
this.x = x;
this.y = y;
this.width = 25;
this.height = 10;
this.drawingPanelDimension = drawingPanelDimension;
this.speed = Math.random() * 20.0 - 10.0;
int red = (int) (Math.random() * 128.0);
int green = (int) (Math.random() * 128.0);
int blue = (int) (Math.random() * 128.0);
this.color = new Color(red, green, blue);
}
public void moveVehicle() {
this.x += speed;
this.x = (this.x < 0) ? drawingPanelDimension.width + this.x : this.x;
this.x = (this.x > drawingPanelDimension.width) ?
this.x - drawingPanelDimension.width : this.x;
}
public int getX() {
return (int) Math.round(x);
}
public int getY() {
return (int) Math.round(y);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Color getColor() {
return color;
}
}
}
Related
I'm currently working on a 2D game in Java for school. We have to use an Abstract Factory design pattern. For the 2D implementation I use a factory as follows:
public class Java2DFact extends AbstractFactory {
public Display display;
private Graphics g;
public Java2DFact() {
display = new Display(2000, 1200);
}
#Override
public PlayerShip getPlayership()
{
return new Java2DPlayership(display.panel);
}
In my display class I create a JFrame and Jpanel
public class Display {
public JFrame frame;
public JPanel panel;
public int width, height;
public Display(int width, int height) {
this.width = width;
this.height = height;
frame = new JFrame();
frame.setTitle("SpaceInvaders");
frame.setSize(1200,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
panel = new JPanel(){
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
}
};
panel.setFocusable(true);
frame.add(panel);
}
}
Now from my main gameloop I call the visualize method inside the Java2DPLayership class to visualize my Playership
public class Java2DPlayership extends PlayerShip {
private JPanel panel;
private Graphics2D g2d;
private Image image;
private BufferStrategy bs;
public Java2DPlayership(JPanel panel) {
super();
this.panel = panel;
}
public void visualize() {
try {
image = ImageIO.read(new File("src/Bee.gif"));
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
//g.setColor(new Color(0, 0, 0));
//g.fillRect(10, 10, 12, 8);
g.drawImage(image, (int) super.getMovementComponent().x, (int) super.getMovementComponent().y, null);
Toolkit.getDefaultToolkit().sync();
g.dispose();
panel.repaint();
} catch(Exception e){
System.out.println(e.toString());
}
}
}
My goal is to pass around the JPanel to every entity and let it draw its contents onto the panel before showing it. However I can't seem to figure out how to do this. When using this approach by changing the Graphics of the panel I get a lot of flickering.
Here is a fully functional, albeit simple example, I wrote some time ago. It just has a bunch of balls bouncing off the sides of the panel. Notice that the render method of the Ball class accepts the graphics context from paintComponent. If I had more classes that needed to be rendered, I could have created a Renderable interface and have each class implement it. Then I could have a list of Renderable objects and just go thru them and call the method. But as I also said, that would need to happen quickly to avoid tying up the EDT.
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Bounce extends JPanel {
private static final int COLOR_BOUND = 256;
private final static double INC = 1;
private final static int DIAMETER = 40;
private final static int NBALLS = 20;
private final static int DELAY = 5;
private final static int PANEL_WIDTH = 800;
private final static int PANEL_HEIGHT = 600;
private final static int LEFT_EDGE = 0;
private final static int TOP_EDGE = 0;
private JFrame frame;
private double rightEdge;
private double bottomEdge;
private List<Ball> balls = new ArrayList<>();
private Random rand = new Random();
private List<Long> times = new ArrayList<>();
private int width;
private int height;
public Bounce(int width, int height) {
this.width = width;
this.height = height;
frame = new JFrame("Bounce");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
addComponentListener(new MyComponentListener());
frame.pack();
frame.setLocationRelativeTo(null);
rightEdge = width - DIAMETER;
bottomEdge = height - DIAMETER;
for (int j = 0; j < NBALLS; j++) {
int r = rand.nextInt(COLOR_BOUND);
int g = rand.nextInt(COLOR_BOUND);
int b = rand.nextInt(COLOR_BOUND);
Ball bb = new Ball(new Color(r, g, b), DIAMETER);
balls.add(bb);
}
frame.setVisible(true);
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
public static void main(String[] args) {
new Bounce(PANEL_WIDTH, PANEL_HEIGHT).start();
}
public void start() {
/**
* Note: Using sleep gives a better response time than
* either the Swing timer or the utility timer. For a DELAY
* of 5 msecs between updates, the sleep "wakes up" every 5
* to 6 msecs while the other two options are about every
* 15 to 16 msecs. Not certain why this is happening though
* since the other timers are run on threads.
*
*/
Timer timer = new Timer(0,(ae)-> {repaint();
for (Ball b : balls) {
b.updateDirection();
}} );
timer.setDelay(5); // 5 ms.
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ball ball : balls) {
ball.render(g2d);
}
}
class MyComponentListener extends ComponentAdapter {
public void componentResized(ComponentEvent ce) {
Component comp = ce.getComponent();
rightEdge = comp.getWidth() - DIAMETER;
bottomEdge = comp.getHeight() - DIAMETER;
for (Ball b : balls) {
b.init();
}
}
}
class Ball {
private Color color;
public double x;
private double y;
private double yy;
private int ydir = 1;
private int xdir = 1;
private double slope;
private int diameter;
public Ball(Color color, int diameter) {
this.color = color;
this.diameter = diameter;
init();
}
public void init() {
// Local constants not uses outside of method
// Provides default slope and direction for ball
slope = Math.random() * .25 + .50;
x = (int) (rightEdge * Math.random());
yy = (int) (bottomEdge * Math.random()) + diameter;
xdir = Math.random() > .5 ? -1
: 1;
ydir = Math.random() > .5 ? -1
: 1;
y = yy;
}
public void render(Graphics2D g2d) {
g2d.setColor(color);
g2d.fillOval((int) x, (int) y, diameter, diameter);
}
public void updateDirection() {
x += (xdir * INC);
yy += (ydir * INC);
y = yy * slope;
if (x < LEFT_EDGE || x > rightEdge) {
xdir = -xdir;
}
if (y < TOP_EDGE || y > bottomEdge) {
ydir = -ydir;
}
}
}
}
I have searched this website and cannot find any posts that ask a question pertaining to bouncing a ball off of a JSlider. I have modified the code to get the ball to bounce off the sides of the window opened as well as to speed up via the JSlider panel, but the ball (circle) goes down behind the panel that is the JSlider. While it bounces around the window, it goes through the JSlider, and I want the ball to be able to consider the JSlider as the bottom part of the window. The code shown below is my constructor.
import java.awt.*;
import java.awt.event.*;
import javax. swing. *;
import javax.swing.event.*;
public class SpeedControlPanel extends JPanel
{
private final int WIDTH = 600;
private final int HEIGHT = 400;
private final int BALL_SIZE = 50;
private Circle bouncingBall; // the object that moves
private Timer timer;
private int moveX, moveY; // increment to move each time
private JPanel pSpeeder;
private JSlider sSpeeder;
private JLabel lSpeeder;
Dimension height;
// --------------------------------------------
// Sets up the panel, including the timer
// for the animation
// --------------------------------------------
public SpeedControlPanel ()
{
timer = new Timer(30, new ReboundListener());
this.setLayout (new BorderLayout());
bouncingBall = new Circle(BALL_SIZE);
moveX = moveY = 5;
// Set up a slider object here
setPreferredSize (new Dimension (WIDTH, HEIGHT));
setBackground(Color.black);
lSpeeder = new JLabel("Timer Delay");
lSpeeder.setAlignmentX(Component.LEFT_ALIGNMENT);
sSpeeder = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
sSpeeder.setMajorTickSpacing(40);
sSpeeder.setMinorTickSpacing(10);
sSpeeder.setPaintTicks(true);
sSpeeder.setPaintLabels(true);
sSpeeder.setAlignmentX(Component.LEFT_ALIGNMENT);
sSpeeder.addChangeListener(new SlideListener());
pSpeeder = new JPanel();
pSpeeder.add(lSpeeder);
pSpeeder.add(sSpeeder);
add(pSpeeder, BorderLayout.SOUTH);
timer.start();
}
// ---------------------
// Draw the ball
// ---------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
bouncingBall.draw(page);
}
// ***************************************************
// An action listener for the timer
// ***************************************************
public class ReboundListener implements ActionListener
{
// ----------------------------------------------------
// actionPerformed is called by the timer -- it updates
// the position of the bouncing ball
// ----------------------------------------------------
public void actionPerformed(ActionEvent action)
{
bouncingBall.move(moveX, moveY);
// change direction if ball hits a side
int x = bouncingBall.getX();
int y = bouncingBall.getY();
int slidePanelHt = pSpeeder.getSize().height;
if (x < 0 || x >= WIDTH - BALL_SIZE)
moveX = moveX * -1;
if (y <= 0 || y >= HEIGHT - BALL_SIZE)
moveY = moveY * -1;
repaint();
}
}
// ***************************************************
// A change listener for the slider.
// ***************************************************
private class SlideListener implements ChangeListener
{
// ------------------------------------------------
// Called when the state of the slider has changed;
// resets the delay on the timer.
// ------------------------------------------------
public void stateChanged (ChangeEvent event)
{
timer.setDelay(sSpeeder.getValue());
}
}
}
Is there a way to modify the width/height of the JSlider to get the ball to bounce off of it?
Create a wrapper JPanel that holds everything, and give it a BorderLayout
Put your drawing/animation JPanel into the wrapper in the BorderLayout.CENTER position
Put your JSlider into the wrapper JPanel in the BorderLayout.PAGE_END position
Add the wrapper to the GUI instead of the drawing/animation JPanel
Done
I would recommend separating the gui from its control by implementing the MVC Pattern.
Have a model that holds all the information that the view (gui) needs:
/*
* The model contains the information for the view and information from the view
* The model is independent of the user interface.
*/
class Model{
private final int WIDTH = 600;
private final int HEIGHT = 400;
private int x,y, delay;
private final int radius;
Model(int radius) {
this.radius = radius;
x= radius; y= radius; delay = 30; //default values
}
void move(int moveX, int moveY) {
x += moveX; y+= moveY;
}
int getX() { return x; }
void setX(int x) { this.x = x; }
int getY() {return y;}
void setY(int y) { this.y = y; }
int getRaduis() {return radius;}
int getDelay() {return delay;}
void setDelay(int delay) {this.delay = delay;}
int getWidth() {return WIDTH;}
int getHeiht() {return HEIGHT; }
}
Have a view that uses the model to displays gui. Note that the slider and the animation are on two separate JPanels as proposed by
Hovercraft Full Of Eels:
/*
* View is just that: a dumb as possible display
*/
class View extends JPanel {
View(Model model) {
setLayout (new BorderLayout());
add(new BallPane(model)); //as explained in Hovercraft Full Of Eels
add(new SliderPane(model), BorderLayout.PAGE_END); //answer
}
class BallPane extends JPanel {
private final Model model;
BallPane(Model model) {
this.model = model;
setPreferredSize (new Dimension (model.getWidth(), model.getHeiht()));
setBackground(Color.black);
}
// ---------------------
// Draw the ball
// ---------------------
#Override
public void paintComponent (Graphics page) {
super.paintComponent (page);
page.setColor(Color.CYAN);
page.fillOval(model.getX(), model.getY(), model.getRaduis()*2, model.getRaduis()*2);
}
}
class SliderPane extends JPanel {
private final Model model;
private final JSlider sSpeeder;
SliderPane(Model model) {
this.model = model;
sSpeeder = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
sSpeeder.setMajorTickSpacing(40);
sSpeeder.setMinorTickSpacing(10);
sSpeeder.setPaintTicks(true);
sSpeeder.setPaintLabels(true);
sSpeeder.setAlignmentX(Component.LEFT_ALIGNMENT);
sSpeeder.addChangeListener(new SlideListener());
add(sSpeeder);
}
// ***************************************************
// A change listener for the slider.
// ***************************************************
private class SlideListener implements ChangeListener
{
// ------------------------------------------------
// Called when the state of the slider has changed;
// resets the delay on the timer.
// ------------------------------------------------
#Override
public void stateChanged (ChangeEvent event)
{
model.setDelay(sSpeeder.getValue());
}
}
}
}
Putting it all together: see the following mvce : it adds a controller that controls the model and view.
For convenience and simplicity, the following code can be copy-pasted into one file called BouncingBall.java, and run.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*
* The controller wires the view and model, and does the processing.
*/
public class BouncingBall {
private final int BALL_SIZE = 25;
private int moveX =5, moveY =5; // increment to move each time
private final Timer timer;
private final Model model;
private JPanel view;
BouncingBall() {
model = new Model(BALL_SIZE);
timer = new Timer(model.getDelay(), new ReboundListener());
makeAndShowGui();
timer.start();
}
private void makeAndShowGui() {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view = new View(model);
window.add(view);
window.pack();
window.setResizable(false);
window.setVisible(true);
}
private void updateGui() {
// change direction if ball hits a side
int x = model.getX();
int y = model.getY();
if (x < 0 || x >= model.getWidth() - model.getRaduis()*2) {
moveX = moveX * -1;
}
if (y <= 0 || y >= model.getHeiht() - model.getRaduis()*2) {
moveY = moveY * -1;
}
model.setX(x+moveX);
model.setY(y+moveY);
timer.setDelay(model.getDelay()); //update timer
view.repaint();
}
// ***************************************************
// An action listener for the timer
// ***************************************************
public class ReboundListener implements ActionListener {
// ----------------------------------------------------
// actionPerformed is called by the timer -- it updates
// the position of the bouncing ball
// ----------------------------------------------------
#Override
public void actionPerformed(ActionEvent action) {
updateGui();
}
}
public static void main(String[] args) {
new BouncingBall();
}
}
/*
* The model contains the information for the view and information from the view
* The model is independent of the user interface.
*/
class Model{
private final int WIDTH = 600;
private final int HEIGHT = 400;
private int x,y, delay;
private final int radius;
Model(int radius) {
this.radius = radius;
x= radius; y= radius; delay = 30; //default values
}
void move(int moveX, int moveY) {
x += moveX; y+= moveY;
}
int getX() { return x; }
void setX(int x) { this.x = x; }
int getY() {return y;}
void setY(int y) { this.y = y; }
int getRaduis() {return radius;}
int getDelay() {return delay;}
void setDelay(int delay) {this.delay = delay;}
int getWidth() {return WIDTH;}
int getHeiht() {return HEIGHT; }
}
/*
* View is just that: a dumb as possible display
*/
class View extends JPanel {
View(Model model) {
setLayout (new BorderLayout());
add(new BallPane(model)); //as explained in Hovercraft Full Of Eels
add(new SliderPane(model), BorderLayout.PAGE_END); //answer
}
class BallPane extends JPanel {
private final Model model;
BallPane(Model model) {
this.model = model;
setPreferredSize (new Dimension (model.getWidth(), model.getHeiht()));
setBackground(Color.black);
}
// ---------------------
// Draw the ball
// ---------------------
#Override
public void paintComponent (Graphics page) {
super.paintComponent (page);
page.setColor(Color.CYAN);
page.fillOval(model.getX(), model.getY(), model.getRaduis()*2, model.getRaduis()*2);
}
}
class SliderPane extends JPanel {
private final Model model;
private final JSlider sSpeeder;
SliderPane(Model model) {
this.model = model;
sSpeeder = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
sSpeeder.setMajorTickSpacing(40);
sSpeeder.setMinorTickSpacing(10);
sSpeeder.setPaintTicks(true);
sSpeeder.setPaintLabels(true);
sSpeeder.setAlignmentX(Component.LEFT_ALIGNMENT);
sSpeeder.addChangeListener(new SlideListener());
add(sSpeeder);
}
// ***************************************************
// A change listener for the slider.
// ***************************************************
private class SlideListener implements ChangeListener
{
// ------------------------------------------------
// Called when the state of the slider has changed;
// resets the delay on the timer.
// ------------------------------------------------
#Override
public void stateChanged (ChangeEvent event)
{
model.setDelay(sSpeeder.getValue());
}
}
}
}
I'm generating a hexagon grid and am able to do so but when I add a MouseListener to the individual hexagons (when they're created) it's almost as if they're behind something because hovering/clicking on a hexagon will not register or do anything for that matter. I want to be able to eventually interact with the hexagons but can't do that if I can't get this to work.
My main GUI elements:
import java.awt.*;
import javax.swing.*;
public class Game2
{
public Game2(int radius,int num_hexes)
{
if(num_hexes%2==0) throw new AssertionError("Can't generate map with
an even number of hexagons.");
JFrame frame=new JFrame();
JPanel panel=new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.RED));
panel.setLayout(new BoxLayout(panel,1));
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setTitle("HexGame");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
Rectangle r=frame.getBounds();
int screen_height=r.height;
int screen_width=r.width;
Hexes2 hexes2=new Hexes2(num_hexes,radius,screen_width,screen_height);
panel.add(hexes2);
JScrollPane scroll_pane=new JScrollPane(panel);
frame.getContentPane().add(scroll_pane);
panel.setOpaque(false);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
Runnable r=new Runnable()
{
#Override
public void run()
{
new Game2(100,11);
}
};
SwingUtilities.invokeLater(r);
}
}
My multiple hexagons:
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;
public class Hexes2 extends JPanel
{
private static final long serialVersionUID=1L;
private static List<Polygon> hexagons;
private static int[] rows;
private int radius;
public Hexes2(int num_columns,int radius,int screen_width,int screen_height)
{
super();
this.radius=radius;
hexagons=new LinkedList<Polygon>();
rows=Functions.columns(num_columns);
int x=screen_width/6;
int y=screen_height/2;
double height=radius*Math.sqrt(3);
double range=num_columns-rows[0];
//build by columns, first
for(int j=0;j<num_columns;j++)
{
x+=((3/2)*radius)*1.5015;
if(j<=Math.floor(num_columns/2)) y=(int) (100-(j*(height/2)));
else y=(int) ((100-(height*(range/2))+(num_columns-rows[j])*(height/2)));
for(int i=0;i<rows[j];i++)
{
y+=height;
Hex2 hex=new Hex2(i,radius,x,y);
hexagons.add(hex.getHex());
}
}
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
setOpaque(false);
for(int i=0;i<hexagons.size();i++)
{
Stroke stroke=new BasicStroke(radius/20, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g2.setStroke(stroke);
g2.setColor(Color.BLACK);
g2.drawPolygon(hexagons.get(i));
}
}
};
My singular hexagon class:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
public class Hex2 extends JLabel implements MouseListener
{
private static final long serialVersionUID = 1L;
private int ID;
private Polygon hexagon;
public Hex2(int ID,int r,int x,int y)
{
super();
this.ID=ID;
hexagon=generateHex(r,x,y);
addMouseListener(this);
}
public Polygon generateHex(int r, int x, int y)
{
Polygon hexagon=new Polygon();
for(int i=0;i<6;i++)
{
/*int _x=(int) (x + r*Math.cos(Math.PI / 3.0 * i));
int _y=(int) (y + r*Math.sin(Math.PI / 3.0 * i));*/
int _x=(int) (x + r*Math.cos(i*2*Math.PI/6));
int _y=(int) (y + r*Math.sin(i*2*Math.PI/6));
hexagon.addPoint(_x,_y);
}
return hexagon;
}
public int getID()
{
return ID;
}
public Polygon getHex()
{
return hexagon;
}
#Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("Clicked on hexagon "+ID);
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
};
Functions:
import java.awt.Dimension;
public class Functions
{
//takes in the max width, n (# hexagons), of the largest row (in the middle)
public static int[] columns(int n)
{
int[] columns=new int[n];
int deviation=(int) java.lang.Math.floor(n/2);
for(int i=0;i<n;i++)
{
columns[i]=n-(java.lang.Math.abs(i-deviation));
}
return columns;
}
public static Dimension getScreenSize()
{
return java.awt.Toolkit.getDefaultToolkit().getScreenSize();
}
}
I apologize for the long code, just wanted to be thorough. Any help greatly appreciated, thanks in advance.
You're adding your MouseListener to your Hex2 JLabels:
public Hex2(int ID, int r, int x, int y) {
super();
this.ID = ID;
hexagon = generateHex(r, x, y);
addMouseListener(this);
}
These are JLabels that you never add to the GUI, since you create them here in line A:
for (int j = 0; j < num_columns; j++) {
x += ((3 / 2) * radius) * 1.5015;
if (j <= Math.floor(num_columns / 2))
y = (int) (100 - (j * (height / 2)));
else
y = (int) ((100 - (height * (range / 2)) + (num_columns - rows[j]) * (height / 2)));
for (int i = 0; i < rows[j]; i++) {
y += height;
Hex2 hex = new Hex2(i, radius, x, y); // ****** [A] *****
hexagons.add(hex.getHex()); // ****** [B] *****
}
}
But hex never is added to the GUI. Instead something else, returned by getHex() is added, so the MouseListener won't work. A MouseListener needs to be added to a component that is visualized within the GUI for its actions to do anything.
I think that you're using too many components here. Only one component, a JPanel, should do all the drawings and should have the MouseLIstener added to it. Everything else should be logical classes that don't extend Swing component classes.
For example, run the code below. It shows that the hex as a non-component class, one that responds to a MouseListener since the listener is added only to the single drawing JLabel. Polygons are shapes and have a contains(Point p) method that can be used inside of the mouse listener to allow them to "know" when they've been pressed:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
public class HexPanel extends JPanel {
private static final long serialVersionUID = 1L;
private List<Hex2b> hex2bs = new ArrayList<>();
private int radius;
private int[] rows;
public HexPanel(int num_columns, int radius, int screen_width, int screen_height) {
super();
setBackground(Color.WHITE);
this.radius = radius;
hex2bs = new LinkedList<Hex2b>();
rows = Functions.columns(num_columns);
int x = screen_width / 6;
int y = screen_height / 2;
double height = radius * Math.sqrt(3);
double range = num_columns - rows[0];
// build by columns, first
for (int j = 0; j < num_columns; j++) {
x += ((3 / 2) * radius) * 1.5015;
if (j <= Math.floor(num_columns / 2))
y = (int) (100 - (j * (height / 2)));
else
y = (int) ((100 - (height * (range / 2)) + (num_columns - rows[j]) * (height / 2)));
for (int i = 0; i < rows[j]; i++) {
y += height;
Hex2b hex = new Hex2b(i, radius, x, y);
hex2bs.add(hex);
}
}
addMouseListener(new MyMouse());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // smooth graphics
// setOpaque(false); // doesn't belong in here
for (int i = 0; i < hex2bs.size(); i++) {
Stroke stroke = new BasicStroke(radius / 20, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
Hex2b hex2b = hex2bs.get(i);
Color color = hex2b.getColor();
g2.setColor(color);
g2.fill(hex2b.getHex());
g2.setStroke(stroke);
g2.setColor(Color.BLACK);
g2.draw(hex2b.getHex());
}
}
private class MyMouse extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
for (Hex2b hex2b : hex2bs) {
if (hex2b.getHex().contains(e.getPoint())) {
hex2b.changeColor();
repaint();
break;
}
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Game2b");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolKit = Toolkit.getDefaultToolkit();
Dimension screen = toolKit.getScreenSize();
int width = screen.width;
int height = screen.height;
frame.getContentPane().add(new HexPanel(11, 100, width, height));
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class Hex2b {
private static final Color INIT_COLOR = Color.white;
private static final Color SELECTED_COLOR = Color.red;
private int ID;
private Polygon hexagon;
private Color color = INIT_COLOR;
public Hex2b(int ID, int r, int x, int y) {
super();
this.ID = ID;
hexagon = generateHex(r, x, y);
}
public Color getColor() {
return color;
}
public void changeColor() {
color = color == INIT_COLOR ? SELECTED_COLOR : INIT_COLOR;
}
public Polygon generateHex(int r, int x, int y) {
Polygon hexagon = new Polygon();
for (int i = 0; i < 6; i++) {
int _x = (int) (x + r * Math.cos(i * 2 * Math.PI / 6));
int _y = (int) (y + r * Math.sin(i * 2 * Math.PI / 6));
hexagon.addPoint(_x, _y);
}
return hexagon;
}
public int getID() {
return ID;
}
public Polygon getHex() {
return hexagon;
}
}
I am trying to draw two circle on a panel with a line joining them, all after a button is pressed. So far (apart from tweaking locations of the line) this is ok. However, I would like to animate it using a timer. The first circle should appear, then gradually the line will be revealed, and finally the second circle.
I have looked at many examples of timers, but I can't seem to get it to work for me. I must be misunderstanding something.
here is the ball class (for each circle):
package twoBalls;
import java.awt.Color;
import java.awt.Point;
public class Ball {
private int x;
private int y;
private int r;
private Color color;
private Point location;
private Ball parent;
public Ball(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
Point p = new Point(x, y);
setLocation(p);
}
public void setParent(Ball b) {
parent = b;
}
public Ball getParent() {
return parent;
}
public void setx(int x) {
this.x = x;
}
public void sety(int y) {
this.y = y;
}
public int getx() {
return x;
}
public int gety() {
return y;
}
public int getr() {
return r;
}
public void setPreferedSize() {
}
public void setLocation(Point p) {
setx(p.x);
sety(p.y);
location = p;
}
public Point getLocation() {
return location;
}
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
}
then the class that will store balls in an arrayList. And I think that this is where the actual drawing should take place, along with the timer.
I am trying to set the start and end point of the line to be the same, and increment the end point until it is where it should be, using the timer. I'm probably way of track, but that was the intention!
I have change this class, the if statements in the while loop can now be entered, as I am now comparing different point. But the line doesn't get drawn at all still.
package twoBalls;
import java.awt.Color;
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.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BallsArray extends JPanel implements ActionListener {
private ArrayList<Ball> balls;
private Timer timer;
private final int DELAY = 25;
private int xDest;
private int yDest;
private Point dest;
private Point starts;
private int xStart;
private int yStart;
public BallsArray() {
balls = new ArrayList<Ball>();
timer = new Timer(DELAY, this);
yDest = 0;
xDest = 0;
dest = new Point(xDest, yDest);
starts = new Point(xStart, yStart);
}
public void setDestXY(int x, int y) {
xDest = x;
yDest = y;
dest = new Point(xDest, yDest);
setDest(dest);
}
public void setDest(Point p) {
dest = p;
}
public Point getDest() {
return dest;
}
public void setStartsXY(int x, int y) {
xStart = x;
yStart = y;
starts = new Point(xStart, yStart);
setStarts(starts);
}
public void setStarts(Point p) {
starts = p;
}
public Point getStarts() {
return starts;
}
public void addBall(Ball b) {
balls.add(b);
}
public void addBall(int x, int y, int r) {
balls.add(new Ball(x, y, r));
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < balls.size(); i++) {
if (i == 0) {
paintBall(balls.get(0), g2);
}
if (i != 0) {
int j = i - 1;
Ball bp = balls.get(j);
Ball bc = balls.get(i);
bc.setParent(bp);
paintLine(bc, g2);
paintBall(bc, g2);
}
}
}
public void paintBall(Ball b, Graphics2D g2d) {
Ellipse2D circ = new Ellipse2D.Float(b.getx(), b.gety(), b.getr(),
b.getr());
g2d.draw(circ);
}
public void paintLine(Ball b, Graphics2D g2d) {
timer.start();
if (b != null && b.getLocation() != null) {
Ball parent = b.getParent();
if (parent != null) {
g2d.setColor(Color.GRAY);
if (parent.getLocation() != null && b.getLocation() != null) {
setDest(parent.getLocation());
setStarts(parent.getLocation());
g2d.draw(new Line2D.Float(starts, dest));
}
}
}
}
#Override
public void actionPerformed(ActionEvent e) {
// Not sure what I need to do here
// increment second location somehow
// Point s = getStarts();
Point p = getDest();
Point t = this.getLocation();
while (p != t) {
if (p.x != t.x && p.y != t.y) {
System.out.println("hello");
int x = dest.x;
int y = dest.y;
x++;
y++;
setDestXY(x, y);
p = getDest();
repaint();
} else if (p.x == t.x && p.y != t.y) {
System.out.println("part 2");
int y = dest.y;
y++;
setDestXY(dest.x, y);
p = getDest();
repaint();
} else if (p.x != t.x && p.y == t.y) {
System.out.println("part 3");
int x = dest.x;
x++;
setDestXY(x, dest.y);
p = getDest();
repaint();
}
repaint();
}
}
}
I have had a lot of help online getting this far, I worry I am just beyond my depth now!. I am unsure about the EventQueue/run part below. Here is the class to set it all up:
package twoBalls;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Display implements ActionListener {
private JFrame frame;
private JButton button;
private BallsArray b;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Display ex = new Display();
}
});
}
public Display() {
b = new BallsArray();
frame = new JFrame();
frame.setSize(800, 500);
frame.setTitle("Show balls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
button = new JButton("New Ball");
frame.add(button, BorderLayout.SOUTH);
frame.setVisible(true);
button.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
Ball ball1 = new Ball(100, 100, 50);
b.addBall(ball1);
b.addBall(200, 200, 50);
frame.add(b, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
}
}
At the moment it draws the two circles, but not the line at all.
When you make an animation, it helps to use the model / view / controller pattern.
Here's the GUI I created from your code.
I simplified your Ball class. This is all you need to define a ball.
package twoBalls;
import java.awt.Color;
import java.awt.Point;
public class Ball {
private final int radius;
private final Color color;
private final Point center;
public Ball(int x, int y, int radius, Color color) {
this(new Point(x, y), radius, color);
}
public Ball(Point center, int radius, Color color) {
this.center = center;
this.radius = radius;
this.color = color;
}
public int getRadius() {
return radius;
}
public Color getColor() {
return color;
}
public Point getCenter() {
return center;
}
}
I created the GUIModel class to hold all of the information your GUI needs. This separates the model from the view.
package twoBalls;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
public class GUIModel {
private double direction;
private double distance;
private List<Ball> balls;
private Point lineStartPoint;
private Point lineEndPoint;
public GUIModel() {
this.balls = new ArrayList<>();
}
public void addBall(Ball ball) {
this.balls.add(ball);
}
public List<Ball> getBalls() {
return balls;
}
public void calculatePoints() {
this.lineStartPoint = balls.get(0).getCenter();
this.lineEndPoint = balls.get(1).getCenter();
this.distance = Point.distance(lineStartPoint.x, lineStartPoint.y,
lineEndPoint.x, lineEndPoint.y);
this.direction = Math.atan2(lineEndPoint.y - lineStartPoint.y,
lineEndPoint.x - lineStartPoint.x);
}
public Point getCurrentPoint(int pos, int total) {
double increment = distance / total;
double length = increment * pos;
double x = lineStartPoint.x + Math.cos(direction) * length;
double y = lineStartPoint.y - Math.sin(direction) * length;
x = Math.round(x);
y = Math.round(y);
return new Point((int) x, (int) y);
}
public Point getLineStartPoint() {
return lineStartPoint;
}
}
This class holds the two Ball instances, and calculates the length and direction of the line, divided into total increments.
Now that we've defined the model classes, let's look at the view classes. The first is your Display class.
package twoBalls;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Display implements Runnable {
private GUIModel guiModel;
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Display());
}
public Display() {
this.guiModel = new GUIModel();
Ball ball1 = new Ball(150, 200, 50, Color.BLUE);
Ball ball2 = new Ball(450, 200, 50, Color.GREEN);
guiModel.addBall(ball1);
guiModel.addBall(ball2);
guiModel.calculatePoints();
}
#Override
public void run() {
frame = new JFrame();
frame.setTitle("Show Balls Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
DrawingPanel drawingPanel = new DrawingPanel(guiModel);
panel.add(drawingPanel, BorderLayout.CENTER);
panel.add(createButtonPanel(drawingPanel), BorderLayout.SOUTH);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private JPanel createButtonPanel(DrawingPanel drawingPanel) {
JPanel panel = new JPanel();
JButton startButton = new JButton("Start Animation");
startButton.addActionListener(new StartAnimation(drawingPanel));
panel.add(startButton);
return panel;
}
public class StartAnimation implements ActionListener {
private DrawingPanel drawingPanel;
public StartAnimation(DrawingPanel drawingPanel) {
this.drawingPanel = drawingPanel;
}
#Override
public void actionPerformed(ActionEvent event) {
LineRunnable runnable = new LineRunnable(drawingPanel);
new Thread(runnable).start();
}
}
}
The constructor of the Display class sets up the GUI model.
The run method of the Display class constructs the GUI, and starts the animation.
See how I've separated the model and view.
The StartAnimation class is your controller. It starts the animation when you left click on the JButton. I'll discuss the LineRunnable class later.
Next, let's take a look at the DrawingPanel class.
package twoBalls;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = -3709678584255542338L;
private boolean drawLine;
private int pos;
private int total;
private GUIModel guiModel;
public DrawingPanel(GUIModel guiModel) {
this.guiModel = guiModel;
this.drawLine = false;
this.setPreferredSize(new Dimension(600, 400));
}
public boolean isDrawLine() {
return drawLine;
}
public void setDrawLine(boolean drawLine) {
this.drawLine = drawLine;
}
public void setPos(int pos) {
this.pos = pos;
repaint();
}
public void setTotal(int total) {
this.total = total;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Ball ball : guiModel.getBalls()) {
g2d.setColor(ball.getColor());
Point center = ball.getCenter();
int radius = ball.getRadius();
g2d.fillOval(center.x - radius, center.y - radius, radius + radius,
radius + radius);
}
if (isDrawLine()) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(5.0F));
Point a = guiModel.getLineStartPoint();
Point b = guiModel.getCurrentPoint(pos, total);
g2d.drawLine(a.x, a.y, b.x, b.y);
}
}
}
The only thing this view class does is draw the balls and the line. The responsibility for calculating the length of the line belongs in the model.
I set the preferred size here, and use the pack method in the Display class to get the size of the JFrame. You usually want to know the dimensions of the drawing area, rather than the entire window.
Finally, let's look at the LineRunnable class. This is the class that controls the animation.
package twoBalls;
import java.awt.EventQueue;
public class LineRunnable implements Runnable {
private int total;
private DrawingPanel drawingPanel;
public LineRunnable(DrawingPanel drawingPanel) {
this.drawingPanel = drawingPanel;
this.total = 240;
}
#Override
public void run() {
setDrawLine();
for (int pos = 0; pos <= total; pos++) {
setPos(pos);
sleep(50L);
}
}
private void setDrawLine() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
drawingPanel.setDrawLine(true);
drawingPanel.setTotal(total);
}
});
}
private void setPos(final int pos) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
drawingPanel.setPos(pos);
}
});
}
private void sleep(long delay) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
}
}
}
In the run method, we divide the line into 240 segments, and draw a segment every 50 milliseconds. It takes the GUI 12 seconds to draw the line. You can play with these numbers if you wish.
The for loop is a classic animation loop. First you update the model, which I'm doing through the drawing panel. Then you sleep.
This animation loop is running on a different thread from the GUI thread. This keeps the GUI responsive. Since the loop is running on a different thread, we have to use the invokeLater method to draw on the Event Dispatch thread.
I hope this was helpful to you. Divide and conquer. Don't let a class do more than one thing.
I'm trying to improve my understanding of Java, particularly Java GUI, by making a puzzle program. Currently the user selects an image, which is cut up into a specified number of pieces. The pieces are drawn randomly to the screen but they seem to be covered by blank portions of other pieces, and not all of them show up, but I can print out all the coordinates. I am using absolute positioning because a LayoutManager didn't seem to work. I briefly tried layeredPanes but they confused me and didn't seem to solve the problem. I would really appreciate some help.
Here are the 2 relevant classes:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class PuzzlePieceDriver extends JFrame
{
private static Dimension SCREENSIZE = Toolkit.getDefaultToolkit().getScreenSize();
private static final int HEIGHT = SCREENSIZE.height;
private static final int WIDTH = SCREENSIZE.width;
public static int MY_WIDTH;
public static int MY_HEIGHT;
private static BufferedImage image;
private int xPieces = PuzzleMagicDriver.getXPieces();
private int yPieces = PuzzleMagicDriver.getYPieces();
private PuzzlePiece[] puzzle = new PuzzlePiece[xPieces*yPieces];
public Container pane = this.getContentPane();
private JLayeredPane layeredPane = new JLayeredPane();
public PuzzlePieceDriver(ImageIcon myPuzzleImage)
{
MY_WIDTH = myPuzzleImage.getIconWidth()+(int)myPuzzleImage.getIconHeight()/2;
MY_HEIGHT = myPuzzleImage.getIconHeight()+(int)myPuzzleImage.getIconHeight()/2;
setTitle("Hot Puzz");
setSize(MY_WIDTH,MY_HEIGHT);
setLocationByPlatform(true);
pane.setLayout(null);
image = iconToImage(myPuzzleImage); //pass image into bufferedImage form
puzzle = createClip(image);
//pane.add(layeredPane);
setVisible(true);
}//end constructor
public static BufferedImage iconToImage(ImageIcon icon)
{
Image img = icon.getImage();
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
// Paint the image onto the buffered image
g.drawImage(img, 0, 0, null);
g.dispose();
return image;
}//end BufferedImage
protected int randomNumber(int min, int max)
{
int temp =
min + (int)(Math.random() * ((max - min) + 1));
return temp;
}//end randomNumber
private PuzzlePiece[] createClip(BufferedImage passedImage)
{
int cw, ch;
int w,h;
w = image.getWidth(null);
h = image.getHeight(null);
cw = w/xPieces;
ch = h/yPieces;
int[] cells=new int[xPieces*yPieces];
int dx, dy;
BufferedImage clip = passedImage;
//layeredPane.setPreferredSize(new Dimension(w,h));
for (int x=0; x<xPieces; x++)
{
int sx = x*cw;
for (int y=0; y<yPieces; y++)
{
int sy = y*ch;
int cell = cells[x*xPieces+y];
dx = (cell / xPieces) * cw;
dy = (cell % yPieces) * ch;
clip= passedImage.getSubimage(sx, sy, cw, ch);
int myX = randomNumber(0,(int)w);
int myY = randomNumber(0,(int)h);
PuzzlePiece piece=new PuzzlePiece(clip,myX,myY);
puzzle[x*xPieces+y]=piece;
piece.setBounds(myX,myY,w,h);
//layeredPane.setBounds(myX,myY,w,h);
//layeredPane.add(piece,new Integer(x*xPieces+y));
pane.add(piece);
piece.repaint();
}//end nested for
}//end for
return puzzle;
}//end createClip
}//end class
Sorry if the spacing is a little messed up!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class PuzzlePiece extends JPanel
{
private Point imageCorner; //the image's top-left corner location
private Point prevPt; //mouse location for previous event
private Boolean insideImage =false;
private BufferedImage image;
public PuzzlePiece(BufferedImage clip, int x, int y)
{
image = clip;
imageCorner = new Point(x,y);
//repaint();
}//end constructor
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, (int)getImageCornerX(),(int)getImageCornerY(), this);
System.out.println("paint "+getImageCornerX()+" "+getImageCornerY());
//repaint();
//g.dispose();
}//end paintComponent
public Point getImageCorner()
{
return imageCorner;
}//end getImageCorner
public double getImageCornerY()
{
return imageCorner.getY();
}//end getImageCornerY
public double getImageCornerX()
{
return imageCorner.getX();
}//end getPoint
}//end class PuzzlePiece
Any help would be appreciated, I've gotten really stuck! Thanks!!
I was really intrigued by this idea, so I made another example, using a custom layout manager.
public class MyPuzzelBoard extends JPanel {
public static final int GRID_X = 4;
public static final int GRID_Y = 4;
private BufferedImage image;
public MyPuzzelBoard(BufferedImage image) {
setLayout(new VirtualLayoutManager());
setImage(image);
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
removeAll();
generatePuzzel();
} else {
Component comp = getComponentAt(e.getPoint());
if (comp != null && comp != MyPuzzelBoard.this) {
setComponentZOrder(comp, 0);
invalidate();
revalidate();
repaint();
}
}
}
});
}
public void setImage(BufferedImage value) {
if (value != image) {
image = value;
removeAll();
generatePuzzel();
}
}
public BufferedImage getImage() {
return image;
}
protected float generateRandomNumber() {
return (float) Math.random();
}
protected void generatePuzzel() {
BufferedImage image = getImage();
if (image != null) {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
int clipWidth = imageWidth / GRID_X;
int clipHeight = imageHeight / GRID_Y;
for (int x = 0; x < GRID_X; x++) {
for (int y = 0; y < GRID_Y; y++) {
float xPos = generateRandomNumber();
float yPos = generateRandomNumber();
Rectangle bounds = new Rectangle((x * clipWidth), (y * clipHeight), clipWidth, clipHeight);
MyPiece piece = new MyPiece(image, bounds);
add(piece, new VirtualPoint(xPos, yPos));
}
}
}
invalidate();
revalidate();
repaint();
}
public class VirtualPoint {
private float x;
private float y;
public VirtualPoint(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public void setX(float x) {
this.x = x;
}
public void setY(float y) {
this.y = y;
}
}
public class VirtualLayoutManager implements LayoutManager2 {
private Map<Component, VirtualPoint> mapConstraints;
public VirtualLayoutManager() {
mapConstraints = new WeakHashMap<>(25);
}
#Override
public void addLayoutComponent(Component comp, Object constraints) {
if (constraints instanceof VirtualPoint) {
mapConstraints.put(comp, (VirtualPoint) constraints);
}
}
#Override
public Dimension maximumLayoutSize(Container target) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
#Override
public float getLayoutAlignmentX(Container target) {
return 0.5f;
}
#Override
public float getLayoutAlignmentY(Container target) {
return 0.5f;
}
#Override
public void invalidateLayout(Container target) {
}
#Override
public void addLayoutComponent(String name, Component comp) {
}
#Override
public void removeLayoutComponent(Component comp) {
mapConstraints.remove(comp);
}
#Override
public Dimension preferredLayoutSize(Container parent) {
return new Dimension(400, 400);
}
#Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
#Override
public void layoutContainer(Container parent) {
int width = parent.getWidth();
int height = parent.getHeight();
for (Component comp : parent.getComponents()) {
VirtualPoint p = mapConstraints.get(comp);
if (p != null) {
int x = Math.round(width * p.getX());
int y = Math.round(height * p.getY());
Dimension size = comp.getPreferredSize();
x = Math.min(x, width - size.width);
y = Math.min(y, height - size.height);
comp.setBounds(x, y, size.width, size.height);
}
}
}
}
}
Basically, this uses a "virtual" coordinate system, where by rather then supply absolute x/y positions in pixels, you provide them as percentage of the parent container. Now, to be honest, it wouldn't take much to convert back to absolute positioning, just this way, you also get layout scaling.
The example also demonstrates Z-reording (just in case) and the double click simple re-randomizes the puzzel
Oh, I also made the piece transparent (opaque = false)
Oh, one thing I should mention, while going through this example, I found that it was possible to have pieces placed off screen (completely and partially).
You may want to check your positioning code to make sure that the images when they are laid out aren't been moved off screen ;)
Try using setBorder(new LineBorder(Color.RED)) in your puzzle piece constructor to see where the bounds of your puzzle pieces are. If they are where you'd expect them to be, it's likely that your positioning is wrong. Also make your puzzle pieces extend JComponent instead, or use setOpaque(false) if you're extending JPanel.
There are lots of suggestions I'd like to make, but first...
The way you choose a random position is off...
int myX = randomNumber(0,(int)w);
int myY = randomNumber(0,(int)h);
This allows duplicate position's to be generated (and overlaying cells)
UPDATES (using a layout manager)
Okay, so this is a slight shift in paradigm. Rather then producing a clip and passing it to the piece, I allowed the piece to make chooses about how it was going to render the the piece. Instead, I passed it the Rectangle it was responsible for.
This means, you could simply use something like setCell(Rectangle) to make a piece change (unless you're hell bent on drag'n'drop ;))
I ended up using Board panel due to some interesting behavior under Java 7, but that's another question ;)
package puzzel;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
public class PuzzlePieceDriver extends JFrame {
public PuzzlePieceDriver(ImageIcon myPuzzleImage) {
setTitle("Hot Puzz");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new Board(myPuzzleImage));
pack();
setVisible(true);
}//end constructor
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
ImageIcon image = new ImageIcon(PuzzlePieceDriver.class.getResource("/issue459.jpg"));
PuzzlePieceDriver driver = new PuzzlePieceDriver(image);
driver.setLocationRelativeTo(null);
driver.setVisible(true);
}
});
}
}//end class
A piece panel...
The panel overrides the preferred and minimum size methods...while it works for this example, it's probably better to use setPreferredSize and setMiniumumSize instead ;)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package puzzel;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class PuzzlePiece extends JPanel {
private BufferedImage masterImage;
private Rectangle pieceBounds;
private BufferedImage clip;
public PuzzlePiece(BufferedImage image, Rectangle bounds) {
masterImage = image;
pieceBounds = bounds;
// Make sure the rectangle fits the image
int width = Math.min(pieceBounds.x + pieceBounds.width, image.getWidth() - pieceBounds.x);
int height = Math.min(pieceBounds.y + pieceBounds.height, image.getHeight() - pieceBounds.y);
clip = image.getSubimage(pieceBounds.x, pieceBounds.y, width, height);
}//end constructor
#Override
public Dimension getPreferredSize() {
return pieceBounds.getSize();
}
#Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int y = 0;
g.drawImage(clip, x, y, this);
g.setColor(Color.RED);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}//end paintComponent
}//end class PuzzlePiece
The board panel...used mostly because of some interesting issues I was having with Java 7...Implements a MouseListener, when you run the program, click the board, it's fun ;)
package puzzel;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/**
*
* #author shane
*/
public class Board extends JPanel {
public static final int X_PIECES = 4;
public static final int Y_PIECES = 4;
private PuzzlePiece[] puzzle = new PuzzlePiece[X_PIECES * Y_PIECES];
private static BufferedImage image;
public Board(ImageIcon myPuzzleImage) {
image = iconToImage(myPuzzleImage); //pass image into bufferedImage form
puzzle = createClip();
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
removeAll();
invalidate();
createClip();
// doLayout();
invalidate();
revalidate();
repaint();
}
});
}
public static BufferedImage iconToImage(ImageIcon icon) {
Image img = icon.getImage();
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
// Paint the image onto the buffered image
g.drawImage(img, 0, 0, null);
g.dispose();
return image;
}//end BufferedImage
protected int randomNumber(int min, int max) {
int temp = min + (int) (Math.random() * ((max - min) + 1));
return temp;
}//end randomNumber
private PuzzlePiece[] createClip() {
int cw, ch;
int w, h;
w = image.getWidth(null);
h = image.getHeight(null);
cw = w / X_PIECES;
ch = h / Y_PIECES;
// Generate a list of cell bounds
List<Rectangle> lstBounds = new ArrayList<>(25);
for (int y = 0; y < h; y += ch) {
for (int x = 0; x < w; x += cw) {
lstBounds.add(new Rectangle(x, y, cw, ch));
}
}
BufferedImage clip = image;
setLayout(new GridBagLayout());
for (int x = 0; x < X_PIECES; x++) {
for (int y = 0; y < Y_PIECES; y++) {
// Get a random index
int index = randomNumber(0, lstBounds.size() - 1);
// Remove the bounds so we don't duplicate any positions
Rectangle bounds = lstBounds.remove(index);
PuzzlePiece piece = new PuzzlePiece(clip, bounds);
puzzle[x * X_PIECES + y] = piece;
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.fill = GridBagConstraints.BOTH;
add(piece, gbc);
piece.invalidate();
piece.repaint();
}//end nested for
}//end for
invalidate();
repaint();
return puzzle;
}//end createClip
}
Now I know you eventually going to ask about how to move a piece, GridBagLayout has this wonderful method called getConstraints which allows you to retrieve the constraints used to layout the component in question. You could then modify the gridx and gridy values and use setConstraints to update it (don't forget to call invalidate and repaint ;))
I'd recommend having a read of How to Use GridBagLayout for more information ;)
Eventually, you'll end up with something like: