Saturday, January 4, 2014

Help with rectangle

Please help me with this, i'm trying to create a domino game (which a project in school) i'm a midway learner of Java Prog, but still i haven't know a lot. My problem is, i'm trying to draw over the background image of my playFrame, but the rectangle does not seem to show, but when i tried to remove the image, it showed up. so How can i put the rectangle over the image? Thank you for helping! Good day!



import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; class MyCanvas extends JComponent { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawRect (10, 10, 200, 200); } } public class DominoTRY extends JFrame implements ActionListener { Font font1 = new Font("Century Gothic", Font.BOLD,20); Font font2 = new Font("Century Gothic",Font.PLAIN,14); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); JOptionPane disp = new JOptionPane(); JFrame mainFrame = new JFrame(); JFrame instFrame = new JFrame("How to Play"); JFrame playFrame = new JFrame("Domino Game"); JButton startBtn = new JButton("START"); JButton instBtn = new JButton ("Instructions"); JButton exitBtn = new JButton ("Exit"); JButton retBtn = new JButton("Return to Menu"); JLabel background = new JLabel(new ImageIcon("domino01.jpg")); JLabel background2 = new JLabel(new ImageIcon("dominobg.jpg")); JTextArea instArea = new JTextArea(5,22); SpringLayout layout = new SpringLayout(); public void startMenu() { mainFrame.setLocationRelativeTo(null); mainFrame.setSize(screenSize); mainFrame.setBounds(0,0,screenSize.width,screenSize.height); mainFrame.setDefaultCloseOperation(JFrame.EXITONCLOSE); mainFrame.setLayout(new BorderLayout()); startBtn.setBounds(55,80,155,70); instBtn.setBounds(55,160,155,70); exitBtn.setBounds(55,240,155,70); startBtn.setBackground(Color.WHITE); startBtn.setForeground(Color.RED); startBtn.setContentAreaFilled(false); startBtn.setOpaque(true); instBtn.setBackground(Color.WHITE); instBtn.setContentAreaFilled(false); instBtn.setOpaque(true); exitBtn.setBackground(Color.WHITE); exitBtn.setContentAreaFilled(false); exitBtn.setOpaque(true); startBtn.setFont(font1); instBtn.setFont(font1); exitBtn.setFont(font1); background.add(startBtn); background.add(instBtn); background.add(exitBtn); mainFrame.add(background); startBtn.addActionListener(this); instBtn.addActionListener(this); exitBtn.addActionListener(this); mainFrame.setVisible(true); } public void startGame() { mainFrame.dispose(); playFrame.setLayout(new BorderLayout()); playFrame.setSize(screenSize); playFrame.setBounds(0,0,screenSize.width,screenSize.height); playFrame.setDefaultCloseOperation(JFrame.EXITONCLOSE); background2.add(new MyCanvas()); background2.add(retBtn); playFrame.add(background2); playFrame.setResizable(false); playFrame.setVisible(true); }//startGame public void instructions() { instArea.setEditable(false); instFrame.setLayout(new FlowLayout()); instFrame.setSize(310,500); instFrame.setLocationRelativeTo(mainFrame); instFrame.setAlwaysOnTop(true); instArea.setLineWrap(true); instArea.setWrapStyleWord(true); instArea.setFont(font2); instArea.setText("How to play Domino Game:n The game is played by matching one number of the domino to the same number of another domino, thus connecting it. Played by players, it starts with shuffling the domino DECK stacks and putting the topmost domino in the PLAYFIELD. Each player has their TURN in which they will draw the topmost domino and add it to their HAND. TURN active player should match either number (x,y) of the active domino in the PLAYFIELD with one of his domino in HAND; otherwise if they dont have domino with numbers to match, he should draw from the DECK until he found a matching domino. Active dominoes in the PLAYFIELD are those pieces without pair yet. A TURN ends when the player finally puts a piece in the PLAYFIELD. The game ends when the DECK is depleted and the player with the least domino in HAND will be the winner"); instFrame.add(instArea); instFrame.setResizable(false); instFrame.setVisible(true); } public static void main(String[] args) { DominoTRY dominotry = new DominoTRY(); dominotry.startMenu(); }//void main public void actionPerformed(ActionEvent e) { Object btnClicked = e.getSource(); if(btnClicked == startBtn) startGame(); else if(btnClicked == instBtn) instructions(); else if(btnClicked == exitBtn) { int reply = disp.showConfirmDialog(mainFrame,"Exit game?","",JOptionPane.YESNOOPTION); if(reply==JOptionPane.YESOPTION) System.exit(0); } }//actionPerformed }//class DominoTRY

Full Post

No comments:

Post a Comment