Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with java GUI graphics
New Posts  All Forums:Forum Nav:

Help with java GUI graphics

post #1 of 4
Thread Starter 


Hi, i'm taking a java course. This is a game where you click green or black to move a token, the green can move 1 right or jump over a token, given there is room. black does same while moving left. Goal is just to swap green and black.

Here is my code (in progress):
You don't really need to read my code, my question is posted at the bottom.
Code:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;


public class TokensGame extends JFrame{
        private JButton BGreen;
        private JButton BBlack;
        private JButton BNewGame;
        int tokenYposition=180;
        int tokenXposition=60;
        char[] theOrder = {'g','g','g','g','g','g','e','b','b','b','b','b','b'};
        public TokensGame()
        {
                super("Tokens Game");
                setSize(400,300);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
                setLayout(new FlowLayout());
                
                BGreen = new JButton("Green");
                BBlack = new JButton("Black");
                BNewGame = new JButton("New Game");
                add(BGreen);
                add(BBlack);
                add(BNewGame);
                
                ButtonHandler handler = new ButtonHandler();
            BGreen.addActionListener( handler );
            BBlack.addActionListener( handler );
            BNewGame.addActionListener(handler);
        }
        public void paint(Graphics g)
        {
               //Initializing:
                super.paint(g);
                g.setColor(Color.green);
                for(int i=0; i<13; i++)
                {
                        if(i == 6) g.setColor(getBackground());
                        else if (i == 7) g.setColor(Color.black);
                        g.fillOval(tokenXposition+(19*i), tokenYposition, 16, 24);      
                }
                        

        }
        public void MoveBlack()
        {       // called when the 'Black' button is clicked,
                int theSpace = -1;
                int theLocation= -1; // the location of the left most black
                for(int i=0; i<=12; i++){
                        if(theOrder[i] == 'b' && theLocation == -1) theLocation = i;
                        if(theOrder[i] == 'e') theSpace =i;
                }
                // only move for acceptable circumstances, & change theOrder after moving
                if(theLocation == theSpace+1){
                        g.setColor(Color.black);
                        g.fillOval(tokenXposition+(19*theSpace), tokenYposition, 16, 24);    //move the black left one token
                        g.setColor(getBackground());
                        g.fillOval(tokenXposition+(19*theLocation), tokenYposition, 16, 24);
                        theOrder[theSpace] = 'b';
                        theOrder[theLocation] = 'e';
                }
        }
        public void MoveGreen()
        {
                
        }
        
class ButtonHandler implements ActionListener
{
                public void actionPerformed (ActionEvent event)
                {
                if(event.getSource() == BGreen)
                {
                        MoveGreen();
                }
                if(event.getSource() == BBlack)
                {
                        MoveBlack();
                }
                
                }
}       
}

So here's what I am trying to get to, you click black, and it runs the moveBlack() function. problem is that the moveBlack() uses things like g.fillOval which requires Graphics g, I'm a newbie so there is probably something simple I'm missing. I had no problem passing Graphics g to paint(Graphics g), how do I do it for moveBlack() ? Any help is appreciated, even though I am taking a course, I have had to learn almost everything on my own so far, so I haven't really had an explanation of how Graphics or Paint works and the like.
Edited by 1keith1 - 8/4/13 at 6:50pm
Only 9" Tall
(8 items)
 
  
CPUMotherboardGraphicsRAM
i5 4570 3.6Ghz Turbo Asrock Z87E-ITX Powercolor 7750 Samsung 
Hard DriveCoolingOSCase
Mushkin Chronos Scythe Big Shuriken v2 Windows 8.1 64bit Lian Li PC-Q02B 
  hide details  
Reply
Only 9" Tall
(8 items)
 
  
CPUMotherboardGraphicsRAM
i5 4570 3.6Ghz Turbo Asrock Z87E-ITX Powercolor 7750 Samsung 
Hard DriveCoolingOSCase
Mushkin Chronos Scythe Big Shuriken v2 Windows 8.1 64bit Lian Li PC-Q02B 
  hide details  
Reply
post #2 of 4
Code:
if(theLocation == theSpace+1){
                        g.setColor(Color.black);
                        g.fillOval(tokenXposition+(19*theSpace), tokenYposition, 16, 24);   
                        g.setColor(getBackground());
                        g.fillOval(tokenXposition+(19*theLocation), tokenYposition, 16, 24);
                        theOrder[theSpace] = 'b';
                        theOrder[theLocation] = 'e';
                        g.repaint(); // added this
}

Been so long since I was told this, but if I remember correctly you have to invoke repaint function.
post #3 of 4
Add "Graphics g" to the MoveBlack method (You will probably need to do this for MoveGreen() as well).
public void MoveBlack(Graphics g){
...
}

Call MoveBlack(g) rather than MoveBlack().

Since its not clear what is calling paint(g), I can't help to much on how to add Graphics g to the ButtonHandler class.
Edited by sidcitris - 8/6/13 at 10:53am
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
post #4 of 4
Thread Starter 
Okay I figured it out, what I did was modify paint to make it more universal, so that I could use repaint(), and then used repaint(); in moveblack and movegreen.
Only 9" Tall
(8 items)
 
  
CPUMotherboardGraphicsRAM
i5 4570 3.6Ghz Turbo Asrock Z87E-ITX Powercolor 7750 Samsung 
Hard DriveCoolingOSCase
Mushkin Chronos Scythe Big Shuriken v2 Windows 8.1 64bit Lian Li PC-Q02B 
  hide details  
Reply
Only 9" Tall
(8 items)
 
  
CPUMotherboardGraphicsRAM
i5 4570 3.6Ghz Turbo Asrock Z87E-ITX Powercolor 7750 Samsung 
Hard DriveCoolingOSCase
Mushkin Chronos Scythe Big Shuriken v2 Windows 8.1 64bit Lian Li PC-Q02B 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with java GUI graphics