如何从另一个类中访问画布?

时间:2015-06-03 10:14:57

标签: java swing java-canvas

基本上,我试图访问在一个类中的方法中创建的画布,并在另一个类中使用该SAME画布,但是向其添加内容。我怎样才能参考画布。这是创建画布的类(重要的东西是第二个方法向下[例如,显示图形页面的那个]):

    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.util.Scanner;
    import java.lang.*;
/**
 * Write a description of class Battleship here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    public class Battleship extends JApplet
    {
    public Battleship()
    {
        String[][] pos = new String[10][10];
        pos[0][0] = "a0";
        pos[0][1] = "a1";
        pos[0][2] = "a2";
        pos[0][3] = "a3";
        pos[0][4] = "a4";
        pos[0][5] = "a5";
        pos[0][6] = "a6";
        pos[0][7] = "a7";
        pos[0][8] = "a8";
        pos[0][9] = "a9";
        pos[1][0] = "b0";
        pos[1][1] = "b1";
        pos[1][2] = "b2";
        pos[1][3] = "b3";
        pos[1][4] = "b4";
        pos[1][5] = "b5";
        pos[1][6] = "b6";
        pos[1][7] = "b7";
        pos[1][8] = "b8";
        pos[1][9] = "b9";
        pos[2][0] = "c0";
        pos[2][1] = "c1";
        pos[2][2] = "c2";
        pos[2][3] = "c3";
        pos[2][4] = "c4";
        pos[2][5] = "c5";
        pos[2][6] = "c6";
        pos[2][7] = "c7";
        pos[2][8] = "c8";
        pos[2][9] = "c9";
        pos[3][0] = "d0";
        pos[3][1] = "d1";
        pos[3][2] = "d2";
        pos[3][3] = "d3";
        pos[3][4] = "d4";
        pos[3][5] = "d5";
        pos[3][6] = "d6";
        pos[3][7] = "d7";
        pos[3][8] = "d8";
        pos[3][9] = "d9";
        pos[4][0] = "e0";
        pos[4][1] = "e1";
        pos[4][2] = "e2";
        pos[4][3] = "e3";
        pos[4][4] = "e4";
        pos[4][5] = "e5";
        pos[4][6] = "e6";
        pos[4][7] = "e7";
        pos[4][8] = "e8";
        pos[4][9] = "e9";
        pos[5][0] = "f0";
        pos[5][1] = "f1";
        pos[5][2] = "f2";
        pos[5][3] = "f3";
        pos[5][4] = "f4";
        pos[5][5] = "f5";
        pos[5][6] = "f6";
        pos[5][7] = "f7";
        pos[5][8] = "f8";
        pos[5][9] = "f9";
        pos[6][0] = "g0";
        pos[6][1] = "g1";
        pos[6][2] = "g2";
        pos[6][3] = "g3";
        pos[6][4] = "g4";
        pos[6][5] = "g5";
        pos[6][6] = "g6";
        pos[6][7] = "g7";
        pos[6][8] = "g8";
        pos[6][9] = "g9";
        pos[7][0] = "h0";
        pos[7][1] = "h1";
        pos[7][2] = "h2";
        pos[7][3] = "h3";
        pos[7][4] = "h4";
        pos[7][5] = "h5";
        pos[7][6] = "h6";
        pos[7][7] = "h7";
        pos[7][8] = "h8";
        pos[7][9] = "h9";
        pos[8][0] = "i0";
        pos[8][1] = "i1";
        pos[8][2] = "i2";
        pos[8][3] = "i3";
        pos[8][4] = "i4";
        pos[8][5] = "i5";
        pos[8][6] = "i6";
        pos[8][7] = "i7";
        pos[8][8] = "i8";
        pos[8][9] = "i9";
        pos[9][0] = "j0";
        pos[9][1] = "j1";
        pos[9][2] = "j2";
        pos[9][3] = "j3";
        pos[9][4] = "j4";
        pos[9][5] = "j5";
        pos[9][6] = "j6";
        pos[9][7] = "j7";
        pos[9][8] = "j8";
        pos[9][9] = "j9";
        boolean a0 = false;
        boolean a1 = false;
        boolean a2 = false;
        boolean a3 = false;
        boolean a4 = false;
        boolean a5 = false;
        boolean a6 = false;
        boolean a7 = false;
        boolean a8 = false;
        boolean a9 = false;
        boolean b0 = false;
        boolean b1 = false;
        boolean b2 = false;
        boolean b3 = false;
        boolean b4 = false;
        boolean b5 = false;
        boolean b6 = false;
        boolean b7 = false;
        boolean b8 = false;
        boolean b9 = false;
    }    
        public void paint(Graphics page)
    {
        page.fillRect(40, 50,500,2);//Lines From Here
        page.fillRect(40,100,500,2);
        page.fillRect(40,150,500,2);
        page.fillRect(40,200,500,2);
        page.fillRect(40,250,500,2);
        page.fillRect(40,300,500,2);
        page.fillRect(40,350,500,2);
        page.fillRect(40,400,500,2);
        page.fillRect(40,450,500,2);
        page.fillRect(40,500,500,2);
        page.fillRect(40,550,500,2);
        page.fillRect(40,50,2,500);
        page.fillRect(90,50,2,500);
        page.fillRect(140,50,2,500);
        page.fillRect(190,50,2,500);
        page.fillRect(240,50,2,500);
        page.fillRect(290,50,2,500);
        page.fillRect(340,50,2,500);
        page.fillRect(390,50,2,500);
        page.fillRect(440,50,2,500);
        page.fillRect(490,50,2,500);
        page.fillRect(540,50,2,500);//To Here
        page.drawString("A", 60, 40);//X Axis From Here
        page.drawString("B", 110, 40);
        page.drawString("C", 160, 40);
        page.drawString("D", 210, 40);
        page.drawString("E", 260, 40);
        page.drawString("F", 310, 40);
        page.drawString("G", 360, 40);
        page.drawString("H", 410, 40);
        page.drawString("I", 460, 40);
        page.drawString("J", 510, 40);//To Here
        page.drawString("0", 15, 80);//Y Axis From Here
        page.drawString("1", 15, 130);
        page.drawString("2", 15, 180);
        page.drawString("3", 15, 230);
        page.drawString("4", 15, 280);
        page.drawString("5", 15, 330);
        page.drawString("6", 15, 380);
        page.drawString("7", 15, 430);
        page.drawString("8", 15, 480);
        page.drawString("9", 15, 530);//To Here
    }
    }

第二课:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.util.Scanner;
    import java.lang.*;
/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    public class Game extends Battleship
    {
        public void Guess()
    {

        Scanner next = new Scanner(System.in);
        String input = next.nextLine();
        int random = (int)Math.random()*100;
        int random2 = (int)random + 1;
    }
    }

请参阅,如何从第一个类访问页面/画布并从该类中进行绘制?

0 个答案:

没有答案