[SOLVED]
Hello,
I am messing up with MVC constructions a little and have a strange problem with adding JPanel to a JFrame. Here is the "skelet" of the code:
MAIN CLASS:
public class MVCtest extends JFrame { public MVCtest() { super("MVC TEST"); Model model = new Model(); View view = new View(model); Controller controller = new Controller(model); view.addActionListener(controller); model.addObserver(view); this.getContentPane().add(view); } public static void main(String[ ] args) { final MVCtest app = new MVCtest(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { app.pack(); app.setVisible(true); } }); } }
MODEL CLASS "SKELET"
class Model extends Observable { // engine of the app }
VIEW CLASS "SKELET"
class View extends JPanel implements Observer { private Model model; private ShowDeck pCards; .... public View(Model m) { this.model = m; // initialization of variables // adding swing objects to the extended JPanel: add(pCards); .... } public void addActionListener(ActionListener l) { button.addActionListener(l); } public void update(Observable o, Object arg) { // calls some methods from the class Model and does other actions //concerned with output on the screen } }
CONTROLLER CLASS "SKELET"
class Controller implements ActionListener { private Model model; public Controller(Model m) { this.model = m; } public void actionPerformed(ActionEvent e) { // calls methods of class Model and does some changes in it } }
The problem I have is in class ShowDeck. It extends JPanel and as you can see, in the class View I add it to a View JPanel and in the main method View JPanel is added to the JFrame.
ShowDeck class calls paintComponent method and draws some stuff on the JPanel.
So can anyone find any mistakes from the skelet I gave you?
================================================== ======================
================================================== ======================
EDIT:
I though that info isn't enough, so here I paste all code. It is a mess here with lot of unnecessary things.
import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; public class MVCtest extends JFrame { public MVCtest() { super("MVC TEST"); setLayout(new BorderLayout()); Model model = new Model(); View view = new View(model); BarView bView = new BarView(); Controller controller = new Controller(model); view.addActionListener(controller); model.addObserver(view); model.addObserver(bView); this.getContentPane().add(view, BorderLayout.CENTER); this.getContentPane().add(bView, BorderLayout.SOUTH); } public static void main(String[ ] args) { final MVCtest app = new MVCtest(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { app.pack(); app.setVisible(true); } }); } } class Model extends Observable { private CardDeck deck = new CardDeck(); protected int i = 0; public void increment() { setChanged(); notifyObservers(new Integer(i)); i++; } public CardDeck getDeck() { return deck; } public CardBase getCard() { return deck.getDeck()[i]; } } class BarView extends JProgressBar implements Observer { public BarView() { super(0, 52); setStringPainted(true); } public void update(Observable o, Object arg) { Integer v = (Integer) arg; setValue(v.intValue()); } } class View extends JPanel implements Observer { private Model model; private JButton button; private JLabel label; private ShowDeck pCards; public View(Model m) { this.model = m; button = new JButton("Add card"); label = new JLabel(); pCards = new ShowDeck(new ArrayList()); add(button); add(label); add(pCards); } public void addActionListener(ActionListener l) { button.addActionListener(l); } public void update(Observable o, Object arg) { pCards.addCard(model.getCard()); label.setText(model.getCard() + " added"); } } class Controller implements ActionListener { private Model model; public Controller(Model m) { this.model = m; } public void actionPerformed(ActionEvent e) { model.increment(); } }
What I actually want to get from this ShowDeck class right now is JPanel with the orange rectangle painted in it.
import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.image.*; public class ShowDeck extends JPanel { public static final int W = Suit.W, H = Suit.H; public static final int XO = 10, YO = 10; public static final int SHIFT = W / 3; Collection deck; public ShowDeck(Collection deck) { this.deck = deck; } public ShowDeck() { deck = new ArrayList(); } // sitie veikia tik jeigu JFrame apatine eilute padaro true: // frame.setDefaultLookAndFeelDecorated(true); public void addCard(CardBase c) { deck.add(c); createAndShowGUI(); } public void paint(Graphics g) { g.setColor(Color.ORANGE); g.fillRect(10, 10, 100, 100); } /* protected void paintComponent(Graphics g) { super.paintComponent(g); int x = 10, y = 10; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); BufferedImage bi = gc.createCompatibleImage(900, 200, Transparency.BITMASK); Graphics2D g2 = bi.createGraphics(); int k = 0; for(Iterator i = deck.iterator(); i.hasNext();) { System.out.println(++k); CardBase crd = (CardBase) i.next(); g2.setColor(Color.RED); g2.fillRect(x, y, 30, 30); // g2.drawImage(crd.getBI(), x, y, null); x += SHIFT; } g.drawImage(bi, 0, 0, null); // g.setColor(Color.BLACK); // g.fillRect(10, 10, 100, 100); g2.dispose(); } */ public static Collection testOp(Collection a, Collection b) { Collection res = new ArrayList(a); res.addAll(b); return res; } private static void createAndShowGUI() { Collection all = new ArrayList(); all.add(Card.getCard(25)); all.add(Card.getCard(5)); all.add(Card.getCard(15)); all.add(Card.getCard(45)); all.add(Card.getCard(35)); Collection arg = new ArrayList(); all.add(Card.getCard(15)); all.add(Card.getCard(51)); ShowDeck s1 = new ShowDeck(all); ShowDeck s2 = new ShowDeck(arg); JPanel pnl = new JPanel(); pnl.setBorder(new javax.swing.border.TitledBorder(null, "Before operation")); pnl.add(s1); pnl.add(s2); JPanel res = new JPanel(); res.setBorder(new javax.swing.border.TitledBorder(null, "After operation")); ShowDeck s3 = new ShowDeck(testOp(all, arg)); res.add(s3); } }