Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to copy

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to copy

    Hello forum members.

    I need some help with my dice game.
    Since I have to program object oriented I dont want to copy everything but make some kind of function.
    Only problem is that I dont know where I should make a function.

    import javax.swing.*;
     
    import java.awt.event.*;
    import java.awt.*;
     
    public class ImplementatieDobbelsteen extends JFrame {
    	public static void main(String[] args) {
    		JFrame frame = new ImplementatieDobbelsteen();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(600, 400);
    		frame.setResizable(false);
    		frame.setContentPane(new Paneel());
    		frame.setVisible(true);
    	}
    }
     
    class Paneel extends JPanel {
    	Dobbelsteen dobbelsteen1, dobbelsteen2; //dobbelstenen maken
    	JButton gooi;
     
    	public Paneel(){
    		dobbelsteen1 = new Dobbelsteen();
    		gooi = new JButton("Gooi");
    		gooi.addActionListener(new GooiHandler());
    		gooi.setBounds(getVisibleRect()); //in het midden zetten
    		add(gooi);
     
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		int a = dobbelsteen1.getAantal(); //a krijgt de waarde van dobbelsteen
     
    		g.drawRect(100, 100, 100, 100);
     
    		/*a word gebruikt voor de ogen van de dobbelsteen*/
    		if(a == 1) {
    			g.fillOval(140, 140, 20, 20);
    		}
     
    		if(a == 2) {
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 3) {
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(140, 140, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 4) {
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(160, 120, 20, 20);
    			g.fillOval(120, 160, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 5) {
    			g.fillOval(140, 140, 20, 20);
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(160, 120, 20, 20);
    			g.fillOval(120, 160, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 6) {
    			g.fillOval(120, 115, 20, 20);
    			g.fillOval(120, 140, 20, 20);
    			g.fillOval(120, 165, 20, 20);
    			g.fillOval(160, 115, 20, 20);
    			g.fillOval(160, 140, 20, 20);
    			g.fillOval(160, 165, 20, 20);
    		}
    	}
     
    	class GooiHandler implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			dobbelsteen1.gooi();
    			repaint();
    			System.out.println(dobbelsteen1.getAantal());
    		}
    	}
    }

    And here is my dice class

    public class Dobbelsteen {
    	int aantal;
     
    	public Dobbelsteen() { //constructor
    		gooi(); 
    	}
     
    	public void gooi() { //gooi functie
    		aantal = (int) (6 * Math.random() + 1);
    	}
     
    	public int getAantal() { //return aantal ogen van dobbelsteen
    		return aantal;
    	}
    }


    Now I need to make the exact same dice but on the right of the other dice.
    So they dont overlap each other.

    Can someone give me some advice on how to do so?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to copy

    If you are asking about how to position components in the GUI, look at the layout managers to see which one will do what you want:
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to copy

    Thats not what I mean.

    I dont want to copy and past this part
    public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		int a = dobbelsteen1.getAantal(); //a krijgt de waarde van dobbelsteen
     
    		g.drawRect(100, 100, 100, 100);
     
    		/*a word gebruikt voor de ogen van de dobbelsteen*/
    		if(a == 1) {
    			g.fillOval(140, 140, 20, 20);
    		}
     
    		if(a == 2) {
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 3) {
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(140, 140, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 4) {
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(160, 120, 20, 20);
    			g.fillOval(120, 160, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 5) {
    			g.fillOval(140, 140, 20, 20);
    			g.fillOval(120, 120, 20, 20);
    			g.fillOval(160, 120, 20, 20);
    			g.fillOval(120, 160, 20, 20);
    			g.fillOval(160, 160, 20, 20);
    		}
     
    		if(a == 6) {
    			g.fillOval(120, 115, 20, 20);
    			g.fillOval(120, 140, 20, 20);
    			g.fillOval(120, 165, 20, 20);
    			g.fillOval(160, 115, 20, 20);
    			g.fillOval(160, 140, 20, 20);
    			g.fillOval(160, 165, 20, 20);
    		}
    	}

    And then change the coordinates.

    I want this to be a function which draws the dices but the coordinates need to be different

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    92
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: How to copy

    You should change all your hard-coded numbers to variables. x, y, width, and height. And I would make a separate function for each die number that draws it in the size/location it's told to:

    int diceX = ???;
    int diceY = ???;
    int diceWidth = ???;
    int diceHeight = ???;
     
    if (a == 1) {
     drawDie1(g, diceX, diceY, diceWidth, diceHeight);
    } else if (a == 2) {
     drawDie2(g, diceX, diceY, diceWidth, diceHeight);
    }
     
    ...
     
    private void drawDie1(Graphics g, int x, int y, int width, int height) {
     g.fillOval(width / 2 + x, height / 2 + y, 20, 20);
    }
     
    private void drawDie2(Graphics g, int x, int y, int width, int height) {
     int centerX = width / 3 + x;
     int centerY = height / 3 + y;
     
     g.fillOval(width / 3 + x, height / 3 + y, 20, 20);
     g.fillOval(width * 2 / 3 + x, height * 2 / 3 + y, 20, 20);
    }

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to copy

    Looks great, just genius!!

    Should be obvious but experience comes with time.

    Thanks for the information!

Similar Threads

  1. Copy Constructor
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2021, 09:12 PM
  2. copy values from arraylist to another
    By e93 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 2nd, 2014, 05:47 PM
  3. array copy!!!
    By Username in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 13th, 2013, 02:53 PM
  4. I need help for this copy byte code ...
    By ricaclaire16 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2012, 08:00 AM
  5. Deep copy of ArrayList?
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2011, 01:30 PM