I am very new to this community. I have tried to get up to speed on all rules before posting this question (as well as researching solutions). I apologize if I offended or broke any rules through ignorance. Please also excuse my awful code, I am still learning. Thank you for understanding!
EDIT: I have added additional information and tried different approaches to this issue I'm having. I have reworked part of the code below.
I am building a simple football game with a game field panel and control panel. The game field displays all of the player and tackles on the GUI. The control panel sets the difficulty of the game, starts the timer, and the type of quarterback. I ran into a road block where I have all of my code to compile correctly, but when calling set methods on the GameField class to update the score, it updates the variable but not the actual score through my JTextArea Score keeper.
I have instantiated the ControlPanel within the GameField class. I've also tested with a System.out.println() and it shows that it is indeed updating the variable. Is updating JTextArea allowed between classes?
GameField.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GameField extends JPanel implements KeyListener {
ControlPanel cp = new ControlPanel();
// Game pieces
private JButton playerIcon = new JButton("RB");
private JButton tackleIcon1 = new JButton("LB");
private JButton tackleIcon2 = new JButton("LB");
private JButton fieldGoal = new JButton("FG");
// Player and Tackle locations
private int playerPositionX = 100;
private int playerPositionY = 500;
private int tackle1PositionX = 1200;
private int tackle1PositionY = 400;
private int tackle2PositionX = 1200;
private int tackle2PositionY = 600;
// Player variable speeds
private int playerSpeed = 20;
public GameField() {
setLayout(null);
setBackground(Color.green);
add(playerIcon);
playerIcon.setBounds(new Rectangle(getPlayerPositionX(), getPlayerPositionY(), 80, 30));
add(tackleIcon1);
tackleIcon1.setBounds(new Rectangle(getTackle1PositionX(), getTackle1PositionY(), 100, 50));
add(tackleIcon2);
tackleIcon2.setBounds(new Rectangle(getTackle2PositionX(), getTackle2PositionY(), 100, 50));
add(fieldGoal);
fieldGoal.setBounds(new Rectangle(1600, 100, 100, 800));
playerIsTackled();
setFocusable(true);
addKeyListener(this);
}
public void playerIsTackled() {
Rectangle playerRect = playerIcon.getBounds();
Rectangle tackle1Rect = tackleIcon1.getBounds();
Rectangle tackle2Rect = tackleIcon2.getBounds();
if (playerRect.intersects(tackle1Rect) || playerRect.intersects(tackle2Rect)) {
setPlayerPositionX(100);
setPlayerPositionY(500);
setTackle1PositionX(1200);
setTackle1PositionY(400);
setTackle2PositionX(1200);
setTackle2PositionY(600);
playerIcon.setBounds(getPlayerPositionX(), getPlayerPositionY(), 80, 30);
tackleIcon1.setBounds(getTackle1PositionX(), getTackle1PositionY(), 100, 50);
tackleIcon2.setBounds(getTackle2PositionX(), getTackle2PositionY(), 100, 50);
cp.setCurrentTackles(cp.getCurrentTackles() + 1);
System.out.println(cp.getCurrentTackles());
}
}
public void playerScored() {
Rectangle playerRect = playerIcon.getBounds();
Rectangle fieldGoalRect = fieldGoal.getBounds();
if (playerRect.intersects(fieldGoalRect)) {
setPlayerPositionX(100);
setPlayerPositionY(500);
setTackle1PositionX(1200);
setTackle1PositionY(400);
setTackle2PositionX(1200);
setTackle2PositionY(600);
playerIcon.setBounds(getPlayerPositionX(), getPlayerPositionY(), 80, 30);
tackleIcon1.setBounds(getTackle1PositionX(), getTackle1PositionY(), 100, 50);
tackleIcon2.setBounds(getTackle2PositionX(), getTackle2PositionY(), 100, 50);
cp.setCurrentScore(cp.getCurrentScore() + 1);
System.out.println(cp.getCurrentScore());
}
}
public void moveToPlayer() {
if (getTackle1PositionX() > getPlayerPositionX()) {
setTackle1PositionX(getTackle1PositionX() - 1);
} else {
setTackle1PositionX(getTackle1PositionX() + 1);
}
if (getTackle1PositionY() > getPlayerPositionY()) {
setTackle1PositionY(getTackle1PositionY() - 1);
} else {
setTackle1PositionY(getTackle1PositionY() + 1);
}
getTackleIcon1().setBounds(getTackle1PositionX(), getTackle1PositionY(), 100, 50);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
requestFocusInWindow();
playerIsTackled();
playerScored();
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if (k == e.VK_LEFT && getPlayerPositionX() > 0) {
setPlayerPositionX(getPlayerPositionX() - getPlayerSpeed());
}
if (k == e.VK_RIGHT && getPlayerPositionX() < 1703) {
setPlayerPositionX(getPlayerPositionX() + getPlayerSpeed());
}
if (k == e.VK_UP && getPlayerPositionY() > 0) {
setPlayerPositionY(getPlayerPositionY() - getPlayerSpeed());
}
if (k == e.VK_DOWN && getPlayerPositionY() < 1089) {
setPlayerPositionY(getPlayerPositionY() + getPlayerSpeed());
}
getPlayerIcon().setBounds(getPlayerPositionX(), getPlayerPositionY(), 80, 30);
}
Below is the ControlPanel.java. In the actionPerformed method, you can see I added a getScore().setText() method for updating the score. It correctly updates the score there if I replace the getCurrentScore() method with any integer.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Hashtable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ControlPanel extends JPanel implements ActionListener, ChangeListener {
private JButton start;
private JButton stop;
private JSlider speed;
private JComboBox playerList;
private Timer tim;
private int delay = 1000;
private int i = 0;
private int currentScore = 0;
private int currentTackles = 0;
private JTextArea timer = new JTextArea("Timer: " + 0, 1, 6);
private JTextArea score = new JTextArea("Field Goals: " + currentScore + " Tackles: " + currentTackles, 1, 16);
private String[] playerStyle = {"Slow Runner", "Running Back", "All Star"};
public ControlPanel() {
super();
setBackground(Color.darkGray);
// Game controls
start = new JButton("Start");
stop = new JButton("Stop");
speed = new JSlider(JSlider.HORIZONTAL, 0, 2, 1);
playerList = new JComboBox(getPlayerStyle());
// Slider label
Hashtable labelTable = new Hashtable();
labelTable.put(new Integer(0), new JLabel("Slow"));
labelTable.put(new Integer(1), new JLabel("Normal"));
labelTable.put(new Integer(2), new JLabel("Fast"));
speed.setLabelTable(labelTable);
speed.setPaintLabels(true);
// Combo box dropdown
playerList.setSelectedIndex(1);
// Timer
tim = new Timer(getDelay(), this);
// Add methods
add(start);
add(stop);
add(timer);
add(speed);
add(score);
add(playerList);
// Event listeners
start.addActionListener(this);
stop.addActionListener(this);
speed.addChangeListener(this);
playerList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String playerChoice = (String) cb.getSelectedItem();
playerList.setSelectedItem(playerChoice);
}
});
// Set focus to false on all game controls
start.setFocusable(false);
stop.setFocusable(false);
speed.setFocusable(false);
playerList.setFocusable(false);
}
#Override
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj == getTim()) {
setI(getI() + 1);
getTimer().setText("Timer: " + getI());
getScore().setText("Field Goals: " + getCurrentScore() + " Tackles: " + getCurrentTackles());
}
if (obj == getStop()) {
getTim().stop();
}
if (obj == getStart()) {
getTim().start();
}
}
#Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
int currentSpeed = (int) source.getValue();
if (currentSpeed == 0) {
int delaySpeed = getTim().getDelay();
delaySpeed = (int) 2000;
getTim().setDelay(delaySpeed);
}
if (currentSpeed == 1) {
int delaySpeed = getTim().getDelay();
delaySpeed = (int) 1000;
getTim().setDelay(delaySpeed);
}
if (currentSpeed == 2) {
int delaySpeed = getTim().getDelay();
delaySpeed = (int) 500;
getTim().setDelay(delaySpeed);
}
}
MyJPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyJPanel extends JPanel {
public MyJPanel() {
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
ControlPanel gm = new ControlPanel();
GameField gf = new GameField();
add(gm, "North");
add(gf, "Center");
}
}
Thanks to #D.B. I have gotten my code to run as intended. Because I only intend to have two JPanels (Control Panel and Game Panel), I added the panels to pass as arguments to each other. After this, the code started working as intended. Thank you for pushing me into the right step! It was such a simple mistake. Special thanks to #DaveyDaveDave for motivating me to push harder!
Changes I made to my main JPanel object:
public class MyJPanel extends JPanel {
ControlPanel gm = new ControlPanel();
GameField gf = new GameField();
public MyJPanel() {
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
add(gm, "North");
add(gf, "Center");
gf.setCp(gm);
gm.setGf(gf);
}
}
Related
Im trying to code a simple 2D game. I have a Square that you can move with a/w/d. I got jumping working with a sort of gravity but now I cant get myself to land on a platform. I know how to detect collision between two things but even still idk what to do. My gravity always pulls me down until I reach groundLevel which is part of the problem. Here is my code so far. There's a lot of experimenting happing so its pretty messy.
import javax.swing.Timer;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
/**
* Custom Graphics Example: Using key/button to move a line left or right.
*/
public class PlatformingGame extends JFrame implements ActionListener, KeyListener{
// Name-constants for the various dimensions
public static final int CANVAS_WIDTH = 800;
public static final int CANVAS_HEIGHT = 400;
public static final Color LINE_COLOR = Color.BLACK;
public static final Color CANVAS_BACKGROUND = Color.WHITE;
public int GRAVITY = 10;
public int TERMINAL_VELOCITY = 300;
public int vertical_speed = 0;
public int jumpSpeed;
public int groundLevel = CANVAS_HEIGHT;
int Shapes;
private DrawCanvas canvas; // the custom drawing canvas (extends JPanel)
public enum STATE {
PLAYING,
PAUSED,
ONGROUND,
INAIR
};
JButton btnStartRestat, btnExit;
Timer timer;
Rectangle2D.Double guy, platform, platform2;
Shape[] shapeArr = new Shape[10];
int dx, dy;
/** Constructor to set up the GUI */
ArrayList myKeys = new ArrayList<Character>();
public static STATE gameState = STATE.PAUSED;
//public static STATE playerState = STATE.ONGROUND;
public PlatformingGame() {
dx = 3;
dy = 3;
guy = new Rectangle2D.Double( CANVAS_WIDTH/2 - 20, CANVAS_HEIGHT, 30, 20);
platform2 = new Rectangle2D.Double( CANVAS_WIDTH/4, CANVAS_HEIGHT/2+130, 50, 10);
platform = new Rectangle2D.Double( CANVAS_WIDTH/3, CANVAS_HEIGHT/2+50, 50, 10);
timer = new Timer(10, this);
// Set up a panel for the buttons
JPanel btnPanel = new JPanel();
// btnPanel.setPreferredSize(new Dimension(CANVAS_WIDTH/2, CANVAS_HEIGHT));
btnPanel.setLayout(new FlowLayout());
btnStartRestat = new JButton("Start/Restart");
btnExit = new JButton("Exit");
btnPanel.add(btnStartRestat);
btnPanel.add(btnExit);
btnStartRestat.addActionListener(this);
btnExit.addActionListener(this);
// Set up a custom drawing JPanel
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
// Add both panels to this JFrame
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(btnPanel, BorderLayout.SOUTH);
// "this" JFrame fires KeyEvent
addKeyListener(this);
requestFocus(); // set the focus to JFrame to receive KeyEvent
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Handle the CLOSE button
pack(); // pack all the components in the JFrame
setVisible(true); // show it
}
public void generateSpikes(){
Rectangle2D.Double spikes = null;
for (int i = 0; i < 10; i++) {
spikes = new Rectangle2D.Double (CANVAS_WIDTH - 300 + i*20 , CANVAS_HEIGHT - 30 , 20, 30);
shapeArr[i] = spikes;
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnStartRestat)
{
generateSpikes();
dx = 3;
dy = 3;
guy = new Rectangle2D.Double( 100, CANVAS_HEIGHT - guy.height, 15, 30);
platform = new Rectangle2D.Double( CANVAS_WIDTH/3, CANVAS_HEIGHT/2+50, 50, 10);
platform2 = new Rectangle2D.Double( CANVAS_WIDTH/4, CANVAS_HEIGHT/2+130, 50, 10);
gameState = STATE.PLAYING;
}
else if (e.getSource()==btnExit)
{
// requestFocus(); // change the focus to JFrame to receive KeyEvent
}
else if (e.getSource()== timer){
if (isHitDetected(platform2, guy)){
// playerState = STATE.ONGROUND;
guy.y = platform2.y;
}
if (myKeys.contains('a')
&& (guy.x > 0)){
guy.x = guy.x - 5; }
if (myKeys.contains('d')
&& (guy.x < CANVAS_WIDTH - guy.width)){
guy.x = guy.x + 5; }
{
updateGuyPosition();
}
requestFocus();
canvas.repaint();
}
}
public void updateGuyPosition(){
double guyHeight = guy.height;
if (guy.x >= CANVAS_WIDTH - guy.width){
}
if(gameState == STATE.PLAYING) {
guy.y += jumpSpeed;
if (guy.y < groundLevel - guyHeight) {
// if(playerState == STATE.INAIR) {
jumpSpeed += 1;
}
// }
else {
// if(playerState == STATE.INAIR) {
//playerState = STATE.ONGROUND;
jumpSpeed = 0;
guy.y = groundLevel - guyHeight;
}
// }
if (myKeys.contains('w') == true && guy.y == groundLevel - guyHeight) {
jumpSpeed = -15;
// playerState = STATE.INAIR;
}
}
}
public static boolean isHitDetected(Shape shapeA, Shape shapeB) {
Area areaA = new Area(shapeA);
areaA.intersect(new Area(shapeB));
return !areaA.isEmpty();
}
public void keyPressed(KeyEvent evt) {
if (!myKeys.contains(evt.getKeyChar())){
myKeys.add(evt.getKeyChar());
}
}
public void keyReleased(KeyEvent evt) {
myKeys.remove(myKeys.indexOf(evt.getKeyChar()));
}
public void keyTyped(KeyEvent evt) {
}
/** The entry main() method */
public static void main(String[] args) {
PlatformingGame myProg = new PlatformingGame(); // Let the constructor do the job
myProg.timer.start();
}
/**
* DrawCanvas (inner class) is a JPanel used for custom drawing
*/
class DrawCanvas extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(CANVAS_BACKGROUND);
g.setColor(LINE_COLOR);
Graphics2D g2d = (Graphics2D)g;
if(gameState == STATE.PLAYING) {
g2d.fill(guy);
g.setColor(Color.lightGray);
g2d.fill(platform);
g2d.fill(platform2);
for (int i = 0; i < 10; i++) {
g2d.fill(shapeArr[i]);
}
}
if(gameState == STATE.PAUSED) {
g.drawString("Game Paused", CANVAS_WIDTH/2, CANVAS_HEIGHT/2);
}
}
}
}
So I have the following code, and whenever I run the program it gives me an error. It all seems correct to me, but it isn't.
It is supposed to take the value of whatever is in the textfields and move the square to that position, and whenever the user enters a new value it is supposed to change the square to whatever the textfield says it should be. Can anybody fix this, or at least tell me whats wrong? Sorry about the code, it got even more screwed up when I started trying to debug it.
The error is
Exception in thread "main" java.lang.NullPointerException
at com.theDevCorner.Game$OptionPanel.<init>(Game.java:228)
at com.theDevCorner.Game$GridPane.<init>(Game.java:81)
at com.theDevCorner.Game.<init>(Game.java:35)
at com.theDevCorner.Game.main(Game.java:52)
Code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Game extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private GridPane gridPane;
private DragPanel drag;
public boolean isMouseClicked = false;
public static JMenuBar bar = new JMenuBar();
public int gridY = 1;
public int gridX = 1;
public Game() {
setLayout(new BorderLayout());
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
gridPane = new GridPane();
gridPane.setBorder(BorderFactory.createLineBorder(Color.white));
add(gridPane);
drag = new DragPanel(options);
drag.setBorder(BorderFactory.createLineBorder(Color.white));
drag.setBackground(new Color(100, 100, 125));
add(drag, BorderLayout.WEST);
}
public static void main(String args[]) {
Game game = new Game();
JFrame frame = new JFrame();
frame.setTitle("Game");
frame.setVisible(true);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
if (e.getActionCommand().equalsIgnoreCase("grid")) {
gridPane.setGridOn(!gridPane.isGridOn());
}
if (e.getActionCommand().equalsIgnoreCase("square")) {
gridPane.setSqaureOn(!gridPane.isSquareOn());
}
if (e.getActionCommand().equalsIgnoreCase("vgrid")) {
gridPane.setVertOn(!gridPane.isVertOn());
}
}
public class GridPane extends JPanel {
public OptionPanel op = new OptionPanel();
private static final long serialVersionUID = 1L;
private boolean gridOn = false;
private boolean squareOn = false;
private boolean vertOn = false;
public int x = 0,y = 0,w = 0,h = 0;
public GridPane() {
setBackground(Color.BLACK);
}
public boolean isGridOn() {
return gridOn;
}
public boolean isSquareOn() {
return squareOn;
}
public boolean isVertOn() {
return vertOn;
}
public void setGridOn(boolean value) {
if (value != gridOn) {
this.gridOn = value;
repaint();
}
}
public void setVertOn(boolean value) {
if (value != vertOn) {
this.vertOn = value;
repaint();
}
}
public void setSqaureOn(boolean value) {
if (value != squareOn) {
this.squareOn = value;
repaint();
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Toolkit tk = Toolkit.getDefaultToolkit();
if (gridOn) {
System.out.println("Grid works");
g.setColor(Color.white);
for (int i = 0; i < tk.getScreenSize().height; i += 64) {
gridY++;
g.drawLine(0, (64 * gridY), tk.getScreenSize().width, (64 * gridY));
}
}
gridY = -1;
gridX = -1;
if (vertOn) {
System.out.println("vert grid works");
g.setColor(Color.white);
for (int ig = 0; ig < tk.getScreenSize().width; ig += 64) {
gridX++;
g.drawLine((64 * gridX), 0, (64 * gridX), tk.getScreenSize().height);
}
}
if (squareOn) {
System.out.println("Square works");
g.setColor(Color.red);
x = Integer.parseInt(op.squareX.getText());
y = Integer.parseInt(op.squareY.getText());
w = Integer.parseInt(op.squareW.getText());
h = Integer.parseInt(op.squareH.getText());
g.fillRect(x,y,w,h);
}
x = 0;
y = 0;
w = 64;
h = 64;
}
}
public class DragPanel extends JPanel {
OptionPanel op;
public DragPanel(OptionPanel op) {
this.op = op;
this.add(this.op.squareButton);
this.op.squareButton.setActionCommand("square");
}
public void addActionListener(ActionListener listener) {
System.out.println(listener);
this.op.squareButton.addActionListener(listener);
}
}
private static class Square {
}
private class OptionPanel extends JPanel {
public JButton grid;
public JButton vgrid;
public JButton squareButton;
public JTextField squareX;
public JTextField squareY;
public JTextField squareW;
public JTextField squareH;
public int x,y,w,h;
public Square square = new Square();
public OptionPanel() {
//Sets the stuff for the panel
setBackground(new Color(155, 0, 255));
setLayout(new GridBagLayout());
//end
//The Show Grid Button Stuff
grid = new JButton("Show Horizontal Grid");
grid.setActionCommand("grid");
//end
//The vertical grid
vgrid = new JButton("Show Vertical Grid");
vgrid.setActionCommand("vgrid");
//end
//The Square tool button stuff
squareButton = new JButton("Sqaure Tool");
//end
squareX = new JTextField(gridPane.x); //<----- THIS IS WHERE THE PROBLEM IS!!!!!
squareY = new JTextField("1",3);
squareW = new JTextField("1",3);
squareH = new JTextField("1",3);
//The gridbagConstraints things
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
//kind of like padding
gbc.weighty = 1;
//sets the positions
gbc.gridx = 0;
gbc.gridy = 0;
//add it
add(grid, gbc);
//changes position for the second button
gbc.gridx = -1;
gbc.gridy = 0;
// adds it
add(vgrid, gbc);
//end
add(squareX, gbc);
add(squareY, gbc);
add(squareW, gbc);
add(squareH, gbc);
}
public void addActionListener(ActionListener listener) {
//adds action listeners
grid.addActionListener(listener);
vgrid.addActionListener(listener);
squareButton.addActionListener(listener);
squareX.addActionListener(listener);
squareY.addActionListener(listener);
squareW.addActionListener(listener);
squareH.addActionListener(listener);
}
}
Update
I need help with this portion of the code still:
squareX = new JTextField("0" + gridPane.x,3); //<----- THIS IS WHERE THE PROBLEM IS!!!!!
squareY = new JTextField("0" + gridPane.y,3);
squareW = new JTextField("0" + gridPane.w,3);
squareH = new JTextField("0" + gridPane.h,3);
It seems that the main errors are coming from when I try to do something like this...
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
gridPane = new GridPane();
You create the OptionPanel before your create the GridPane, so the gridPane variable is null when your optionPanel tries to access that variable.
Create the gridPane before you create the OptionPanel
Your code should be
gridPane = new GridPane();
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
gridPane.setBorder(BorderFactory.createLineBorder(Color.white));
add(gridPane);
You're first NullPointerException can be fixed by doing something like...
public Game() {
setLayout(new BorderLayout());
gridPane = new GridPane();
gridPane.setBorder(BorderFactory.createLineBorder(Color.white));
OptionPanel options = new OptionPanel();
options.addActionListener(this);
add(options, BorderLayout.NORTH);
add(gridPane);
drag = new DragPanel(options);
drag.setBorder(BorderFactory.createLineBorder(Color.white));
drag.setBackground(new Color(100, 100, 125));
add(drag, BorderLayout.WEST);
}
You're second NullPointerException is more tricky...
You have a cyclic dependency problem...
Class A is trying to create Class B, which is trying to create class C when class C depends on an instance of B...
To clarify...GridPane has an instance of OptionPane, but OptionPane is depended on the state of GridPane, which is null, because GridPane can't initalise until OptionPane has been initialised...confused yet...
You can solve this by using a lazy loading approach. That is, not try to insitate the OptionPane till you need it...
public class GridPane extends JPanel {
public OptionPanel op;
// Other variables...
// Other methods...
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Other paint code...
if (squareOn) {
if (op == null) {
op = new OptionPanel();
}
System.out.println("Square works");
g.setColor(Color.red);
x = Integer.parseInt(op.squareX.getText());
y = Integer.parseInt(op.squareY.getText());
w = Integer.parseInt(op.squareW.getText());
h = Integer.parseInt(op.squareH.getText());
g.fillRect(x, y, w, h);
}
}
}
How ever. I don't think this is going to achieve what you want in the long run, as the state of the OptionPane will be different between the classes...(GridPane and OptionPane won't share the same instance....)
A better solution would be to remove these direct dependencies, so that you can pass some the same reference of some kind of model between GridPane and OptionPane, which would allow it to act as the glue between them...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java swing background image
drawing your own buffered image on frame
I am trying to add a back ground image to my frame, but nothing that I have done works.
I designed a slot machine consisting of several panels added to the container. Now, I am trying to add a nice background to the frame.
I tried using the paint method. But, since I am already using the paint method to paint the reel images, it is not working on the background.
I also tried adding a JLabel, but when I do it overwrites everything or get overwritten depending on how I call it. Following is my code; any help will be much appreciated:
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import sun.audio.*;
public class SlotMachine extends JFrame {
private Container c = getContentPane();
private ImageIcon handleIcon = new ImageIcon("starWars/slot-handle.png");
private ImageIcon quitIcon = new ImageIcon("starWars/quit2.jpg");
private ImageIcon logoIcon = new ImageIcon("starWars/logo3.jpg");
private ImageIcon BG = new ImageIcon("space.jpg");
private JButton spin = new JButton("Spin", handleIcon);
private JButton quit = new JButton("Quit", quitIcon);
private JLabel logo = new JLabel(logoIcon);
private JLabel bankTotal = new JLabel("Empire Total");
private JLabel bankLabel = new JLabel("$1000.00");
private JLabel playerLabel = new JLabel("$1000.00");
private JLabel playerTotal = new JLabel("Rebellion Total");
private Font newFont = new Font("DialogInput",Font.ITALIC, 25);
private JPanel logoPanel = new JPanel(new BorderLayout());
private JPanel moneyPanel = new JPanel(new GridLayout(1, 3, 5, 5));
private JPanel imagePanel;
private JPanel mainPanel = new JPanel(new BorderLayout());
private JPanel bankPanel = new JPanel(new GridLayout(2, 1, 5, 5));
private JPanel playerPanel = new JPanel(new GridLayout(2, 1, 5, 5));
private JPanel panel = new JPanel(new FlowLayout());
private SlotMachine.ReelPanel reel1 = new SlotMachine.ReelPanel();
private SlotMachine.ReelPanel reel2 = new SlotMachine.ReelPanel();
private SlotMachine.ReelPanel reel3 = new SlotMachine.ReelPanel();
private AudioPlayer audioPlayer = AudioPlayer.player;
private AudioDataStream continuousMusic;
private AudioDataStream winMusic;
private AudioDataStream force;
private AudioDataStream force2;
//private AudioDataStream intro;
private ContinuousAudioDataStream audioLoop;
private static final int DELAY = 1000;
private static final double FUNDS = 1000.00;
private static final float PRICE = 1;
private int timerCounter = 0;
private double bank = FUNDS;
private double playerMoney = 1000.00;
private Timer timer = new Timer(DELAY, new SlotMachine.TimeHandler());
public SlotMachine() {
try {
FileInputStream inputStream = new FileInputStream(new File("cantina4.wav"));
AudioStream audioStream = new AudioStream(inputStream);
AudioData audioData = audioStream.getData();
continuousMusic = new AudioDataStream(audioData);
audioLoop = new ContinuousAudioDataStream(audioData);
inputStream = new FileInputStream(new File("Cheer.wav"));
audioStream = new AudioStream(inputStream);
audioData = audioStream.getData();
winMusic = new AudioDataStream(audioData);
inputStream = new FileInputStream(new File("forceNN.wav"));
audioStream = new AudioStream(inputStream);
audioData = audioStream.getData();
force = new AudioDataStream(audioData);
inputStream = new FileInputStream(new File("force2NN.wav"));
audioStream = new AudioStream(inputStream);
audioData = audioStream.getData();
force2 = new AudioDataStream(audioData);
} catch (Exception e) {
e.printStackTrace();
}
audioPlayer.start(force);
// Set the font
spin.setFont(newFont);
quit.setFont(newFont);
bankLabel.setFont(newFont);
bankTotal.setFont(newFont);
playerLabel.setFont(newFont);
playerTotal.setFont(newFont);
// implements start button
spin.setVerticalTextPosition(SwingConstants.BOTTOM);
spin.setHorizontalTextPosition(SwingConstants.CENTER);
spin.setBackground(Color.GREEN);
spin.setForeground(Color.WHITE);
spin.setPreferredSize(new Dimension(100, 370));
spin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
audioPlayer.stop(force);
audioPlayer.stop(force2);
timer.start();
reel1.startAnimation();
reel2.startAnimation();
reel3.startAnimation();
spin.setEnabled(false);
audioPlayer.start(audioLoop);
}
});
// implements quit button
quit.setBackground(Color.RED);
quit.setForeground(Color.WHITE);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
spin.setEnabled(true);
reel1.stopAnimation();
reel2.stopAnimation();
reel3.stopAnimation();
timer.stop();
audioPlayer.stop(continuousMusic);
audioPlayer.stop(audioLoop);
audioPlayer.stop(winMusic);
timerCounter = 0;
audioPlayer.stop(force);
audioPlayer.start(force2);
imagePanel.repaint(); // without this call for repaint, if you press quit but then choose to cancel
// the curent image and the next image would sometimes overlap this repaint may change the images but they do not overlap.
if (JOptionPane.showConfirmDialog(SlotMachine.this,
"Are you sure you want to quit?", "Quit Option",
JOptionPane.OK_CANCEL_OPTION) == 0) {
audioPlayer.start(force2);
System.exit(0);
}
}
});
// create image panel
imagePanel = new JPanel(new GridLayout(1, 3, 15, 15));
imagePanel.setBackground(Color.WHITE);
imagePanel.add(reel1);
imagePanel.add(reel2);
imagePanel.add(reel3);
// create a panel to hold bank values
bankTotal.setForeground(Color.WHITE);
bankLabel.setForeground(Color.WHITE);
bankPanel.setBackground(Color.GRAY);
bankPanel.add(bankTotal);
bankPanel.add(bankLabel);
// panel to hold player values
playerTotal.setForeground(Color.WHITE);
playerLabel.setForeground(Color.WHITE);
playerPanel.setBackground(Color.GRAY);
playerPanel.add(playerTotal);
playerPanel.add(playerLabel);
// create a panel to add bank and player panels and quit button
//moneyPanel.setBackground(Color.BLACK);
moneyPanel.add(bankPanel);
moneyPanel.add(playerPanel);
moneyPanel.add(quit);
moneyPanel.setOpaque(false);
// this panel adds the reel panel and spin button
panel.setPreferredSize(new Dimension(650, 350));
//panel.setBackground(Color.BLACK);
panel.add(imagePanel);
panel.add(spin);
panel.setOpaque(false);
// create the logo panel
logoPanel.add(logo);
//logoPanel.setBackground(Color.BLACK);
logoPanel.setOpaque(false);
mainPanel.add(logoPanel, BorderLayout.NORTH);
mainPanel.add(panel, BorderLayout.CENTER);
mainPanel.add(moneyPanel, BorderLayout.SOUTH);
mainPanel.setOpaque(false);
//////////////////////////////////// background ???????????????????
/// I could just set backgroung black but i want to add a image
add(mainPanel, BorderLayout.CENTER);
setTitle("Slot Machine");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(950, 750);
setResizable(false);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new SlotMachine();
}
public class ReelPanel extends JPanel {
private final static String IMAGE_NAME = "starWars/icon";
protected ImageIcon images[];
private int currentImage = 0;
private final int ANIMATION_DELAY = 150;
private final int TOTAL_IMAGES = 12;
private int width;
private int height;
private Timer animationTimer = new Timer(ANIMATION_DELAY, new SlotMachine.ReelPanel.TimerHandler());
private int index;
public ReelPanel() {
try {
images = new ImageIcon[TOTAL_IMAGES];
for (int count = 0; count < images.length; count++) {
images[ count] = new ImageIcon(IMAGE_NAME + (count + 1) + ".jpg");
}
width = images[ 1].getIconWidth();
height = images[ 1].getIconHeight();
currentImage = 0;
index = 0;
animationTimer.setInitialDelay(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
images[ currentImage].paintIcon(this, g, 0, 0);
if (animationTimer.isRunning()) {
currentImage = (int) (Math.random() * TOTAL_IMAGES);
}
}
public void startAnimation() {
animationTimer.start();
}
public void stopAnimation() {
animationTimer.stop();
}
public int getIndex() {
return index;
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
private class TimerHandler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
repaint();
index = currentImage;
}
}
}
private class TimeHandler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
audioPlayer.stop(winMusic);
++timerCounter;
if (timerCounter == 2) {
reel1.stopAnimation();
} else if (timerCounter == 3) {
reel2.stopAnimation();
} else if (timerCounter == 4) {
reel3.stopAnimation();
audioPlayer.stop(continuousMusic);
audioPlayer.stop(audioLoop);
timerCounter = 0;
timer.stop();
spin.setEnabled(true);
if (reel1.getIndex() == reel2.getIndex() && reel1.getIndex() == reel3.getIndex()) {
if (playerMoney > 0) {
playerMoney += bank;
} else {
playerMoney = bank;
}
bank = FUNDS;
winMusic.reset();
audioPlayer.start(winMusic);
} else {
bank += PRICE;
playerMoney -= PRICE;
}
bankLabel.setText("$" + bank + 0);
playerLabel.setText("$" + playerMoney + 0);
if (playerMoney <= 0) {
JOptionPane.showMessageDialog(SlotMachine.this,
"You are out of funds. GAME IS OVER", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
}
}
}
You can simply override paintComponent for your mainPanel and draw the background image in that method. You should choose the appropriate strategy for painting your image (stretch it, keep aspect ratio, repeat horizontally/vertically) but that should not be too hard.
Here is an example that stretches the image over the content pane.
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestBackgroundImage {
private static final String BACKHGROUND_IMAGE_URL = "http://cache2.allpostersimages.com/p/LRG/27/2740/AEPND00Z/affiches/blue-fiber-optic-wires-against-black-background.jpg";
protected void initUI() throws MalformedURLException {
JFrame frame = new JFrame(TestBackgroundImage.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ImageIcon backgroundImage = new ImageIcon(new URL(BACKHGROUND_IMAGE_URL));
JPanel mainPanel = new JPanel(new BorderLayout()) {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImage.getImage(), 0, 0, getWidth(), getHeight(), this);
}
#Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width = Math.max(backgroundImage.getIconWidth(), size.width);
size.height = Math.max(backgroundImage.getIconHeight(), size.height);
return size;
}
};
mainPanel.add(new JButton("A button"), BorderLayout.WEST);
frame.add(mainPanel);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
new TestBackgroundImage().initUI();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
Set the layout of your main panel to BorderLayout
Create a JLabel and add it to you main panel
Set the image icon of the label using your background image
Set the layout of the label to what ever you want to use
Continue adding your components to the label as you normally would
An example can be found here
I have a applet that simulates a coffee vending machine. I am trying to get the text Ready to be display for 5 secs. But it doesn't seem to be working. can anyone tell me what's wrong with my timer ?
Class qn1 is the class for JApplet.
init() will be called to instantiate the variables.
I add actionlistener to the 2 buttons so they will react when i click them.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class qn1 extends JApplet implements ActionListener {
private static int FULL = 4;
private int coffees = FULL;
private JPanel p;
private JButton jbtw, jbtb;
private Dispenser dis;
private JLabel jlbl;
private Timer timer;
public void init() {
setLayout(new BorderLayout(5, 5));
setSize(400, 500);
p = new JPanel(new GridLayout(1, 2));
jbtw = new JButton("White");
jbtb = new JButton("Black");
jbtw.addActionListener(this);
jbtb.addActionListener(this);
p.add(jbtw);
p.add(jbtb);
add(p, BorderLayout.NORTH);
dis = new Dispenser();
add(dis, BorderLayout.CENTER);
jlbl = new JLabel("Select", SwingConstants.CENTER);
add(jlbl, BorderLayout.SOUTH);
timer = new Timer(50, this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jbtb) | e.getSource().equals(jbtw)) {
if (coffees - 1 >= 0) {
coffees--;
jlbl.setText("Ready " + coffees);
}
timer.setInitialDelay(5000);
timer.start();
if (coffees == 0) {
jlbl.setText("Empty");
} else {
jlbl.setText("Select" + coffees);
}
}
}
}
class Dispenser extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, 25, 50);
g.drawLine(25, 50, 55, 50);
g.drawLine(55, 50, 80, 0);
}
}
Your timer is called, but if the source is not one of your button, you don't perform anything:
if (e.getSource().equals(jbtb) | e.getSource().equals(jbtw)) {
To answer your comments, you might want something like that:
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jbtb) | e.getSource().equals(jbtw)) {
timer.setInitialDelay(5000);
timer.start();
if (coffees == 0) {
jlbl.setText("Empty");
} else {
jlbl.setText("Select " + coffees);
}
}
else {
if (coffees - 1 >= 0) {
coffees--;
jlbl.setText("Ready " + coffees);
}
}
}
I have a list of N JSliders (N does not change procedurally, only as I add more features. Currently N equals 4). The sum of all the sliders values must equal to 100. As one slider moves the rest of the sliders shall adjust. Each slider has values that range from 0 to 100.
Currently I am using this logic when a slider is changed (pseudo-code):
newValue = currentSlider.getValue
otherSliders = allSliders sans currentSlider
othersValue = summation of otherSliders values
properOthersValue = 100 - newValue
ratio = properOthersValue / othersValue
for slider in otherSlider
slider.value = slider.getValue * ratio
The problem with this setup is slider's values are stored as ints. So as I adjust the sliders I get precision problems: sliders will twitch or not move at all depending on the ratio value. Also the total value does not always add up to 100.
Does anyone have a solution to this problem without creating an entirely new JSlider class that supports floats or doubles?
If you want an example of the behavior I want, visit: Humble Indie Bundle and scroll to the bottom of the page.
thank you
p.s. Multiplying the values by the ratio allows for the user to 'lock' values at 0. However, I am not sure what to do when 3 of the 4 sliders are at 0 and the 4th slider is at 100 and I move the 4th slider down. Using the logic above, the 3 sliders with 0 as their value stay put and the 4th slider moves to where the user puts it, which makes the total less than 100, which is improper behavior.
EDIT
Here is the SSCCE:
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.LinkedList;
public class SliderDemo
{
static LinkedList<JSlider> sliders = new LinkedList<JSlider>();
static class SliderListener implements ChangeListener
{
boolean updating = false;
public void stateChanged(ChangeEvent e)
{
if (updating) return;
updating = true;
JSlider source = (JSlider)e.getSource();
int newValue = source.getValue();
LinkedList<JSlider> otherSliders = new LinkedList<JSlider>(sliders);
otherSliders.remove(source);
int otherValue = 0;
for (JSlider slider : otherSliders)
{
otherValue += slider.getValue();
}
int properValue = 100 - newValue;
double ratio = properValue / (double)otherValue;
for (JSlider slider : otherSliders)
{
int currentValue = slider.getValue();
int updatedValue = (int) (currentValue * ratio);
slider.setValue(updatedValue);
}
int total = 0;
for (JSlider slider : sliders)
{
total += slider.getValue();
}
System.out.println("Total = " + total);
updating = false;
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame("SliderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = frame.getContentPane();
JPanel sliderPanel = new JPanel(new GridBagLayout());
container.add(sliderPanel);
SliderListener listener = new SliderListener();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
int sliderCount = 4;
int initial = 100 / sliderCount;
for (int i = 0; i < sliderCount; i++)
{
gbc.gridy = i;
JSlider slider = new JSlider(0, 100, initial);
slider.addChangeListener(listener);
slider.setMajorTickSpacing(50);
slider.setPaintTicks(true);
sliders.add(slider);
sliderPanel.add(slider, gbc);
}
frame.pack();
frame.setVisible(true);
}
}
Why not making the granularity of the JSlider models finer by say having them go from 0 to 1000000, and having the sum be 1000000? With the proper Dictionary for the LabelTable, the user will probably not know that it doesn't go from 0 to 100.
For example:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
#SuppressWarnings("serial")
public class LinkedSliders2 extends JPanel {
private static final int SLIDER_COUNT = 5;
public static final int SLIDER_MAX_VALUE = 1000;
private static final int MAJOR_TICK_DIVISIONS = 5;
private static final int MINOR_TICK_DIVISIONS = 20;
private static final int LS_WIDTH = 700;
private static final int LS_HEIGHT = 500;
private JSlider[] sliders = new JSlider[SLIDER_COUNT];
private SliderGroup2 sliderGroup = new SliderGroup2(SLIDER_MAX_VALUE);
public LinkedSliders2() {
Dictionary<Integer, JComponent> myDictionary = new Hashtable<Integer, JComponent>();
for (int i = 0; i <= MAJOR_TICK_DIVISIONS; i++) {
Integer key = i * SLIDER_MAX_VALUE / MAJOR_TICK_DIVISIONS;
JLabel value = new JLabel(String.valueOf(i * 100 / MAJOR_TICK_DIVISIONS));
myDictionary.put(key, value);
}
setLayout(new GridLayout(0, 1));
for (int i = 0; i < sliders.length; i++) {
sliders[i] = new JSlider(0, SLIDER_MAX_VALUE, SLIDER_MAX_VALUE
/ SLIDER_COUNT);
sliders[i].setLabelTable(myDictionary );
sliders[i].setMajorTickSpacing(SLIDER_MAX_VALUE / MAJOR_TICK_DIVISIONS);
sliders[i].setMinorTickSpacing(SLIDER_MAX_VALUE / MINOR_TICK_DIVISIONS);
sliders[i].setPaintLabels(true);
sliders[i].setPaintTicks(true);
sliders[i].setPaintTrack(true);
sliderGroup.addSlider(sliders[i]);
add(sliders[i]);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(LS_WIDTH, LS_HEIGHT);
}
private static void createAndShowGui() {
LinkedSliders2 mainPanel = new LinkedSliders2();
JFrame frame = new JFrame("LinkedSliders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class SliderGroup2 {
private List<BoundedRangeModel> sliderModelList = new ArrayList<BoundedRangeModel>();
private ChangeListener changeListener = new SliderModelListener();
private int maxValueSum;
public SliderGroup2(int maxValueSum) {
this.maxValueSum = maxValueSum;
}
public void addSlider(JSlider slider) {
BoundedRangeModel model = slider.getModel();
sliderModelList.add(model);
model.addChangeListener(changeListener);
}
private class SliderModelListener implements ChangeListener {
private boolean internalChange = false;
#Override
public void stateChanged(ChangeEvent cEvt) {
if (!internalChange) {
internalChange = true;
BoundedRangeModel sourceModel = (BoundedRangeModel) cEvt.getSource();
int sourceValue = sourceModel.getValue();
int oldSumOfOtherSliders = 0;
for (BoundedRangeModel model : sliderModelList) {
if (model != sourceModel) {
oldSumOfOtherSliders += model.getValue();
}
}
if (oldSumOfOtherSliders == 0) {
for (BoundedRangeModel model : sliderModelList) {
if (model != sourceModel) {
model.setValue(1);
}
}
internalChange = false;
return;
}
int newSumOfOtherSliders = maxValueSum - sourceValue;
for (BoundedRangeModel model : sliderModelList) {
if (model != sourceModel) {
long newValue = ((long) newSumOfOtherSliders * model
.getValue()) / oldSumOfOtherSliders;
model.setValue((int) newValue);
}
}
int total = 0;
for (BoundedRangeModel model : sliderModelList) {
total += model.getValue();
}
//!! System.out.printf("Total = %.0f%n", (double)total * 100 / LinkedSliders2.SLIDER_MAX_VALUE);
internalChange = false;
}
}
}
}
Edited to have SliderGroup2 use a List of BoundedRangeModels rather than JSliders.
sliders will twitch or not move at all depending on the ratio value.
HumbleBundle has the same problem. If you move the slider by the keyboard then the change is only 1, which means it will only ever go to the first slider. So you ratios will eventually get out of sync.
Also the total value does not always add up to 100.
So you need to do a rounding check. If it doesn't add to 100, then you need to decide where the error goes. Maybe the last slider given the above problem?
I am not sure what to do when 3 of the 4 sliders are at 0 and the 4th slider is at 100 and I move the 4th slider down.
The way HumbleBundle handles it is to move all the slicers. However it only allows you to move the slider down increments of 3, so that you can increase each of the 3 sliders by 1.
Even the implementation at HumbleBundle isn't perfect.
Borrowing from some of Hovercrafts solution I came up with a different approach. The basis of this approach is that the "other sliders" values are tracked at the time a slider is moved. As long as you continue to slide the same slider the frozen values are used to calculate the new values. Any rounding differences are then applied sequentially to each slider until the difference is used up. Using this approach you can have incremental changes in the slider applied evenly to all of the other sliders.
The values in the model are the actual values of the slider and you can also use the keyboard to adjust the sliders:
import java.awt.*;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SliderGroup implements ChangeListener
{
private List<JSlider> sliders = new ArrayList<JSlider>();
private int groupSum;
private boolean internalChange = false;
private JSlider previousSlider;
private List<SliderInfo> otherSliders = new ArrayList<SliderInfo>();
public SliderGroup(int groupSum)
{
this.groupSum = groupSum;
}
public void addSlider(JSlider slider)
{
sliders.add(slider);
slider.addChangeListener(this);
}
#Override
public void stateChanged(ChangeEvent e)
{
if (internalChange) return;
internalChange = true;
JSlider sourceSlider = (JSlider)e.getSource();
if (previousSlider != sourceSlider)
{
setupForSliding(sourceSlider);
previousSlider = sourceSlider;
}
int newSumOfOtherSliders = groupSum - sourceSlider.getValue();
int oldSumOfOtherSliders = 0;
for (SliderInfo info : otherSliders)
{
JSlider slider = info.getSlider();
if (slider != sourceSlider)
{
oldSumOfOtherSliders += info.getValue();
}
}
int difference = newSumOfOtherSliders - oldSumOfOtherSliders;
if (oldSumOfOtherSliders == 0)
{
resetOtherSliders( difference / otherSliders.size() );
allocateDifference(difference % otherSliders.size(), true);
internalChange = false;
return;
}
double ratio = (double)newSumOfOtherSliders / oldSumOfOtherSliders;
for (SliderInfo info : otherSliders)
{
JSlider slider = info.getSlider();
int oldValue = info.getValue();
int newValue = (int)Math.round(oldValue * ratio);
difference += oldValue - newValue;
slider.getModel().setValue( newValue );
}
if (difference != 0)
{
allocateDifference(difference, false);
}
internalChange = false;
}
private void allocateDifference(int difference, boolean adjustZeroValue)
{
while (difference != 0)
{
for (SliderInfo info : otherSliders)
{
if (info.getValue() != 0 || adjustZeroValue)
{
JSlider slider = info.getSlider();
if (difference > 0)
{
slider.getModel().setValue( slider.getValue() + 1 );
difference--;
}
if (difference < 0)
{
slider.getModel().setValue( slider.getValue() - 1 );
difference++;
}
}
}
}
}
private void resetOtherSliders(int resetValue)
{
for (SliderInfo info : otherSliders)
{
JSlider slider = info.getSlider();
slider.getModel().setValue( resetValue );
}
}
private void setupForSliding(JSlider sourceSlider)
{
otherSliders.clear();
for (JSlider slider: sliders)
{
if (slider != sourceSlider)
{
otherSliders.add( new SliderInfo(slider, slider.getValue() ) );
}
}
}
class SliderInfo
{
private JSlider slider;
private int value;
public SliderInfo(JSlider slider, int value)
{
this.slider = slider;
this.value = value;
}
public JSlider getSlider()
{
return slider;
}
public int getValue()
{
return value;
}
}
private static JPanel createSliderPanel(int groupSum, int sliderCount)
{
int sliderValue = groupSum / sliderCount;
SliderGroup sg = new SliderGroup(groupSum);
JPanel panel = new JPanel( new BorderLayout() );
JPanel sliderPanel = new JPanel( new GridLayout(0, 1) );
panel.add(sliderPanel, BorderLayout.CENTER);
JPanel labelPanel = new JPanel( new GridLayout(0, 1) );
panel.add(labelPanel, BorderLayout.EAST);
for (int i = 0; i < sliderCount; i++)
{
JLabel label = new JLabel();
label.setText( Integer.toString(sliderValue) );
labelPanel.add( label );
JSlider slider = new JSlider(0, groupSum, sliderValue);
slider.setMajorTickSpacing(25);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setPaintTrack(true);
slider.addChangeListener( new LabelChangeListener(label) );
sliderPanel.add( slider );
sg.addSlider( slider );
}
return panel;
}
static class LabelChangeListener implements ChangeListener
{
private JLabel label;
public LabelChangeListener(JLabel label)
{
this.label = label;
}
#Override
public void stateChanged(ChangeEvent e)
{
JSlider slider = (JSlider)e.getSource();
label.setText( Integer.toString(slider.getValue()) );
}
}
private static void createAndShowGui()
{
JPanel panel = createSliderPanel(100, 5);
JFrame frame = new JFrame("SliderGroup");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}