如何在这里实现我的最后一个方法(paint)?

问题描述:

所以,我有点写这个游戏的代码叫做“河内塔”。 我以前的代码显示了控制台中每一步的动作。我试图用图形来展示这些动作。如何在这里实现我的最后一个方法(paint)?

我想我来了很远,但我不能实施绘制方法。

我试过几件事情像g.drawRect(s1.x,s1.y+200,s1.width,s1.height);但它只是表示矩形,没有移动等

继承人我的代码:

import java.io.*; 
import java.util.*; 
import java.awt.*; 

public class hanoi { 

    public static void ziehe_scheibe(MyFrame f,Pole von, Pole nach) { 
    int hs = von.onPole.size(); 
    int ht = nach.onPole.size(); 
    int hy = -25*(ht-hs+1); 
    int hx = (nach.xPos - von.xPos); 

    String d = von.scheibenehmen(); 
    nach.scheibelegen(d); 
    f.moveDisk(d,hx,hy); 
    } 


    public static void hanoi(MyFrame f,int n, Pole platz1, Pole platz2, Pole hilfsplatz) { 

     if (n==1) 
     ziehe_scheibe(f,platz1,platz2); 
    else 
    { hanoi(f,n-1,platz1,hilfsplatz,platz2); 
     ziehe_scheibe(f,platz1,platz2); 
     hanoi(f,n-1,hilfsplatz,platz2,platz1); 
    } 
    } 

    public static int xSize(String d) { 
    if (d == "klein") return 30; 
    if (d == "mittel") return 40; 
    else return 50; 
    } 

    public static void main(String args[]) { 

    Pole p1 = new Pole("a",60); 
    Pole p2 = new Pole("b",180); 
    Pole p3 = new Pole("c",300); 

    p1.scheibelegen("groß"); 
    p1.scheibelegen("mittel"); 
    p1.scheibelegen("klein"); 

    Rectangle r1 = new Rectangle(p1.xPos-50,50,100,20);  
    Rectangle r2 = new Rectangle(p1.xPos-40,25,80,20);  
    Rectangle r3 = new Rectangle(p1.xPos-30,0,60,20); 

    MyFrame fff = new MyFrame(r1,r2,r3); 
    fff.setSize(800,600); 
    fff.setVisible(true); 

    hanoi(fff,3,p1,p3,p2); 
    } 

} 
// Frame-Klasse zur Anzeige des Zustandes 
class MyFrame extends Frame { 

    public Rectangle s1, s2, s3; 


    public MyFrame(Rectangle a, Rectangle b, Rectangle c) { 
    s1 = a; s2 = b; s3 = c; 

    } 

    public void paint(Graphics g) { 


    } 

    public Rectangle xRect(String d) { 
    if (d == "klein") return s3; 
    if (d == "mittel") return s2; 
    else return s1; 
    } 

    public void moveDisk(String name,int dx,int dy) 
    { xRect(name).translate(dx,dy); 
    repaint(); 
    try 
    { System.in.read();System.in.read(); } 
    catch (Exception e) {} 
    } 
} 
// Pole-Klasse: Repräsentation eines Stabes 

class Pole { 
    public String label; 
    public Vector onPole; 
    public int xPos; 

    public Pole(String s, int i) { 
    onPole=new Vector(); 
    label = s; xPos = i; 
    } 

    public void scheibelegen(String d) { 
    this.onPole.addElement(d); 
    } 

    public String scheibenehmen() { 
    Object lastEl = this.onPole.lastElement(); 
    String lastElStr = lastEl.toString(); 
    this.onPole.removeElement(lastEl); 
    return lastElStr; 
    } 
} 

也许你们可以看看吧

顺便说一句原谅我的英语不好

+0

这是失败,因为你使用的''==代替'.equals'比较'String'值? –

+0

还是因为你没有编写任何代码来将'Pole'对象添加到'JFrame'? –

+0

好的,我改成了.equals,但还是没有任何反应。 –

尝试

public static int xSize(String d) { 
    if (d.equals("klein")) return 30; 
    if (d.equals("mittel")) return 40; 
    else return 50; 
    } 
+0

好吧,我做到了,但我仍然看到一个空白的窗口。我的绘画方法仍然是空的:O –