2017-04-25 7 views
0

私は本質的には一種のスロットマシンであるクラスのプロジェクトに取り組んでいます。上に3つの列があり、ユーザーがボタンをクリックして上のパネルの形を変更できるようにしました。ボトムには実際のスロットマシンがあり、目的はスピンした後に「スロットマシン」が何を表示するかを試してみることです。私はパネルとswitch文を書いていますが、ユーザーが上部で変更しなければならないパネルの形状を変更するのに問題があります。誰でもこのことについてどうやって正しい方向に私を置くことができますか?別のパネルのswitch文を使用するにはどうすればよいですか?

これはボタンとそのアクションを作成するパネルです:

import java.util.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class SlotPanel extends JPanel { 
ImagePanel slotOne, slotTwo, slotThree; // three panels for the computer to 
generate 
ImagePanel userOne, userTwo, userThree; // three panels chosen by the user 

// Panels used in SlotPanel 
JPanel titlePanel, guessPanel, slotTitlePanel; 
JPanel selectOnePanel, selectTwoPanel, selectThreePanel; 
JLabel title, guess, slotTitle; 
JButton selectOne, selectTwo, selectThree, spinMachine; 
private int currentShape; 

public SlotPanel() { 
currentShape = 0; 
// Instantiate all the objects declared above 
slotOne=new ImagePanel(); slotTwo=new ImagePanel(); slotThree=new ImagePanel(); 
userOne=new ImagePanel(); userTwo=new ImagePanel(); userThree=new ImagePanel(); 

titlePanel=new JPanel(); 
guessPanel=new JPanel(); 
slotTitlePanel=new JPanel(); 
title = new JLabel("Austin's Wheel of Wacky Shapes"); // Change this label for the top of the display section for the default display 
guess = new JLabel("Your Guess"); 
slotTitle = new JLabel("Slot Machine Wheel"); 
selectOnePanel=new JPanel(); selectTwoPanel=new JPanel(); selectThreePanel=new JPanel(); 
// Each of the three buttons to select the object has the same label 
selectOne=new JButton("Wheel 1"); selectTwo=new JButton("Wheel 2"); selectThree=new JButton("Wheel 3"); 
// Button to spin the machine at the bottom 
spinMachine=new JButton("Spin Machine!"); 

ObjectChanger listener = new ObjectChanger(); 
selectOne.addActionListener (listener); 
selectTwo.addActionListener (listener); 
selectThree.addActionListener (listener); 

setBackgroundColor(Color.gray); 
setPreferredSize(new Dimension(350, 415)); 

// Select the sizes for each of the panels 
titlePanel.setPreferredSize(new Dimension(300,20)); 
guessPanel.setPreferredSize(new Dimension(300,20)); 
slotTitlePanel.setPreferredSize(new Dimension(300,20)); 
selectOnePanel.setPreferredSize(new Dimension(110, 50)); 
selectOnePanel.setBackground(Color.red); 
selectTwoPanel.setPreferredSize(new Dimension(110, 50)); 
selectTwoPanel.setBackground(Color.red); 
selectThreePanel.setPreferredSize(new Dimension(110, 50)); 
selectThreePanel.setBackground(Color.red); 
spinMachine.setPreferredSize(new Dimension(240, 20)); 
selectOne.setPreferredSize(new Dimension(110,30)); 
selectTwo.setPreferredSize(new Dimension(110,30)); 
selectThree.setPreferredSize(new Dimension(110,30)); 
// Action listeners that will take care of the changing of the objects displayed 
selectOne.addActionListener(new ObjectChanger()); 
selectTwo.addActionListener(new ObjectChanger()); 
selectThree.addActionListener(new ObjectChanger()); 
// Action listener to get the "machine" in motion 
spinMachine.addActionListener(new Spin()); 
slotOne.setPreferredSize(new Dimension(110,100)); 
slotOne.setBackground(Color.white); 
slotTwo.setPreferredSize(new Dimension(110,100)); 
slotTwo.setBackground(Color.white); 
slotThree.setPreferredSize(new Dimension(110,100)); 
slotThree.setBackground(Color.white); 


// Add the title JLabel to the top panel 
titlePanel.add(title); 
guessPanel.add(guess); 
slotTitlePanel.add(slotTitle); 
// Add the selection button to the user-selectable panel one 
selectOnePanel.add(selectOne); // 
// Add the selection button to the user-selectable panel two 
selectTwoPanel.add(selectTwo); 
// Add the selection button to the user-selectable panel three 
selectThreePanel.add(selectThree); 

add(titlePanel); 
add(guessPanel); 
add(userOne); 
add(userTwo); 
add(userThree); 
add(selectOnePanel); 
add(selectTwoPanel); 
add(selectThreePanel); 
add(spinMachine); 
add(slotTitlePanel); 
add(slotOne); 
add(slotTwo); 
add(slotThree); 
} 

public void setBackgroundColor(Color color){ 

} 

// ActionListener class to determine which shape button was pressed 
public class ObjectChanger implements ActionListener{ 

@Override 
    public void actionPerformed(ActionEvent event){ 

    //Identifies which buttons have been pressed to change their according slot wheel 
     if (event.getSource() == selectOne);{ 
     while (currentShape == 0){ 
      switch (currentShape){ 
       case 0: 
        currentShape++; 
       break; 
       case 1: 
        currentShape++; 
       break; 
       case 2: 
        currentShape++; 
       break; 
       case 3: 
        currentShape = 0; 
       break; 
      } 
     } 
     } 
    if (event.getSource() == selectTwo);{ 
     while (currentShape == 0){ 
      switch (currentShape){ 
       case 0: 
        currentShape++; 
       break; 
       case 1: 
        currentShape++; 
       break; 
       case 2: 
        currentShape++; 
       break; 
       case 3: 
        currentShape = 0; 
       break; 
      } 
      } 
     } 
    if (event.getSource() == selectThree);{ 
     while (currentShape == 0){ 
      switch (currentShape){ 
       case 0: 
        currentShape++; 
       break; 
       case 1: 
        currentShape++; 
       break; 
       case 2: 
        currentShape++; 
       break; 
       case 3: 
        currentShape = 0; 
       break; 
      } 
      } 
     } 
    } 
} 

// ActionListener class to determine when the Spin button is called 

public class Spin implements ActionListener{ 

    public void actionPerformed(ActionEvent event){ 
    Random generator = new Random(); 

     //Changes the background color to green if all of the wheels match 
     //or red if even one does not match to its designated wheel 
     if (slotOne == userOne && slotTwo == userTwo && slotThree == userThree) 
      setBackgroundColor(Color.green); 
     else 
      setBackgroundColor(Color.red); 


    } 
    } 
} 

そして、これが変更されているパネルを作成し、私が作った画像パネルである:

import java.awt.*; 
import javax.swing.*; 

// This class describes each of the Image Panels (6 total displayed) 
// And provides methods for drawing of the shapes 
public class ImagePanel extends JPanel{ 
// Use an int value to keep the current shape for the object 
private int currentShape, count; 
// Constructs the panels by assigning their shape and size 
public ImagePanel() { 
setPreferredSize(new Dimension(110,100)); 
setBackground(Color.black); 
currentShape = 0; 

} 

// Redraws the object after "Spin" has been selected 
public void changeShape(){ 
    currentShape++; 
    if (currentShape > 3) 
     currentShape = 0; 

    // Repaint is called 
    repaint(); 
} 

// Used when the "SpinMachine" button is called to randomly generate shapes 
public void changeShape(int setShape){ 
    currentShape=setShape; 
    repaint(); 
} 

// Accessor for the shape 
public int getShape(){ 
    return currentShape; 
} 

// PaintComponent method takes care of the drawing 
public void paintComponent(Graphics g){ 

    super.paintComponent(g); 
    g.setColor(Color.blue); 

//Switch method to change between the four shapes needed for the slot machine 
    switch (currentShape) { 
    case 0: g.fillRect(25, 20, 35, 70); 
    break; 
    case 1: 
     g.fillOval(25, 20, 70, 70); 
     break; 
    case 2: 
     int x[]={55,15,95}, y[]={25,95,95}; 
     g.fillPolygon(x,y,3); 
     break; 
    case 3: 
     g.fillRect(25, 25, 70, 35); 
     break; 
    default: 
     break; 
    } 
    } 
} 

感謝私は事前に取得するためのあなたの助けのためにあなた!

+0

あなたが必要とするのは、モデルとオブザーバーのパターンです。モデルは状態を保持し、制御します。状態が変化すると、関心のあるすべての関係者に通知が生成されます。 [model-view-controller](https://blog.codinghorror.com/understanding-model-view-controller/)と[observer pattern](http://www.oodesign.com/observer-pattern)を見てください。 .html)詳細については、 – MadProgrammer

答えて

-1

ここではコードの作業バージョンを示していますが、以下で間違っていたことを説明しています。テスト目的で私自身のメインメソッドを追加しました。まず

import java.awt.*; 
import javax.swing.*; 

// This class describes each of the Image Panels (6 total displayed) 
// And provides methods for drawing of the shapes 
public class ImagePanel extends JPanel{ 
// Use an int value to keep the current shape for the object 
private int count; 
private int currentShape; 
// Constructs the panels by assigning their shape and size 
public ImagePanel() { 
setPreferredSize(new Dimension(110,100)); 
setBackground(Color.black); 
currentShape = 0; 

} 
public void incrementCurrent(){ 
    currentShape++; 
    if(currentShape>3) 
     currentShape=0; 
    changeShape(currentShape); 
} 
// Redraws the object after "Spin" has been selected 
public void changeShape(){ 
    currentShape++; 
    if (currentShape > 3) 
     currentShape = 0; 

    // Repaint is called 
    repaint(); 
} 

// Used when the "SpinMachine" button is called to randomly generate shapes 
public void changeShape(int setShape){ 
    currentShape=setShape; 
    repaint(); 
} 

// Accessor for the shape 
public int getShape(){ 
    return currentShape; 
} 

// PaintComponent method takes care of the drawing 
public void paintComponent(Graphics g){ 

    super.paintComponent(g); 
    g.setColor(Color.blue); 

//Switch method to change between the four shapes needed for the slot machine 
    switch (currentShape) { 
    case 0: g.fillRect(25, 20, 35, 70); 
    break; 
    case 1: 
     g.fillOval(25, 20, 70, 70); 
     break; 
    case 2: 
     int x[]={55,15,95}, y[]={25,95,95}; 
     g.fillPolygon(x,y,3); 
     break; 
    case 3: 
     g.fillRect(25, 25, 70, 35); 
     break; 
    default: 
     break; 
    } 
    } 
} 

:ここ

import java.util.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class SlotPanel extends JPanel { 

public static void main(String[] args){ 
    JFrame frame = new JFrame("TESt"); 
    frame.add(new SlotPanel()); 
    frame.pack(); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 
ImagePanel slotOne, slotTwo, slotThree; // three panels for the computer to 
//generate 
ImagePanel userOne, userTwo, userThree; // three panels chosen by the user 

// Panels used in SlotPanel 
JPanel titlePanel, guessPanel, slotTitlePanel; 
JPanel selectOnePanel, selectTwoPanel, selectThreePanel; 
JLabel title, guess, slotTitle; 
JButton selectOne, selectTwo, selectThree, spinMachine; 
private int currentShape; 

public SlotPanel() { 
    currentShape = 0; 
    // Instantiate all the objects declared above 
    slotOne=new ImagePanel(); slotTwo=new ImagePanel(); slotThree=new ImagePanel(); 
    userOne=new ImagePanel(); userTwo=new ImagePanel(); userThree=new ImagePanel(); 

    titlePanel=new JPanel(); 
    guessPanel=new JPanel(); 
    slotTitlePanel=new JPanel(); 
    title = new JLabel("Austin's Wheel of Wacky Shapes"); // Change this label for the top of the display section for the default display 
    guess = new JLabel("Your Guess"); 
    slotTitle = new JLabel("Slot Machine Wheel"); 
    selectOnePanel=new JPanel(); selectTwoPanel=new JPanel(); selectThreePanel=new JPanel(); 
    // Each of the three buttons to select the object has the same label 
    selectOne=new JButton("Wheel 1"); selectTwo=new JButton("Wheel 2"); selectThree=new JButton("Wheel 3"); 
    // Button to spin the machine at the bottom 
    spinMachine=new JButton("Spin Machine!"); 

    //Um why? 
    //ObjectChanger listener = new ObjectChanger(); 
    //selectOne.addActionListener (listener); 
    //selectTwo.addActionListener (listener); 
    //selectThree.addActionListener (listener); 

    setBackgroundColor(Color.gray); 
    setPreferredSize(new Dimension(350, 415)); 

    // Select the sizes for each of the panels 
    titlePanel.setPreferredSize(new Dimension(300,20)); 
    guessPanel.setPreferredSize(new Dimension(300,20)); 
    slotTitlePanel.setPreferredSize(new Dimension(300,20)); 
    selectOnePanel.setPreferredSize(new Dimension(110, 50)); 
    selectOnePanel.setBackground(Color.red); 
    selectTwoPanel.setPreferredSize(new Dimension(110, 50)); 
    selectTwoPanel.setBackground(Color.red); 
    selectThreePanel.setPreferredSize(new Dimension(110, 50)); 
    selectThreePanel.setBackground(Color.red); 
    spinMachine.setPreferredSize(new Dimension(240, 20)); 
    selectOne.setPreferredSize(new Dimension(110,30)); 
    selectTwo.setPreferredSize(new Dimension(110,30)); 
    selectThree.setPreferredSize(new Dimension(110,30)); 
    // Action listeners that will take care of the changing of the objects displayed 
    selectOne.addActionListener(new ObjectChanger()); 
    selectTwo.addActionListener(new ObjectChanger()); 
    selectThree.addActionListener(new ObjectChanger()); 
    // Action listener to get the "machine" in motion 
    spinMachine.addActionListener(new Spin()); 
    slotOne.setPreferredSize(new Dimension(110,100)); 
    slotOne.setBackground(Color.white); 
    slotTwo.setPreferredSize(new Dimension(110,100)); 
    slotTwo.setBackground(Color.white); 
    slotThree.setPreferredSize(new Dimension(110,100)); 
    slotThree.setBackground(Color.white); 


    // Add the title JLabel to the top panel 
    titlePanel.add(title); 
    guessPanel.add(guess); 
    slotTitlePanel.add(slotTitle); 
    // Add the selection button to the user-selectable panel one 
    selectOnePanel.add(selectOne); // 
    // Add the selection button to the user-selectable panel two 
    selectTwoPanel.add(selectTwo); 
    // Add the selection button to the user-selectable panel three 
    selectThreePanel.add(selectThree); 

    add(titlePanel); 
    add(guessPanel); 
    add(userOne); 
    add(userTwo); 
    add(userThree); 
    add(selectOnePanel); 
    add(selectTwoPanel); 
    add(selectThreePanel); 
    add(spinMachine); 
    add(slotTitlePanel); 
    add(slotOne); 
    add(slotTwo); 
    add(slotThree); 
} 

public void setBackgroundColor(Color color){ 

} 

// ActionListener class to determine which shape button was pressed 
public class ObjectChanger implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent event){ 
     //Identifies which buttons have been pressed to change their according slot wheel 
     if (event.getSource() == selectOne){ 
      userOne.incrementCurrent(); 
     } 
     if (event.getSource() == selectTwo){ 
      userTwo.incrementCurrent(); 
     } 
     if (event.getSource() == selectThree){ 
      userThree.incrementCurrent(); 
     } 
    } 
} 

// ActionListener class to determine when the Spin button is called 

public class Spin implements ActionListener{ 

    public void actionPerformed(ActionEvent event){ 
     Random generator = new Random(); 

     //Changes the background color to green if all of the wheels match 
     //or red if even one does not match to its designated wheel 
     if (slotOne == userOne && slotTwo == userTwo && slotThree == userThree) 
      setBackgroundColor(Color.green); 
     else 
      setBackgroundColor(Color.red); 


    } 
} 
} 

は、第二のクラスであるあなたは二回、両方ここにActionListenerを追加しました:ここ

ObjectChanger listener = new ObjectChanger(); 
selectOne.addActionListener (listener); 
selectTwo.addActionListener (listener); 
selectThree.addActionListener (listener); 

と:

selectOne.addActionListener(new ObjectChanger()); 
selectTwo.addActionListener(new ObjectChanger()); 
selectThree.addActionListener(new ObjectChanger()); 

セカンドますcurrentShapeの値を使用してJPanelsの形状を変更しようとしましたが、switchステートメントはインクリメントしたいだけなので賢明でした。変更シェイプメソッドを呼び出さなかったので、実際には何もしませんでした。 ImagePanelクラスにincrementCurrentメソッドを追加しました。

public void incrementCurrent(){ 
    currentShape++; 
    if(currentShape>3) 
     currentShape=0; 
    changeShape(currentShape); 
} 

これで値が変更され、形状が再描画されるようになりました。

関連する問題