Simon Says won't show sequence - java

Below is a Simon Says program I am working on. Right now it only displays a gray frame. I added in a keyListener to see if i could make the arcs light up.I wanted to display a flash animation sequence. Why isn't this working?
public class SimonShape extends JFrame implements KeyListener, ActionListener {
private JFrame f;
private JPanel p;
public static void main(String[] args) {
new SimonShape();
}
public SimonShape() {
f = new JFrame("Simon Says");
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawStuff draw = new DrawStuff();
p = new JPanel();
p.setBackground(Color.GRAY);
p.setLayout(new BorderLayout());
draw.playSequence();
p.add(draw, BorderLayout.CENTER);
// initiates the sequence
f.add(p);
f.addKeyListener(this);
f.setLocationRelativeTo(null); // positions the frame in the middle of
// the screen
f.setVisible(true);
}
public class DrawStuff extends JComponent {
Color COLOR1;
Color COLOR2;
Color COLOR3;
Color COLOR4;
public void playSequence() {
ArrayList<Integer> Computer = new ArrayList<Integer>();
ArrayList<Integer> Player = new ArrayList<Integer>();
int compPick, compPick2, compPick3, compPick4;
Random gen = new Random();
compPick = gen.nextInt(4);
compPick2 = gen.nextInt(4);
compPick3 = gen.nextInt(4);
compPick4 = gen.nextInt(4);
Computer.add(compPick);
Computer.add(compPick4);
Computer.add(compPick2);
Computer.add(compPick3);
for (int i = 0; i < Computer.size(); i++) {
if (Computer.get(i) == 0) {
COLOR1 = Color.GREEN.brighter();
repaint();
} else if (Computer.get(i) == 1) {
COLOR2 = Color.BLUE.darker();
repaint();
} else if (Computer.get(i) == 2) {
COLOR3 = Color.RED.darker();
repaint();
} else if (Computer.get(i) == 3) {
COLOR4 = Color.YELLOW.brighter();
repaint();
}
}
}
}
public int flash = 0;
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Graphics2D g3 = (Graphics2D) g;
Graphics2D g4 = (Graphics2D) g;
Graphics2D g5 = (Graphics2D) g;
// assume d == 145 && e == 90
if (flash == 1) {
g2.setPaint(Color.GREEN);
} else {
g2.setPaint(Color.GREEN.darker());
}
g2.fill(new Arc2D.Double(150, 150, 200, 200, 145, 90, Arc2D.PIE));
if (flash == 2) {
g3.setPaint(Color.BLUE);
} else {
g3.setPaint(Color.BLUE.darker());
}
g3.fill(new Arc2D.Double(150, 150, 200, 200, 235, 90, Arc2D.PIE));
if (flash == 3) {
g4.setPaint(Color.RED);
} else {
g4.setPaint(Color.RED.darker());
}
g4.fill(new Arc2D.Double(150, 150, 200, 200, 325, 90, Arc2D.PIE));
if (flash == 4) {
g5.setPaint(Color.YELLOW);
} else {
g4.setPaint(Color.YELLOW.darker());
}
g5.fill(new Arc2D.Double(150, 150, 200, 200, 55, 90, Arc2D.PIE));
}
public void keyPressed(KeyEvent e) {
int event = e.getKeyCode();
if (event == KeyEvent.VK_RIGHT) {
flash = 1;
}
if (event == KeyEvent.VK_DOWN) {
flash = 2;
}
if (event == KeyEvent.VK_LEFT) {
flash = 3;
}
if (event == KeyEvent.VK_UP) {
flash = 4;
}
}
#Override
public void keyTyped(KeyEvent e) {//not used
}
#Override
public void keyReleased(KeyEvent e) {//not used
}
#Override
public void actionPerformed(ActionEvent e) {//not used
}
}

When creating any Swing GUI, you should always use the model / view / controller pattern. This pattern allows you to separate your concerns and focus on one part of the GUI at a time.
Divide and conquer.
Here's the GUI I created.
The first thing I did was create a model for the game. The GameModel class is a plain Java object that holds the computer sequence and the player sequence.
public class GameModel {
private List<Integer> computerSequence;
private List<Integer> playerSequence;
private Random random;
public GameModel() {
this.computerSequence = new ArrayList<Integer>();
this.playerSequence = new ArrayList<Integer>();
this.random = new Random();
}
public void addToComputerSequence() {
computerSequence.add(Integer.valueOf(random.nextInt(4)));
}
public void clearComputerSequence() {
computerSequence.clear();
}
public List<Integer> getComputerSequence() {
return computerSequence;
}
public void clearPlayerSequence() {
playerSequence.clear();
}
public void addToPlayerSequence(int number) {
playerSequence.add(Integer.valueOf(number));
}
public boolean doSequencesMatch() {
if (computerSequence.size() == playerSequence.size()) {
for (int i = 0; i < computerSequence.size(); i++) {
int computer = computerSequence.get(i);
int player = playerSequence.get(i);
if (computer != player) {
return false;
}
}
return true;
}
return false;
}
}
The model class allows us to add to the computer sequence, add to the player sequence, and determine if the computer and player sequence match.
Next, we need a model class to hold the 4 slices of the circle. The ArcModel class is another plain Java object that holds a slice.
public class ArcModel {
private final int closureType;
private final double startingAngle;
private final double extent;
private Color color;
private final Color originalColor;
private final Rectangle rectangle;
public ArcModel(Color color, Rectangle rectangle, double startingAngle,
double extent, int closureType) {
this.color = color;
this.originalColor = color;
this.rectangle = rectangle;
this.startingAngle = startingAngle;
this.extent = extent;
this.closureType = closureType;
}
public int getClosureType() {
return closureType;
}
public double getStartingAngle() {
return startingAngle;
}
public double getExtent() {
return extent;
}
public Rectangle getRectangle() {
return rectangle;
}
public Color getColor() {
return color;
}
public void brighterColor() {
this.color = Color.WHITE;
}
public void darkerColor() {
this.color = originalColor;
}
}
In addition to the getters and setters, we have a method to brighten the color and a method to darken the color. I've set the bright color to white to make it more easily visible.
Now that we've created the model classes, let's look at the view classes. The first view class is the DrawingPanel class.
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 70146219705119575L;
private List<ArcModel> segments;
public DrawingPanel() {
this.segments = new ArrayList<ArcModel>();
int margin = 50;
int diameter = 300;
Rectangle r = new Rectangle(margin, margin, diameter, diameter);
segments.add(new ArcModel(Color.GREEN, r, 180, 90, Arc2D.PIE));
segments.add(new ArcModel(Color.BLUE, r, 270, 90, Arc2D.PIE));
segments.add(new ArcModel(Color.RED, r, 360, 90, Arc2D.PIE));
segments.add(new ArcModel(Color.YELLOW, r, 90, 90, Arc2D.PIE));
int width = diameter + margin + margin;
this.setPreferredSize(new Dimension(width, width));
}
public void brighterArcModelColor(int index) {
segments.get(index).brighterColor();
repaint();
}
public void darkerArcModelColor(int index) {
segments.get(index).darkerColor();
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (ArcModel arcModel : segments) {
g2d.setPaint(arcModel.getColor());
Rectangle r = arcModel.getRectangle();
g2d.fill(new Arc2D.Double(r.getX(), r.getY(), r.getWidth(), r
.getHeight(), arcModel.getStartingAngle(), arcModel
.getExtent(), arcModel.getClosureType()));
}
}
}
Here, we create a List of ArcModel segments. We set the size of the drawing panel based on the margin and diameter of the circle we want to create with the segments.
We have two methods, one for brightening a color of a segment, and another for darkening a color of a segment.
We do the drawing in the paintComponent method. Since we created the ArcModel class, the actual drawing is straightforward. The paintComponent method does nothing but draw the pie slices of the circle.
Next, we look at the main SimonShape class. This class creates the game model and creates the GUI.
public class SimonShape implements Runnable {
private GameModel gameModel;
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimonShape());
}
public SimonShape() {
this.gameModel = new GameModel();
}
#Override
public void run() {
frame = new JFrame("Simon Says");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel drawingPanel = new DrawingPanel();
frame.add(drawingPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
GameRunnable runnable = new GameRunnable(drawingPanel, gameModel);
new Thread(runnable).start();
}
}
The last two lines of the run method create the animation.
The controller class is the GameRunnable class. I wrote enough code to have the computer pick 10 random segments, and display the sequence of the segments. I'm leaving the rest of the game code up to you. It will go in the GameRunnable class.
public class GameRunnable implements Runnable {
private volatile boolean running;
private DrawingPanel drawingPanel;
private GameModel gameModel;
public GameRunnable(DrawingPanel drawingPanel, GameModel gameModel) {
this.drawingPanel = drawingPanel;
this.gameModel = gameModel;
}
#Override
public void run() {
running = true;
while (running && gameModel.getComputerSequence().size() < 10) {
generateComputerSequence();
sleep(1800L);
}
}
private void generateComputerSequence() {
gameModel.addToComputerSequence();
for (Integer index : gameModel.getComputerSequence()) {
drawingPanel.brighterArcModelColor(index);
sleep(1000L);
drawingPanel.darkerArcModelColor(index);
sleep(200L);
}
}
private void sleep(long duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
}
}
public synchronized void setRunning(boolean running) {
this.running = running;
}
}
Remember, divide and conquer.

Related

Change thumb and color of JSlider?

I am creating a music player program.
I have created the seek bar using JSlider
Code:
JSlider seek = new JSlider(JProgressBar.HORIZONTAL);
seek.setOpaque(true);
seek.setMajorTickSpacing(0);
seek.setMinorTickSpacing(0);
seek.setBackground(Color.DARK_GRAY);
seek.setSize(100, 13);
seek.setLocation(6, 30);
Currently, it looks like this :
I can only change the background of JSlider using setBackground() method.
I don't have any idea about how to change the thumb colour, thumb shape, track colour, etc.
I want my seek bar to look something like this :
How can I achieve this?
If not possible with JSlider, is it possible to create a JProgressBar which has a slidable thumb?
As was already mentioned in the comments there is no way for you to change the appearance of the slider without extending an existing implementation of SliderUI. Here is an example implementation of how one could achieve the visuals from your demo picture.
Note that hard coding the sizes and colours isn't the best approach and for a real implementation should be handled by setting and using values available by the UIManager.
class Scratch {
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
JPanel content = new JPanel(new BorderLayout());
content.setPreferredSize(new Dimension(300, 100));
JSlider slider = new JSlider() {
#Override
public void updateUI() {
setUI(new CustomSliderUI(this));
}
};
content.add(slider);
JFrame frame = new JFrame();
frame.setContentPane(content);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
private static class CustomSliderUI extends BasicSliderUI {
private static final int TRACK_HEIGHT = 8;
private static final int TRACK_WIDTH = 8;
private static final int TRACK_ARC = 5;
private static final Dimension THUMB_SIZE = new Dimension(20, 20);
private final RoundRectangle2D.Float trackShape = new RoundRectangle2D.Float();
public CustomSliderUI(final JSlider b) {
super(b);
}
#Override
protected void calculateTrackRect() {
super.calculateTrackRect();
if (isHorizontal()) {
trackRect.y = trackRect.y + (trackRect.height - TRACK_HEIGHT) / 2;
trackRect.height = TRACK_HEIGHT;
} else {
trackRect.x = trackRect.x + (trackRect.width - TRACK_WIDTH) / 2;
trackRect.width = TRACK_WIDTH;
}
trackShape.setRoundRect(trackRect.x, trackRect.y, trackRect.width, trackRect.height, TRACK_ARC, TRACK_ARC);
}
#Override
protected void calculateThumbLocation() {
super.calculateThumbLocation();
if (isHorizontal()) {
thumbRect.y = trackRect.y + (trackRect.height - thumbRect.height) / 2;
} else {
thumbRect.x = trackRect.x + (trackRect.width - thumbRect.width) / 2;
}
}
#Override
protected Dimension getThumbSize() {
return THUMB_SIZE;
}
private boolean isHorizontal() {
return slider.getOrientation() == JSlider.HORIZONTAL;
}
#Override
public void paint(final Graphics g, final JComponent c) {
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
super.paint(g, c);
}
#Override
public void paintTrack(final Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Shape clip = g2.getClip();
boolean horizontal = isHorizontal();
boolean inverted = slider.getInverted();
// Paint shadow.
g2.setColor(new Color(170, 170 ,170));
g2.fill(trackShape);
// Paint track background.
g2.setColor(new Color(200, 200 ,200));
g2.setClip(trackShape);
trackShape.y += 1;
g2.fill(trackShape);
trackShape.y = trackRect.y;
g2.setClip(clip);
// Paint selected track.
if (horizontal) {
boolean ltr = slider.getComponentOrientation().isLeftToRight();
if (ltr) inverted = !inverted;
int thumbPos = thumbRect.x + thumbRect.width / 2;
if (inverted) {
g2.clipRect(0, 0, thumbPos, slider.getHeight());
} else {
g2.clipRect(thumbPos, 0, slider.getWidth() - thumbPos, slider.getHeight());
}
} else {
int thumbPos = thumbRect.y + thumbRect.height / 2;
if (inverted) {
g2.clipRect(0, 0, slider.getHeight(), thumbPos);
} else {
g2.clipRect(0, thumbPos, slider.getWidth(), slider.getHeight() - thumbPos);
}
}
g2.setColor(Color.ORANGE);
g2.fill(trackShape);
g2.setClip(clip);
}
#Override
public void paintThumb(final Graphics g) {
g.setColor(new Color(246, 146, 36));
g.fillOval(thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height);
}
#Override
public void paintFocus(final Graphics g) {}
}
}
Result:

Program randomly does not display on startup

I'm making a game in Swing and I'm running into an error where randomly, when opening the program, nothing displays. There's seemingly nothing that triggers it, but I have no idea what could possibly be causing it. And also this is everything in my program that seems to be the problem, Since when i tested the source of the problem, it seems to be that the problem is that it is not rendering anything. My update method is working, but the paint method is not running.
Here is the gameHandler class that loads everything and where I believe the problem could be occurring, if this isn't enough to locate the error, please ask me to add whatever it is you would like to see.
public class GameHandler
{
private GamePanel gp;
private Player p;
private StarManager sm;
private EnemyManager em;
private UI ui;
private BulletManager bm;
private PANEL state;
private MenuPanel mp;
public GameHandler(GamePanel gp, Core c)
{
this.gp = gp;
p = new Player(c.getWidth()/2 - 50/2, c.getHeight() - c.getHeight()/4, 25, 50, c);
sm = new StarManager(50, c.getWidth(), c.getHeight(), 15);
em = new EnemyManager(gp, c, p);
bm = new BulletManager(4, p, em);
new GameInput(c, p, bm);
ui = new UI(p);
mp = new MenuPanel(c);
}
public void update()
{
state = gp.getState();
sm.update();
if(state == PANEL.GAME)
{
p.update();
em.update();
bm.update();
}
}
public void render(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
sm.render(g);
if(state == PANEL.GAME)
{
p.render(g);
bm.render(g);
em.render(g);
ui.render(g);
}
else if(state == PANEL.MENU)
{
mp.render(g);
}
}
}
public class Core extends JComponent implements Runnable
{
private static final long serialVersionUID = 1L;
private int
WIDTH = 800,
HEIGHT = 800;
public boolean running;
public GamePanel gp;
public Core()
{
new Display(WIDTH, HEIGHT, "Geometric Space Invasion", this);
init();
}
public void init()
{
gp = new GamePanel(this);
setLayout(new BorderLayout());
add(gp, BorderLayout.CENTER);
Thread t = new Thread(this);
running = true;
t.start();
}
public static void main(String[] args)
{
new Core();
}
#Override
public void run()
{
while(running)
{
try
{
Thread.sleep(7);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
gp.update();
gp.repaint();
}
}
public int getWidth()
{ return WIDTH; }
public int getHeight()
{ return HEIGHT; }
}
public class GamePanel extends JPanel
{
private static final long serialVersionUID = 1L;
private GameHandler gh;
private PANEL state = PANEL.MENU;
private boolean gameOver;
public GamePanel(Core c)
{
setBackground(Color.BLACK);
gameOver = false;
gh = new GameHandler(this, c);
}
public void update()
{
if(gameOver)
state = PANEL.MENU;
gh.update();
}
public enum PANEL
{
MENU,
GAME
/* THIS IS HOW TO CREATE A PANEL VAIRABLE AND CHECK
private PANEL test = PANEL.MENU;
if(test == PANEL.MENU)
//Do whatever.
*/
}
public enum DIFFICULTY
{
EASY,
MEDIUM,
HARD
}
public DIFFICULTY diff = DIFFICULTY.MEDIUM; //TODO : REMOVE
public void paintComponent(Graphics g)
{
super.paintComponent(g);
gh.render(g);
}
public DIFFICULTY getDifficulty()
{
return diff;
}
public PANEL getState()
{
return state;
}
public void setGameOver(boolean gameOver)
{
this.gameOver = gameOver;
}
}
EDIT: FIXED. I fixed it by switching the two lines in the constructor for the Core class.
public Core()
{
new Display(WIDTH, HEIGHT, "Geometric Space Invasion", this);
init();
}
became
public Core()
{
init();
new Display(WIDTH, HEIGHT, "Geometric Space Invasion", this);
}

On scrolling dynamically generated graphics using JPanel in JScrollPane, the scrolled portion graphics disappears

outputImage
Everything works fine but when i scroll while the graphics is being generated, the scrolled portion graphics disappears
MainApp.java
public abstract class MainApp implements ActionListener{
//Just Listing the method that triggers Canvas(extends JPanel and starts painting)
public void startGeneration(){
String rule = (String) cb.getSelectedItem();
Jexception.setVisible(false);
Jexception1.setVisible(false);
if(genNum > 0){
cgs = new CAGenerationSet(genNum, rule);
cAGenList = new ArrayList<>();
cAGenList = cgs.run(cgs);
if(sp!= null) {
frame.remove(sp);
if(canvas!=null) sp.remove(canvas);
}
//initializing canvas and adding it to the JScrollPane
canvas = new Canvas(cAGenList);
sp = new JScrollPane();
splitPane.setDividerLocation(80);
splitPane.setRightComponent(sp);
sp.setVisible(true);
EventQueue.invokeLater(new Runnable() {
public void run() {
sp.setViewportView(canvas);
sp.revalidate();
}
});
}
else {
Jexception.setVisible(false);
Jexception1.setVisible(true);
}
}
}
Canvas extends JPanel. Here I am overriding the paintComponent() and calling the draw method that triggers the SwingWorker to draw the graphics.
Canvas.java
public class Canvas extends JPanel {
ArrayList<CAGeneration> cAGenList;
private int y,x;
private Thread t;
public static SwingWorker<Void, Void> worker;
private int count = 0;
private boolean check = false;
public CACanvas(ArrayList<CAGeneration> cAGenList) {
this.cAGenList = cAGenList;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
revalidate();
if(!check){
drawCA(g, this.cAGenList);
}
}
private void drawCA(Graphics g, ArrayList<CAGeneration> cAGenList ) {
worker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
Graphics2D g2d = (Graphics2D) g;
check = true;
y= 10;
count = 0;
synchronized (cAGenList) {
for(CAGeneration cg: cAGenList){
count++;
y = y+10;
x = 0;
if(!isCancelled()){
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
for(int i=0; i<cg.getcACell().length; i++){
x = x+10;
if (cg.getcACell()[i] == 0) {
paintRect(g2d, x, y, 9, Color.WHITE);
}
if (cg.getcACell()[i] == 1) {
paintRect(g2d, x, y, 9, Color.GRAY);
}
if (cg.getcACell()[i] == 2) {
paintRect(g2d, x, y, 9, Color.BLACK);
}
}
}
}
}
check = false;
return null;
}
};
ExecutorService threadPool = Executors.newSingleThreadExecutor();
threadPool.submit(worker);
}
private void paintRect(Graphics2D g2D, int x, int y, int size, Color color){
if(g2D!=null) g2D = (Graphics2D) getGraphics();
g2D.setColor(color);
g2D.fillRect(x, y, size, size);
}
}

Background image in different place every time

I have a large program that I will post some classes of and hopefully you guys can find the problem. Basically, sometimes when I start it, it creates the game just fine, and others the background is up a few pixels to the north and west directions leaving very unsightly whitespace. I cannot seem to find the missing piece of code that decides whether not it does this. It honestly feel like some kind of rendering glitch on my machine. At any rate, I have put a background getX and getY method in for debugging and have noticed that whether the background is fully stretched to the screen(its a custom background so the pixel height and width match perfectly), or its up and to the left, the background still reads that it is displaying at (0,0). I will post all the methods from the main thread to the creating of the background in the menu. I will leave notes indicating the path it takes through this code that gets it to creating the background. Thank you for your help and I will check in regularly for edits and more information.
EDIT: added background.java
EDIT2: added pictures explaining problem
Menu.java *ignore the FileIO code, the main point is the creation of a new GamePanel()
public class Menu {
private static File file;
public static void main(String[] args) throws IOException {
file = new File("saves.txt");
if(file.exists()){
FileIO.run();
FileIO.profileChoose();
}
else{
FileIO.profileCreate();
FileIO.run();
}
JFrame window = new JFrame("Jolly Jackpot Land");
window.setContentPane(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
Next is the GamePanel.java
public class GamePanel extends JPanel implements Runnable, KeyListener {
// ID
private static final long serialVersionUID = 1L;
// Dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
// Thread
private Thread thread;
private boolean running;
private int FPS = 30;
private long targetTime = 1000 / FPS;
// Image
private BufferedImage image;
private Graphics2D g;
// Game State Manager
private GameStateManager gsm;
public GamePanel() {
super();
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setFocusable(true);
requestFocus();
}
public void addNotify() {
super.addNotify();
if (thread == null) {
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
private void init() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
running = true;
gsm = new GameStateManager();
}
#Override
public void run() {
init();
long start;
long elapsed;
long wait;
// Game Loop
while (running) {
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = targetTime - (elapsed / 1000000);
if (wait < 0) {
wait = 5;
}
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void update() {
gsm.update();
}
private void draw() {
gsm.draw(g);
}
private void drawToScreen() {
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
g2.dispose();
}
#Override
public void keyPressed(KeyEvent k) {
gsm.keyPressed(k.getKeyCode());
}
#Override
public void keyReleased(KeyEvent k) {
}
#Override
public void keyTyped(KeyEvent arg0) {
}
}
This calls for the creation of a new GameStateManager object in its init() method and the class for that is here.
GameStateManager.java
public class GameStateManager {
private ArrayList<GameState> gameStates;
private int currentState;
public static final int MENUSTATE = 0;
public static final int SLOTGAMESTATE = 1;
public static final int DICEGAMESTATE = 2;
public static final int ROULETTEGAMESTATE = 3;
public static final int LEADERBOARDSTATE = 4;
public static final int SETTINGSSTATE = 5;
public static final int HELPSTATE = 6;
public GameStateManager() {
gameStates = new ArrayList<GameState>();
currentState = 0;
gameStates.add(new MenuState(this));
gameStates.add(new SlotGameState(this));
gameStates.add(new DiceGameState(this));
gameStates.add(new RouletteGameState(this));
gameStates.add(new LeaderboardState(this));
gameStates.add(new SettingsState(this));
gameStates.add(new HelpState(this));
}
public void setState(int state){
currentState = state;
gameStates.get(currentState).init();
currentState = 0;
}
public int getState() {
return currentState;
}
public void update() {
gameStates.get(currentState).init();
}
public void draw(java.awt.Graphics2D g){
gameStates.get(currentState).draw(g);
}
public void keyPressed(int k){
gameStates.get(currentState).keyPressed(k);
}
public void keyReleased(int k) {
gameStates.get(currentState).keyReleased(k);
}
}
GameState is an abstract class I have so its not worth posting, it only contains init(), draw(), etc. This next class is the last and final class and is called because GameStateMananger starts at MENUSTATE or 0, and when GSM is initialized it initializes its current state, thus taking us to the class MenuState
MenuState.java
public class MenuState extends GameState {
private Background bg;
public FontMetrics fontMetrics;
private int choice = 0;
private String[] options = { "Slot Machine", "Dice Toss", "Roulette Wheel", "Leaderboards", "Settings", "Help",
"Quit" };
private Color titleColor;
private Font titleFont;
private Font font;
public MenuState(GameStateManager gsm) {
this.gsm = gsm;
try {
bg = new Background("/Backgrounds/happybg.png");
titleColor = Color.WHITE;
titleFont = new Font("Georgia", Font.PLAIN, 28);
} catch (Exception e) {
e.printStackTrace();
}
font = new Font("Arial", Font.PLAIN, 12);
}
#Override
public void init() {
}
#Override
public void update() {
}
#Override
public void draw(Graphics2D g) {
Canvas c = new Canvas();
fontMetrics = c.getFontMetrics(font);
// Draw BG
bg.draw(g);
// Draw title
g.setColor(titleColor);
g.setFont(titleFont);
String title = "Jolly Jackpot Land!";
g.drawString(title, 36, 60);
g.setFont(font);
for (int i = 0; i < options.length; i++) {
if (i == choice)
g.setColor(Color.RED);
else
g.setColor(Color.WHITE);
g.drawString(options[i], 30, 120 + i * 15);
}
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.PLAIN, 10));
g.drawString("v1.1", 165, 235);
Object[] a = { ("Name: " + Player.getName()), ("Gil: " + Player.getGil()),
("Personal Best: " + Player.getPersonalBest()), ("Winnings: " + Player.getWinnings()),
("Wins: " + Player.getWins()), ("Losses: " + Player.getLosses()),
("Win/Loss Ratio: " + String.format("%.2f", Player.getRatio()) + "%") };
g.setFont(font);
if (Player.getName() != null) {
for (int x = 0; x < a.length; x++) {
g.drawString(a[x].toString(), GamePanel.WIDTH - fontMetrics.stringWidth(a[x].toString()) - 30,
120 + x * 15);
}
}
}
private void select() {
if (choice == 0) {
// Slots
gsm.setState(GameStateManager.SLOTGAMESTATE);
}
if (choice == 1) {
// Dice
gsm.setState(GameStateManager.DICEGAMESTATE);
}
if (choice == 2) {
// Roulette
gsm.setState(GameStateManager.ROULETTEGAMESTATE);
}
if (choice == 3) {
// Leaderboards
gsm.setState(GameStateManager.LEADERBOARDSTATE);
}
if (choice == 4) {
// Settings
gsm.setState(GameStateManager.SETTINGSSTATE);
}
if (choice == 5) {
// Help
gsm.setState(GameStateManager.HELPSTATE);
}
if (choice == 6) {
// Quit
System.exit(0);
}
}
#Override
public void keyPressed(int k) {
if (k == KeyEvent.VK_ENTER) {
select();
}
if (k == KeyEvent.VK_UP) {
choice--;
if (choice == -1) {
choice = options.length - 1;
}
}
if (k == KeyEvent.VK_DOWN) {
choice++;
if (choice == options.length) {
choice = 0;
}
}
}
#Override
public void keyReleased(int k) {
}
}
Background.java
public class Background {
private BufferedImage image;
private double x;
private double y;
public Background(String s) {
try {
image = ImageIO.read(getClass().getResourceAsStream(s));
} catch (Exception e) {
e.printStackTrace();
}
}
public void setPosition(double x, double y) {
this.setX(x);
this.setY(y);
}
public void draw(Graphics2D g) {
g.drawImage(image, 0, 0, null);
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
This is where it waits for input in the game loop basically. I know this is a lot of code, but a lot of it is skimming till a method call takes you to the next class. I just can't figure out why it only happens sometimes, if it was consistent I could debug it. Any help would be extremely appreciated.
These are both from clicking the .jar of the above program, exact same .jar, exact same source code, different result. I am bewildered.

Java Racing game: JPanel component moves along when I repaint the car object

I created 2 car objects using my CarPanel. Then I used to keylistener to repaint the object so it will look like moving forward.
But the problem is the whole panel moves along, even though I just increased the distance to the 'x' coordinate.
If I were to resize the window, it sometimes doesn't even clear the component.
public class RacingCars extends JFrame {
CarPanel car1;
CarPanel car2;
//Constructor
public RacingCars(){
setLayout(new GridLayout(2,1));
car1 = new CarPanel('w',Color.RED);
car2 = new CarPanel('k',Color.blue);
car1.setBackground(Color.black);
this.add(car1, BorderLayout.NORTH);
this.add(car2, BorderLayout.SOUTH);
this.addKeyListener(new keyListener());
}
class keyListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
if(e.getKeyChar()=='w'){
car1.moveCar();
}
if(e.getKeyChar()=='k'){
car2.moveCar();
}
}
}
}
CarPanel::
public class CarPanel extends JPanel {
private char forwardKey = 'w';
private boolean reachedTarget = false;
private Color color = Color.blue;
private int x= 10;
private int y= 10;
private int panelWidth;
private int panelHeight;
//default Constructor
public CarPanel(){
}
//overloaded Constructor
public CarPanel(char key, Color color){
this.forwardKey=key;
this.color = color;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
panelWidth= getWidth();
panelHeight= getHeight();
//draw a Car
g.setColor(color);
//polygon points
int t_x[]= {x+10,x+20,x+30,x+40};
int t_y[]= {y+10,y,y,y+10};
g.fillPolygon(t_x,t_y,t_x.length);
g.fillRect(x, y+10, 50, 10);
g.fillArc(x+10, y+20, 10, 10, 0, 360);
g.fillArc(x+30, y+20, 10, 10, 0, 360);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(750,100);
}
public void moveCar(){
if(this.x < panelWidth){
this.x+=10;
repaint();
}
}
}
Solution
Because of this code, I always had that extra space. but no idea how it supposed to cause to this strange behavior.
/*
public int getX(){
return this.x;
}
*/
But the problem is the whole panel moves along, ..
No it doesn't. The 'green dot' proves that.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RacingCars extends JFrame {
CarPanel car1;
CarPanel car2;
//Constructor
public RacingCars(){
setLayout(new GridLayout(2,1));
car1 = new CarPanel('w',Color.RED);
car2 = new CarPanel('k',Color.blue);
car1.setBackground(Color.black);
this.add(car1, BorderLayout.NORTH);
this.add(car2, BorderLayout.SOUTH);
this.addKeyListener(new MyKeyListener());
}
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
if(e.getKeyChar()=='w'){
car1.moveCar();
}
if(e.getKeyChar()=='k'){
car2.moveCar();
}
}
}
public static void main(String[] args) {
//Create the frame on the event dispatching thread
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
RacingCars rc = new RacingCars();
rc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rc.pack();
rc.setVisible(true);
}
});
}
}
class CarPanel extends JPanel {
private char forwardKey = 'w';
private boolean reachedTarget = false;
private Color color = Color.blue;
private int x= 10;
private int y= 10;
private int panelWidth;
private int panelHeight;
//default Constructor
public CarPanel(){
}
//overloaded Constructor
public CarPanel(char key, Color color){
this.forwardKey=key;
this.color = color;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(0,0,25,25);
panelWidth= getWidth();
panelHeight= getHeight();
//draw a Car
g.setColor(color);
//polygon points
int t_x[]= {x+10,x+20,x+30,x+40};
int t_y[]= {y+10,y,y,y+10};
g.fillPolygon(t_x,t_y,t_x.length);
g.fillRect(x, y+10, 50, 10);
g.fillArc(x+10, y+20, 10, 10, 0, 360);
g.fillArc(x+30, y+20, 10, 10, 0, 360);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(750,100);
}
public void moveCar(){
if(this.x < panelWidth){
this.x+=10;
repaint();
}
}
}

Categories

Resources