PDA

View Full Version : [Java]Gestione grafica lancio dei dadi


MusashiDamian
09-02-2012, 13:59
Salve , sto facendo un programma chiamato "Dadi". In pratica , premuto il pulsante "Lancio dei dadi", dovrebbe uscirmi una faccia del dado a random.Solo che non so come collegare la mia classe class BActionListener implements ActionListener con la classe class MyPanel extends JPanel.Cioè , come faccio ,attraverso un numero random,a fargli disegnare,ad esempio se esce 1,solo la pallina al centro.Oppure nel caso esca 6,3 palline orizzontali sopra e sotto.A voi il programma :

package grafica;

import grafica.EsB.BActionListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Dadi extends JFrame {
JPanel c;
JButton b;
public Dadi(){
super("Lancio dei dadi");
setDefaultCloseOperation(EXIT_ON_CLOSE);
b=(new JButton("Lancio dadi"));
c=new JPanel();
//Container contentPane = getContentPane();
this.getContentPane().add(b,BorderLayout.NORTH);
this.getContentPane().add(c,BorderLayout.SOUTH);
b.addActionListener(new BActionListener());
MyPanel p=new MyPanel();
this.add(p);
this.setVisible(true);
}

public static void main(String[] args) {
new Dadi();




}
class BActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int numeroCasuale;
String etic=e.getActionCommand();
if(etic.compareTo("Lancio dei dadi")==0){
numeroCasuale=(int)(Math.random()*6);
if(numeroCasuale==1){

}
}
}



}
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
int x = (this.getWidth()/2)-45;
int y = (this.getHeight()/2)-45;
g.drawOval(x,y,90,90);
g.drawOval(0,0,90,90);

}

}

}

PGI-Bis
09-02-2012, 14:40
Prendi questo pezzo del tuo programma:

class MyPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
int x = (this.getWidth()/2)-45;
int y = (this.getHeight()/2)-45;
g.drawOval(x,y,90,90);
g.drawOval(0,0,90,90);
}
}

E' lo stesso se lo trasformiamo in:

class MyPainter {

public void doPaint(Component c, Graphics g) {
int x = (c.getWidth() / 2) - 45;
int y = (c.getHeight() / 2) - 45;
g.drawOval(x,y,90,90);
g.drawOval(0,0,90,90)
}
}

class MyPanel extends JPanel {

MyPainter painter = new MyPainter();

public void paintComponent(Graphics g) {
super.paintComponent(g);
painter.doPaint(this, g);
}
}

Giusto? Che abbiamo fatto: pigliamo quello che prima c'era nel paint component e lo spostiamo nel doPaint (nome a piacere) di un'altra classe, in MyPanel c'è un MyPainter e nel paint lo usiamo.

Ed è sempre lo stesso se scriviamo:

interface Painter {
void doPaint(Component c, Graphics g);
}

class MyPainter implements Painter {

public void doPaint(Component c, Graphics g) {
int x = (c.getWidth() / 2) - 45;
int y = (c.getHeight() / 2) - 45;
g.drawOval(x,y,90,90);
g.drawOval(0,0,90,90)
}
}

class MyPanel extends JPanel {

Painter painter = null = new MyPainter();

public void setPainter(Painter p) {
this.painter = p;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
if(painter != null) {
painter.doPaint(this, g);
}
}
}

O no? Le operazioni sono sempre quelle. Ma ecco cosa possiamo scrivere adesso:

class Uno implements Painter {

public void doPaint(Component c, Graphics g) {
int x = c.getWidth() / 2 - 20;
int y = c.getHeight() / 2 - 20;
g.fillOval(x, y, 20, 20);
}
}

class Due implements Painter {

public void doPaint(Component c, Graphics g) {
int x = c.getWidth() / 2 - 20;
int y = c.getHeight() / 2 - 20;
g.fillOval(x, y - 30, 20, 20);
g.fillOval(x, y + 30, 20, 20);
}
}

class Tre implements Painter {

public void doPaint(Component c, Graphics g) {
int x = c.getWidth() / 2 - 20;
int y = c.getHeight() / 2 - 20;
g.fillOval(x - 30, y - 30, 20, 20)
g.fillOval(x, y, 20, 20)
g.fillOval(x + 30, y + 30, 20, 20)
}
}

(Qui le x e le y sono un po' a naso ma è il pensiero che conta).

Se voglio disegnare l'uno dirò:

istanzaDiMyPanel.setPainter(new Uno());
istanzaDiMyPanel.repaint();

Se il due:

istanzaDiMyPanel.setPainter(new Due());
istanzaDiMyPanel.repaint();

eccetera. A questo punto si tratta solo di trovare le coordinate dei pallini.

MusashiDamian
09-02-2012, 14:55
:) Grazie mille ma c'è un problema : è un pò troppo complicato per me :confused: ... comunque sono andato avanti e ho creato tutti i miei 7 cerchi
package grafica;

import grafica.EsB.BActionListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Dadi extends JFrame {
JPanel c;
JButton b;
public Dadi(){
super("Lancio dei dadi");
setDefaultCloseOperation(EXIT_ON_CLOSE);
b=(new JButton("Lancio dadi"));
c=new JPanel();
//Container contentPane = getContentPane();
this.getContentPane().add(b,BorderLayout.NORTH);
this.getContentPane().add(c,BorderLayout.SOUTH);
b.addActionListener(new BActionListener());
MyPanel p=new MyPanel();
this.add(p);
setSize(300,100);


}

public static void main(String[] args) {
Dadi a=new Dadi();
a.setSize(600,600);
a.setLocation(200,200);
a.setResizable(false);
a.setVisible(true);




}
class BActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int numeroCasuale;
String etic=e.getActionCommand();
if(etic.compareTo("Lancio dei dadi")==0){
numeroCasuale=(int)(Math.random()*6);
if(numeroCasuale==1){

}
}
}



}
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
int x = (this.getWidth()/2)-45;
int y = (this.getHeight()/2)-45;
g.fillOval(x,y,90,90);
g.fillOval(0,0,90,90);
g.fillOval(503,0,90,90);
g.fillOval(503,446,90,90);
g.fillOval(0,446,90,90);
g.fillOval(251,0,90,90);
g.fillOval(251,446,90,90);

}

}

}




Non c'è un modo per usare la classe MyPanel in quella BActionListener?Ad esempio se esce il numero due mi crea solo due cerchi a mia scelta?

PGI-Bis
09-02-2012, 18:14
Mettici degli if:

class MyPanel extends JPanel{
private int number = 0

public void setNumber(int n) {
number = n;
repaint();
}

public void paintComponent(Graphics g){
super.paintComponent(g);
int x = (this.getWidth()/2)-45;
int y = (this.getHeight()/2)-45;
if(n == 1 || n == 1 || n == qualcosa) g.fillOval(x,y,90,90);
if(n == 1 || n == 5 || n == qualcos'altro) g.fillOval(0,0,90,90);
if(eccetera) g.fillOval(503,0,90,90);
if(eccetera) g.fillOval(503,446,90,90);
if(eccetera) g.fillOval(0,446,90,90);
if(eccetera) g.fillOval(251,0,90,90);
if(eccetera) g.fillOval(251,446,90,90);
}

}

L'action listener lo fai diventare:

class BActionListener implements ActionListener{

MyPanel display;

BAActionListener(MyPanel p) {
this.display = p;
}

public void actionPerformed(ActionEvent e){
int numeroCasuale;
String etic=e.getActionCommand();
if(etic.compareTo("Lancio dei dadi")==0){
numeroCasuale=(int)(Math.random()*6);
this.display.setNumber(i);
}
}
}

E nel costruttore di Dati inverti due linee:


MyPanel p=new MyPanel();
b.addActionListener(new BActionListener(p));