Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Roll two sets of dice when window is resized.

Posted on 2007-10-18
1
Medium Priority
?
165 Views
Last Modified: 2013-11-23
Hello,

I'm a kook.  

The dice on the window are supposed to change pips once the panel is resized.  Top row is complete.  Bottom row is supposed to change pips every 100 or so times. I can't figure out how to do this.  Thank you for your patience.

This has taken me more than twelve hours.  I'm not trying to get anyone to do my homework.  I simply need help.

Please..


package dicesample;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Face extends JPanel{
   
    private int faceValue;
    private int faceValue1;
    private int faceValue2=5;
    private int faceValue3=1;
   
    public Face() {
        setBackground(Color.BLACK);
    }
   
    public void paintComponent(Graphics g){
        faceValue = (int) (6*Math.random()+1);
        faceValue1=(int) (6*Math.random()+1);
//         faceValue2 =(int) (6*Math.random()+1);
////        faceValue3=(int) (6*Math.random()+1);
       
        super.paintComponent(g);
       
        rollDie(g, 40,40,faceValue);
        rollDie(g, 150,40,faceValue1);
        rollDie(g, 40,160, faceValue2);
        rollDie(g, 150,160,faceValue3 );
       
    }
    public void rollDie( Graphics g, int x, int y, int faceValue) {
       
        g.setColor(Color.WHITE);
        g.fillRect(x,y,105,105);
        g.setColor(Color.RED);
        g.drawRect(x,y,105,105);
       
       
        switch( faceValue){
            case 1:
                g.setColor(Color.BLACK);
                g.fillOval(x+45,y+45,15,15);
                break;
               
            case 2:
                g.setColor(Color.BLACK);
                g.fillOval(x+15,y+15,15,15);
                g.fillOval(x+75,y+75,15,15);
                break;
            case 3:
                g.setColor(Color.BLACK);
                g.fillOval(x+15,y+15,15,15);
                g.fillOval(x+45,y+45,15,15);
                g.fillOval(x+75,y+75,15,15);
                break;
               
            case 4:
               
                g.setColor(Color.BLACK);
                g.fillOval(x+15,y+15,15,15);
                g.fillOval(x+75,y+15,15,15);
                g.fillOval(x+45,y+45,15,15);
                g.fillOval(x+15,y+75,15,15);
                g.fillOval(x+75,y+75,15,15);
                break;
               
            case 5:
               
                g.setColor(Color.BLACK);
                g.fillOval(x+15,y+15,15,15);
                g.fillOval(x+75,y+15,15,15);
                g.fillOval(x+45,y+45,15,15);
                g.fillOval(x+15,y+75,15,15);
                g.fillOval(x+75,y+75,15,15);
                break;
               
            case 6:
               
                g.setColor(Color.BLACK);
                g.fillOval(x+15,y+15,15,15);
                g.fillOval(x+45,y+15,15,15);
                g.fillOval(x+75,y+15,15,15);
                g.fillOval(x+15,y+75,15,15);
                g.fillOval(x+45,y+75,15,15);
                g.fillOval(x+75,y+75,15,15);
                break;
        }
    }
   
    public void setFaceValue2(int _faceValue2) {
        int value=_faceValue2;
        int result = getFaceValue2();
        result +=faceValue2;
        if(result%100==0)faceValue2=(int) (6*Math.random()+1);
    }
   
    public int getFaceValue2() {
        return faceValue2;
    }
}

package dicesample;

import java.awt.Color;
import javax.swing.JFrame;

/**
 *
 * @author admin
 */
public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
   
    public static void main(String[] args) {
 
   
        Face panel = new Face();
       
        JFrame application = new JFrame();
       
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
     
        application.setSize(300,335);
        application.setVisible(true);
       
    }
   
}

0
Comment
Question by:mhening
1 Comment
 
LVL 15

Accepted Solution

by:
mr_egyptian earned 2000 total points
ID: 20116934
Since this is hw, here's a clue (assuming I understand what you're looking for). All you need in setFaceValue2() is to randomly set values for face2 and face3, then count how many times the window is resized, if it's a multiple of 100, run the function, if not,  leave it alone.

Hope that's enough to get you there.  I tested it and it only takes ~2 minutes to implement.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question