![]() |
JAVA: syntax, commands, or what?
Hi Everbody!
I'm just learning JAVA. I'm using Windows 98/JDK 1.3.1_08/DOS4.0. 1. I'm thinking I need an "implements ActionListener and an ItemListener in my main(), for the menu items; but, "it" doesn't like it---and/or, it doesn't like the way I'm, maybe, not followin'-through?! 2. I need a Status Bar. Couldn't I just use, something like " public statusBar extends JPanel { " ?? 3. I need to know how to "talk" to, I guess, another object/class... for instance, I need to know what command to use to make the menu items do what they're s'pose to. I tried putting: public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (command.equals("Red")) { setBackground(Color.red); } else if (command.equals("Green")) setBackground(Color.green); else if (command.equals("Blue")) setBackground(Color.blue); else if (command.equals("Cyan")) setBackground(Color.cyan); else if (command.equals("Magenta")) setBackground(Color.magenta); else if (command.equals("Yellow")) setBackground(Color.yellow); etc, after drawCard Graphics---"it" didn't like it!! 4. I need a Right-click menu---I tried: JPopupMenu popup; I put it right before " Image cardImages; " in the main() then, I put: popup = new JPopupMenu(); popup.add("Sort By Suit").addActionListener(this); popup.add("Numerical Sort").addActionListener(this); popup.add("Restore Card Order").addActionListener(this); in class CardsHandsFrameCanvas---but, I just happened to think of something---I didn't put, something like: setJPopupMenu(create popupMenu); in the Constructor-----could that've been it?? 5. I need to Drag-and-drop; I put: Image clickedImage = null; Image draggedImage = null; int prevDragX; int prevDragY; public void mousePressed(MouseEvent evt) { if (draggedImage != null) { return; } if (evt.isPopupTrigger()) { popup.show(this,x70,y70); } else { // Start dragging the shape. draggedShape = clickedShape; prevDragX = x; prevDragY = y; } } in the class CardsHandsFrameCanvas; and, also: public void mouseDragged(MouseEvent evt) { etc. public void mouseReleased(MouseEvent evt) { etc. and: addMouseListener(this); addMouseMotionListener(this); after setBackground(Color.white); in the Constructor. I don't THINK I got any/many errors on this part---I know I didn't get any, for puttin' addMouseListener(this); after the setBackground... part. 6. Right now, the errors I'm getting, are: CardsHandsFrame.java:26: ';' expected public JMenuBar create menuBar() { ^ CardsHandsFrame.java:16: cannot resolve symbol symbol : constructor CardsHandsFrame (java.awt.image) location: class CardsHandsFrame CardsHandsFrame game = new CardsHandsFrame(cards); ^ 2 errors I've tried everything I can think of, to correct these errors. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.ImageIcon; public class CardsHandsFrame extends JFrame { Image cardImages; // ONE IMAGE CONTAINING ALL CARDS; boolean runStandAlone; public static void main(String[] args) { Image cards = Toolkit.getDefaultToolkit().getImage("smallcards.gif"); CardsHandsFrame game = new CardsHandsFrame(cards); game.runStandAlone = true; } public JMenuBar create menuBar() { // BEGIN MENU BAR menuBar = new JMenuBar(); // FIRST MENU - GAME menu = new JMenu("Game"); menu.setMnemonic(KeyEvent.VK_G); menu.getAccessibleContext().setAccessibleDescription( "Displays Game Items"); menuBar.add(menu); // FIRST ITEM - NEW MATCH menuItem = new JMenuItem("New Match", KeyEvent.VK_M); //menuItem.setMnemonic(KeyEvent.VK_M); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Begins New Match"); menuItem.addActionListener(this); menu.add(menuItem); // SECOND ITEM - NEW GAME menuItem = new JMenuItem("New Game", KeyEvent.VK_G); //menuItem.setMnemonic(KeyEvent.VK_M); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Begins New Game"); menuItem.addActionListener(this); menu.add(menuItem); // THIRD ITEM - DISPLAY SAVED GAME submenu = new JMenu("Display Saved Game"); submenu.setMnemonic(KeyEvent.VK_D); menuItem = new JMenuItem("First Game"); menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_2, ActionEvent.ALT_MASK)); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Second Game"); menuItem.addActionListener(this); submenu.add(menuItem); menu.add(submenu); // FOURTH ITEM - HIGH SCORE BOARD menu.addSeparator(); menuItem = new JMenuItem("High Scores", KeyEvent.VK_I); //menuItem.setMnemonic(KeyEvent.VK_I); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "View HighScore Board"); menuItem.addActionListener(this); menu.add(menuItem); // FIFTH ITEM - SAVE GAME menu.addSeparator(); menuItem = new JMenuItem("Save Game", KeyEvent.VK_S); //menuItem.setMnemonic(KeyEvent.VK_S); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Saves Current Game/Match"); menuItem.addActionListener(this); menu.add(menuItem); // SIXTH ITEM - EXIT GAME menuItem = new JMenuItem("Exit", KeyEvent.VK_X); //menuItem.setMnemonic(KeyEvent.VK_X); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Exits Game"); menuItem.addActionListener(this); menu.add(menuItem); // SECOND MENU - OPTIONS menu = new JMenu("Options"); menu.setMnemonic(KeyEvent.VK_O); menu.getAccessibleContext().setAccessibleDescription( "Displays Options Items"); menuBar.add(menu); // FIRST ITEM - SORT BY SUIT ImageIcon iconSuit = new ImageIcon("images/CARDS_Heart.gif"); menuItem = new JMenuItem("Sort By Suit", iconSuit); menuItem.setMnemonic(KeyEvent.VK_S); menuItem.addActionListener(this); menu.add(menuItem); // SECOND ITEM - NUMERICAL SORT ImageIcon iconNum = new ImageIcon("images/CARDS_123.gif"); menuItem = new JMenuItem("Numerical Sort", iconNum); menuItem.setMnemonic(KeyEvent.VK_N); menuItem.addActionListener(this); menu.add(menuItem); // THIRD ITEM - RESTORE CARD ORDER ImageIcon iconRestore = new ImageIcon("images/RESTORE.gif"); menuItem = new JMenuItem("Restore Card Order", iconRestore); menuItem.setMnemonic(KeyEvent.VK_R); menuItem.addActionListener(this); menu.add(menuItem); // THIRD MENU - VIEW menu = new JMenu("View"); menu.setMnemonic(KeyEvent.VK_V); menu.getAccessibleContext().setAccessibleDescription( "Displays View Items"); menuBar.add(menu); // FIRST ITEM - BACKGROUND SUBMENU submenu = new JMenu("Background Color"); submenu.setMnemonic(KeyEvent.VK_B); menuItem = new JMenuItem("Red"); menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_2, ActionEvent.ALT_MASK)); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Green"); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Blue"); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Cyan"); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Magenta"); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Yellow"); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Black"); menuItem.addActionListener(this); submenu.add(menuItem); menuItem = new JMenuItem("Gray"); menuItem.addActionListener(this); menuItem = new JMenuItem("White"); menuItem.addActionListener(this); submenu.add(menuItem); menu.add(submenu); // SECOND ITEM - BACKGROUND SCENERY menuItem = new JMenuItem("Background Scenery", KeyEvent.VK_S); //menuItem.setMnemonic(KeyEvent.VK_S); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Displays Background Scenery Choices"); menuItem.addActionListener(this); menu.add(menuItem); // THIRD ITEM - CARD BACKS menuItem = new JMenuItem("Card Backs", KeyEvent.VK_C); //menuItem.setMnemonic(KeyEvent.VK_C); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Shows Card-Back Image Choices"); menuItem.addActionListener(this); menu.add(menuItem); // THIRD ITEM - CARD FACES menuItem = new JMenuItem("Card Faces", KeyEvent.VK_F); //menuItem.setMnemonic(KeyEvent.VK_F); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Shows Card-Face Image Choices"); menuItem.addActionListener(this); menu.add(menuItem); // FOURTH ITEM - SOUNDS menuItem = new JMenuItem("Sounds", KeyEvent.VK_S); //menuItem.setMnemonic(KeyEvent.VK_S); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Displays Sounds Choices"); menuItem.addActionListener(this); menu.add(menuItem); // FOURTH MENU - PREFERENCES menu = new JMenu("Preferences"); menu.setMnemonic(KeyEvent.VK_P); menu.getAccessibleContext().setAccessibleDescription( "Displays Preference Items"); menuBar.add(menu); // FIRST ITEM - PLAY OFF-LINE AGAINST THE //COMPUTER *DEFAULT* menuItem = new JMenuItem("Play Off-Line, Against Computer", KeyEvent.VK_F); //menuItem.setMnemonic(KeyEvent.VK_M); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Play Off-Line, against the Computer"); menuItem.addActionListener(this); menu.add(menuItem); // SECOND ITEM - PLAY ON-LINE, AGAINST ANOTHER PERSON menuItem = new JMenuItem("Play On-Line, Against Another Person", KeyEvent.VK_N); //menuItem.setMnemonic(KeyEvent.VK_N); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Play On-Line, against another person"); menuItem.addActionListener(this); menu.add(menuItem); // THIRD ITEM - PLAY ON INTRANET, AGAINST ANOTHER PERSON menuItem = new JMenuItem("Play On Intranet, Against Another Person", KeyEvent.VK_I); //menuItem.setMnemonic(KeyEvent.VK_I); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Play On Intranet, against another person"); menuItem.addActionListener(this); menu.add(menuItem); // FOURTH MENU - HELP menu = new JMenu("Help"); menu.setMnemonic(KeyEvent.VK_H); menu.getAccessibleContext().setAccessibleDescription( "Displays Help Items"); menuBar.add(menu); // FIRST ITEM - RULES menuItem = new JMenuItem("Rules", KeyEvent.VK_R); //menuItem.setMnemonic(KeyEvent.VK_R); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Displays Game Rules"); menuItem.addActionListener(this); menu.add(menuItem); // SECOND ITEM - HELP TOPICS menuItem = new JMenuItem("Help Topics", KeyEvent.VK_H); //menuItem.setMnemonic(KeyEvent.VK_H); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Displays Help Index"); menuItem.addActionListener(this); menu.add(menuItem); // THIRD ITEM - ABOUT menu.addSeparator(); menuItem = new JMenuItem("About", KeyEvent.VK_A); //menuItem.setMnemonic(KeyEvent.VK_A); //used constructor instead menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription( "Displays Version/Copyright Info."); menuItem.addActionListener(this); menu.add(menuItem); return menuBar; } public CardsHandsFrame(Image cards) { // THE CONSTRUCTOR super("Card Game"); // WINDOW TITLE cardImages = cards; setJMenuBar(create menuBar); setBackground(Color.white); CardsHandsFrameCanvas board = new CardsHandsFrameCanvas(); getContentPane().add(board, BorderLayout.CENTER); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // THE FOLLOWING WILL CLOSE THE WINDOW addWindowListener( new WindowAdapter() { // GIVE USER CHANCE TO CHANGE HIS/HER MIND public void windowClosing(WindowEvent evt) { int response = JOptionPane.showConfirmDialog(null, "Do you really want to quit?"); if (response == JOptionPane.YES_OPTION) { dispose(); if (runStandAlone) System.exit(0); } } } pack(); // Make the frame just large enough to hold // the components that it contains. setResizable(false); setLocation(150,100); show(); } // end constructor class CardsHandsFrameCanvas extends JPanel implements ActionListener { Deck deck; Hand hand; String message; boolean gameInProgress; Font bigFont; Font smallFont; CardsHandsFrameCanvas() { setPreferredSize(new Dimension(510,410)); setBackground( new Color(88,152,171) ); setForeground( Color.black ); smallFont = new Font("SansSerif", Font.PLAIN, 12); bigFont = new Font("Serif", Font.BOLD, 14); } // end constructor public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem)(e.getSource()); } public void itemStateChanged(ItemEvent e) { JMenuItem source = (JMenuItem)(e.getSource()); } public void paintComponent(Graphics g) { super.paintComponent(g); int cardCt = hand.getCardCount(); for (int i = 0; i < cardCt; i++) drawCard(g, hand.getCard(i), 30 + i * 70, 15); if (gameInProgress) drawCard(g, null, 30 + cardCt * 70, 15); } // end paintComponent() void drawCard(Graphics g, Card card, int x, int y) { if (card == null) { // Draw a face-down card g.setColor(Color.blue); g.fillRect(x,y,71,96); g.setColor(Color.white); g.drawRect(x+5,y+5,71,96); g.drawRect(x+35,y+5,71,96); } else { int row = 0; switch (card.getSuit()) { case Card.HEARTS: row = 0; break; case Card.CLUBS: row = 1; break; case Card.DIAMONDS: row = 2; break; case Card.SPADES: row = 3; break; } int sx, sy; // coords of upper left corner in the source image. sx = 71*(card.getValue() - 1); sy = 96*row; g.drawImage(cardImages, x, y, x+71, y+96, sx, sy, sx+71, sy+96, this); } } } // end nested class CardsHandsFrameCanvas } // end class CardsHandsFrame I made a list of things I needed, then took them off the list, above, as I learned/implemented them; the remainder I posted, here. I thought that would be better than comin'-back SIX times!!Also, obviously, I took the program apart; posted the problem areas; and then, posted the shortest part of the program. With any of the items I've asked about, any help would be, GREATLY, appreciated!! I've taken a dozen (okay---maybe not a dozen) tutorials---I searched these forums---I have the API---java.sun.com drives me insane---I can't figure anything out... A MILLION THANKS, in advance!!! TheTree |
| All times are GMT +1. The time now is 08:28 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.