|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Nov 2011
Messaggi: 6
|
[Java]Gestione grafica lancio dei dadi
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; |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Prendi questo pezzo del tuo programma:
Codice:
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);
}
}
Codice:
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);
}
}
Ed è sempre lo stesso se scriviamo: Codice:
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);
}
}
}
Codice:
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)
}
}
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.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Nov 2011
Messaggi: 6
|
package grafica;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? |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Mettici degli if:
Codice:
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);
}
}
Codice:
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);
}
}
}
Codice:
MyPanel p=new MyPanel(); b.addActionListener(new BActionListener(p));
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:03.



















