Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Prikaz jednog od dva panela u frejmu

[es] :: Java :: Prikaz jednog od dva panela u frejmu

[ Pregleda: 1883 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Nedja995

Član broj: 309141
Poruke: 55
62.240.22.*



+1 Profil

icon Prikaz jednog od dva panela u frejmu02.09.2013. u 21:26 - pre 129 meseci
Pravim jednostavnu PingPong igricu radi vezbanja pa hocu da dodam funkciju start screen koja ce da se prikazuje kad se otvori igrica i ako se pritisne odredjeno dugme za vreme igre (new game/pause).
Klasa za menu screen i igricu nasledjuju JPanel
Kako da napravim dva panela (jedan start screen a u drugom je grafika igrice) koja se pokrecu u zasebne niti i prikazuju u klasi koja nasledjuje JFrame, zavisno od vrednosti jedne promenjive u toj frejm klasi.Molim vas objasnite mi samo princip nisam uspeo da shvatim sa neta iako niti shvatam solidno ali oko swinga ima malo vishe z********je
Pokushao sam da u dve panel klase implementiram Runnable interfejs i u run() metode sam pokushao svashta ali buni me dodavanje panela u frejm sigurno postoji neko reshenje za to(da li brisati panel iz frejma pa dodavati drugi ili??).
Znaci da mi se jedan panel pojavi i onda posle pritiska na dugme da pokrene igricu tj da se pojavi drugi panel na tom mestu ali shta onda sa tim prvim panelom(taj bi mogao da ugasim ali bi panel za igricu hteo samo da zamrznem i uklonim ga kad treba da se pojavi menu screen panel.Molim vas za neke smernice.
 
Odgovor na temu

Nedja995

Član broj: 309141
Poruke: 55
62.240.22.*



+1 Profil

icon Re: Prikaz jednog od dva panela u frejmu02.09.2013. u 21:32 - pre 129 meseci
PongPingGameFrame
Code:
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;


public class PongPingGameFrame extends JFrame {

    volatile private boolean showConfig;
    
    volatile private static boolean gameRunning;
    
    private  ConfigScreen configScreen;
    
    private  GamePanel gamePanel;
    
    private  Thread configScreenT;
    private  Thread gamePanelT;

    public PongPingGameFrame(){
        this.setBounds(300,300,500,500);
        this.setGameRunning(false);
        this.setShowConfig(true);
        

        gamePanel = new GamePanel(this);
        configScreen = new ConfigScreen(this);
        configScreenT = new Thread(configScreen);
        gamePanelT = new Thread(gamePanel);
    

        this.setVisible(true);
    }
    

    



    public static void main(String[] ar){
        EventQueue.invokeLater(new Runnable(){

            PongPingGameFrame frame = new PongPingGameFrame();
            
            @Override
            public void run() {
                if(frame.isGameRunning()){

                    frame.gamePanelT.start();
                    System.out.print("GAME PANELLL");
                }
                else
                {
                    try{
        
                    frame.configScreenT.start();
                    System.out.print("CONFIGGG");
                    }
                    catch(NullPointerException n){}
                }
        
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            frame.repaint();
            
            }

        });
    

        
                

    }    
    
    public boolean isShowConfig() {    return showConfig;    }
    public void setShowConfig(boolean showConfig) {    this.showConfig = showConfig;    }

    public boolean isGameRunning() {    return gameRunning;    }
    public void setGameRunning(boolean gameRunning) {    this.gameRunning = gameRunning;    }


}


GamePanel
Code:
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable {

    private PongPingGameFrame frame;
    
    public GamePanel(PongPingGameFrame frame){
        this.frame = frame;
        this.setBounds(frame.getBounds());        
    }
            
    public void paintComponent(Graphics g){
        Graphics2D g2D = (Graphics2D)g;
        g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
    
    @Override
    public void run() {

        frame.add(this);
        
        repaint();

        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}


ConfigScreen (pause)
Code:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.EventListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class ConfigScreen extends JPanel implements ActionListener, Runnable {

    
    /******************************************************************************
     * Private members
     * 
     * backgroundColor Color
     * width           int
     * height          int
     * stickColor      Color
     * stickSize       int
     * stickSpeed      int
     * ballSize        int
     * gameRunning     boolean
     * parrentFrame    JFrame with no getter and setter
     */
    private Color backgroundColor;
    private int width;
    private int height;
    private Color sticksColor;
    private int stickSize;
    private int stickSpeed;
    private int ballSize;
    private boolean gameRunning;
    private PongPingGameFrame parrentFrame;

    
    /*************************************
     * Getter And Setter methods
     */
    public Color getBackgroundColor() {    return backgroundColor;    }
    public void setBackgroundColor(Color backgroundColor) {    this.backgroundColor = backgroundColor;    }
    
    public void setWidth(int w){ this.width = w; }
    public int getWidth(){ return this.width; }
    
    public void setHeight(int h){ this.height = h; }
    public int getHeight(){return this.height; }
    
    public Color getSticksColor() { return sticksColor;    }
    public void setSticksColor(Color sticksColor) { this.sticksColor = sticksColor; }
    
    public int getStickSpeed() {    return stickSpeed; }
    public void setStickSpeed(int stickSpeed) {    this.stickSpeed = stickSpeed;    }
    
    public int getBallSize() {    return ballSize;  }
    public void setBallSize(int ballSize) {    this.ballSize = ballSize;    }
    
    public boolean isGameRunning() {    return gameRunning;    }
    public void setGameRunning(boolean gameRunning) {    this.gameRunning = gameRunning;    }
    /**
     * End Getter And Setters
     *///////////////////////////////////////////////////////////////////////////////////////////////////////
    
    

    private JButton resumeB;
    private JButton startB;
    private JButton exitB;
    
    
    
    /**********************
     * init()
     * Set default Background and Sticks Color
     */
    public void init(){

        this.setBackgroundColor(Color.BLACK);
        this.width = parrentFrame.getWidth();
        this.height = parrentFrame.getHeight();
        this.stickSize = width / 4;
        this.setBallSize(stickSize / 4);
        this.setStickSpeed(height / 100);
        this.setSticksColor(Color.GREEN);
        this.setGameRunning(false);
        
        System.out.println(" \ninit");

    
    }
    
    
    private void initComponents(){
        
        this.resumeB = new JButton("Resume");
        resumeB.setBounds(width / 3 - this.getWidth() / 49, 
                          height / 3 - 40,
                          width / 3, 
                          height / 10 );
        resumeB.setEnabled(false);
        
        this.startB = new JButton("Start Game");
        startB.setBounds(width / 3 - this.getWidth() / 49, 
                         resumeB.getY() + resumeB.getHeight() + 10, 
                         width / 3, 
                         height / 10 );
        startB.addActionListener(this);
        
        this.exitB = new JButton("Exit");
        exitB.setBounds(width / 3 - this.getWidth() / 49, 
                        startB.getY() + startB.getHeight() + 30, 
                        width / 3, 
                        height / 10 );
          
        
        this.setLayout(null);
        
        this.add(resumeB);
        this.add(startB);
        this.add(exitB);
        System.out.println("\ncomponent init");
        
    }
    
    public ConfigScreen(PongPingGameFrame parrentFrame){
        this.parrentFrame = parrentFrame;
        init();
        initComponents();
        
        this.setBounds(parrentFrame.getBounds());


        System.out.println("\ntgread init");
        
    }
    @Override
    public void run() {
        parrentFrame.add(this);
        this.setVisible(true);
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
                    if(actionEvent.getSource().equals(startB)){
                        parrentFrame.setGameRunning(true);
                        System.out.print("kliketd");
            parrentFrame.repaint();
    
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }        
                    }
    }
    
}


Ovi delovi shto se ticu Akcija i Niti su mi bitni.
Molim iskusne programere da mi daju neku smernicu unapred hvala
 
Odgovor na temu

Nedja995

Član broj: 309141
Poruke: 55
62.240.22.*



+1 Profil

icon Re: Prikaz jednog od dva panela u frejmu03.09.2013. u 14:31 - pre 129 meseci
Ili je bolji nacin da se jedan panel boji u zavisnosti da li je pritisnuta pauza ili je igra u toku?
 
Odgovor na temu

blekmor
fax
fax

Član broj: 309532
Poruke: 55
*.teol.net.



+30 Profil

icon Re: Prikaz jednog od dva panela u frejmu04.09.2013. u 01:21 - pre 129 meseci
Cini mi se da imas prilicno pogresan pristup.
Ne trebaju paneli da ti budu niti, nego nit treba da ti bude frejm koji sadrzi dva panela.
Znaci napravis da ti je frejm nit, tu mu gurnes boolean da li je igra pauzirana. U run-u vrtis u while petlji pitanje da li je igra pauzirana, ukoliko jeste prikazes "start panel", zamrznes fiziku (nisam ti gledao fiziku, ali treba da imas par parametara sa informacijama o trenutnoj poziciji lopte, igraca itd...). Ukoliko igra nije pauzirana "start panel" ti bude nevidljiv a repejntujes panel sa igrom.

prosta skica
Code:

public class MainFrame extends JFrame implements Runnable{
 private JPanel gamePanel;
 private JPanel startPanel;
 private boolean isPaused;
 private boolean continue;
 // fizika i ostala polja
 
 public MainFrame(){
  // inicijalizuj pocetno stanje. Npr stavis gamePanel.setVisible(false); startPanel.setVisible(true);
  // inicijalizujes startPanel, i zapocnes nit
   super();
   new Thread(this).start();
 }

 public void run(){
  // bitno je da ubacis neki sleep u while petlju
  while(continue){
   if(isPaused)
    // zaustavis igru, i prikazes start skrin, zamrznes fiziku (ne uradis nista) i stavis game skrin da bude nevidljiv
    showStartScreen();
   else
    // izracunas fiziku i repejntujes skirn
    showGameScreen();
  }
 }
}
 
Odgovor na temu

[es] :: Java :: Prikaz jednog od dva panela u frejmu

[ Pregleda: 1883 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.