helo, can any one help to read one class from another class. i em knew with java actually. i got class a which has got sub class b static class. now i em trying to access class a from another class c using button. when in class c user click on button class a should load. is there any one there to help.... thank you in advance
Emint
kvprajapati 1,826 Posting Genius Team Colleague
emint,
Don't say "thank you in advance", we have to talk more yet.
I think you want to create an object of class A.
A.java
public class A
{
public A() // Constructor
{
System.out.println("A");
}
// class B is Sub class of A and it is static
public static class B extends A
{
public B() //Constructor
{
System.out.println("B");
}
}
}
C.java
public class C
{
public static void main(String []args){
A a=new A(); // Create an object of class A
A.B b=new A.B(); // Create an object of class B
}
}
emint 0 Junior Poster in Training
Thank you for your reply. yes i did what you said. it works but i got program as i told you earlier class A has got sub class B as static when i run them they works fine. but wen i try to from class c it give an error. i dun know where i m makin mistake. i m attachin my code here. you can have look.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Maze extends JFrame
{
public static String a, b, c;
public static final int TILE_SIZE = 20;
public Maze()
{
// super("Maze algorithm program");
RunDB runDB = new RunDB();
try{
runDB.loadDriver();
runDB.makeConnection();
runDB.buildStatement();
runDB.executeQuery();
String x;
x = runDB.text;
c = x.substring(6, 9);
b = x.substring(3, 6);
a = x.substring(0, 3);
}catch(Exception e){
e.printStackTrace();
}
}
String maze = a+b+c;
int width = (int)Math.sqrt(maze.length());
Screen screen;
void searchExit(Screen screen, int milliseconds) {
this.screen = screen;
int[] startCoordinates = getCoordinates(maze.indexOf("S"));
maze = visit(maze, startCoordinates[0], startCoordinates[1]);
}
String visit(String maze, int x, int y) {
screen.showMaze(maze,0);
if (foundExit(maze))
return maze;
if (isEmpty(maze, x - 1, y)) {
String newMaze = moveLeft(maze, x, y);
String visitedNewMaze = visit(newMaze, x - 1, y);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
if (isEmpty(maze, x, y - 1)) {
String newMaze = moveUp(maze, x, y);
String visitedNewMaze = visit(newMaze, x, y - 1);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
if (isEmpty(maze, x + 1, y)) {
String newMaze = moveRight(maze, x, y);
String visitedNewMaze = visit(newMaze, x + 1, y);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
if (isEmpty(maze, x, y + 1)) {
String newMaze = moveDown(maze, x, y);
String visitedNewMaze = visit(newMaze, x, y + 1);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
return maze;
}
int[] getCoordinates(int pos) {
return new int[] { pos % width, pos / width };
}
int getPosition(int x, int y) {
return y * width + x;
}
boolean isInsideMaze(String maze, int x, int y) {
if (x < 0 || x >= width)
return false;
if (y < 0 || y >= width)
return false;
int pos = getPosition(x, y);
return pos >= 0 && pos < maze.length();
}
char look(String maze, int x, int y) {
return maze.charAt(getPosition(x, y));
}
boolean isEmpty(String maze, int x, int y) {
if (!isInsideMaze(maze, x, y))
return false;
return look(maze, x, y) == 'P';
}
static int cnt = 0;
boolean isExit(String maze, int x, int y) {
if (!isInsideMaze(maze, x, y))
return false;
return look(maze, x, y) == 'E';
}
boolean isStone(String maze, int x, int y) {
if (!isInsideMaze(maze, x, y))
return false;
return look(maze, x, y) == '.';
}
boolean foundExit(String maze) {
cnt++;
if (cnt % 100 == 0)
System.out.println(cnt);
int[] exitCoordinates = getCoordinates(maze.indexOf('E'));
if (isStone(maze, exitCoordinates[0] - 1, exitCoordinates[1])) {
return true;
}
if (isStone(maze, exitCoordinates[0], exitCoordinates[1] - 1)) {
return true;
}
if (isStone(maze, exitCoordinates[0] + 1, exitCoordinates[1])) {
return true;
}
if (isStone(maze, exitCoordinates[0], exitCoordinates[1] + 1)) {
return true;
}
return false;
}
String setStone(String maze, int x, int y) {
int pos = getPosition(x, y);
return maze.substring(0, pos) + '.'
+ maze.substring(pos + 1, maze.length());
}
String moveUp(String maze, int x, int y) {
return setStone(maze, x, y - 1);
}
String moveDown(String maze, int x, int y) {
return setStone(maze, x, y + 1);
}
String moveLeft(String maze, int x, int y) {
return setStone(maze, x - 1, y);
}
String moveRight(String maze, int x, int y) {
return setStone(maze, x + 1, y);
}
static class Screen extends Maze {
int WIDTH = 500;
String maze = "";
public Screen() {
setSize(500,700 );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void addComponent(String region, Component component) {
Panel panel = new Panel();
panel.add(component);
add(region, panel); // make sure you add the panel!
}
public void paint(Graphics g) {
BufferedImage image = (BufferedImage) createImage(WIDTH, WIDTH);
Graphics g2 = image.getGraphics();
g2.setColor(Color.GRAY);
g2.fillRect(0, 0, WIDTH, WIDTH);
int n = (int) Math.sqrt(maze.length());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
char type = maze.charAt(i + j * n);
drawMazeArea(g2, i, j, n, type);
g2.fillRect(i*TILE_SIZE,j*TILE_SIZE,TILE_SIZE,TILE_SIZE);
g.setColor(g.getColor().darker());
g2.drawRect(i*TILE_SIZE,j*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
}
g.drawImage(image, 0, 0, this);
}
void drawMazeArea(Graphics g, int i, int j, int n, char type) {
if (type == 'O')
{
g.setColor(Color.BLUE);
}
if (type == 'P')
{
g.setColor(Color.WHITE);
g.fillRect('P'*TILE_SIZE,'P'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('P'*TILE_SIZE,'P'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
if (type == 'S')
{
g.setColor(Color.RED);
g.fillRect('S'*TILE_SIZE,'S'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('S'*TILE_SIZE,'S'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
if( type == '.')
{
g.setColor(Color.YELLOW);
g.fillRect('.'*TILE_SIZE,'.'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('.'*TILE_SIZE,'.'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
if (type == 'E')
{
g.setColor(Color.GREEN);
g.fillRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
int r = WIDTH / (n + 3);
int x = i * r + r, y = j * r + r, width = r, height = r;
g.fillRect(x, y, width, height);
g.setColor(Color.WHITE);
if (type == 'S')
g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
if (type == 'E'){
g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
g.setColor(Color.black); }
g.fillRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
g.setColor(g.getColor().darker());
g.drawRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
void showMaze(String maze, int milliseconds) {
int a = milliseconds;
a = 1500;
this.maze = maze;
repaint();
try {
Thread.sleep(a);
} catch (Exception e) {
}
}
public void update(Graphics g) {
paint(g);
}
}
public static void main(String [] a)
{ Screen screen = new Screen();
Maze maze = new Maze();
Button sm = new Button(" Solve Maze ");
Button exit = new Button(" Exit ");
TextField tb = new TextField(20 );
Label button = new Label(" Algorithm: ");
sm.setBackground(Color.green);
exit.setBackground(Color.red);
button.setBackground(Color.pink);
screen.add(button);
screen.add(tb );
screen.add(sm );
screen.add(exit );
screen.setLayout(new FlowLayout());
screen.setVisible(true);
int milliseconds = 4000;
maze.searchExit(screen, milliseconds);
}
}
Edited by Reverend Jim because: Fixed formatting
emint 0 Junior Poster in Training
Class Maze has got class Screen as sub class they works fine, i got another class C with load button. when i hit load button i want to run class Maze. hope you clicked me. thank you.
kvprajapati 1,826 Posting Genius Team Colleague
emint,
Use BB code tags. Source code must be surrounded with bb code tags. See # icon at toolbar. Read How to use bb code tag?
emint 0 Junior Poster in Training
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Maze extends JFrame
{
public static String a, b, c;
public static final int TILE_SIZE = 20;
public Maze()
{
// super("Maze algorithm program");
RunDB runDB = new RunDB();
try{
runDB.loadDriver();
runDB.makeConnection();
runDB.buildStatement();
runDB.executeQuery();
String x;
x = runDB.text;
c = x.substring(6, 9);
b = x.substring(3, 6);
a = x.substring(0, 3);
}catch(Exception e){
e.printStackTrace();
}
}
String maze = a+b+c;
int width = (int)Math.sqrt(maze.length());
Screen screen;
void searchExit(Screen screen, int milliseconds) {
this.screen = screen;
int[] startCoordinates = getCoordinates(maze.indexOf("S"));
maze = visit(maze, startCoordinates[0], startCoordinates[1]);
}
String visit(String maze, int x, int y) {
screen.showMaze(maze,0);
if (foundExit(maze))
return maze;
if (isEmpty(maze, x - 1, y)) {
String newMaze = moveLeft(maze, x, y);
String visitedNewMaze = visit(newMaze, x - 1, y);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
if (isEmpty(maze, x, y - 1)) {
String newMaze = moveUp(maze, x, y);
String visitedNewMaze = visit(newMaze, x, y - 1);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
if (isEmpty(maze, x + 1, y)) {
String newMaze = moveRight(maze, x, y);
String visitedNewMaze = visit(newMaze, x + 1, y);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
if (isEmpty(maze, x, y + 1)) {
String newMaze = moveDown(maze, x, y);
String visitedNewMaze = visit(newMaze, x, y + 1);
if (foundExit(visitedNewMaze))
return visitedNewMaze;
}
return maze;
}
int[] getCoordinates(int pos) {
return new int[] { pos % width, pos / width };
}
int getPosition(int x, int y) {
return y * width + x;
}
boolean isInsideMaze(String maze, int x, int y) {
if (x < 0 || x >= width)
return false;
if (y < 0 || y >= width)
return false;
int pos = getPosition(x, y);
return pos >= 0 && pos < maze.length();
}
char look(String maze, int x, int y) {
return maze.charAt(getPosition(x, y));
}
boolean isEmpty(String maze, int x, int y) {
if (!isInsideMaze(maze, x, y))
return false;
return look(maze, x, y) == 'P';
}
static int cnt = 0;
boolean isExit(String maze, int x, int y) {
if (!isInsideMaze(maze, x, y))
return false;
return look(maze, x, y) == 'E';
}
boolean isStone(String maze, int x, int y) {
if (!isInsideMaze(maze, x, y))
return false;
return look(maze, x, y) == '.';
}
boolean foundExit(String maze) {
cnt++;
if (cnt % 100 == 0)
System.out.println(cnt);
int[] exitCoordinates = getCoordinates(maze.indexOf('E'));
if (isStone(maze, exitCoordinates[0] - 1, exitCoordinates[1])) {
return true;
}
if (isStone(maze, exitCoordinates[0], exitCoordinates[1] - 1)) {
return true;
}
if (isStone(maze, exitCoordinates[0] + 1, exitCoordinates[1])) {
return true;
}
if (isStone(maze, exitCoordinates[0], exitCoordinates[1] + 1)) {
return true;
}
return false;
}
String setStone(String maze, int x, int y) {
int pos = getPosition(x, y);
return maze.substring(0, pos) + '.'
+ maze.substring(pos + 1, maze.length());
}
String moveUp(String maze, int x, int y) {
return setStone(maze, x, y - 1);
}
String moveDown(String maze, int x, int y) {
return setStone(maze, x, y + 1);
}
String moveLeft(String maze, int x, int y) {
return setStone(maze, x - 1, y);
}
String moveRight(String maze, int x, int y) {
return setStone(maze, x + 1, y);
}
static class Screen extends Maze {
int WIDTH = 500;
String maze = "";
public Screen() {
setSize(500,700 );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void addComponent(String region, Component component) {
Panel panel = new Panel();
panel.add(component);
add(region, panel); // make sure you add the panel!
}
public void paint(Graphics g) {
BufferedImage image = (BufferedImage) createImage(WIDTH, WIDTH);
Graphics g2 = image.getGraphics();
g2.setColor(Color.GRAY);
g2.fillRect(0, 0, WIDTH, WIDTH);
int n = (int) Math.sqrt(maze.length());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
char type = maze.charAt(i + j * n);
drawMazeArea(g2, i, j, n, type);
g2.fillRect(i*TILE_SIZE,j*TILE_SIZE,TILE_SIZE,TILE_SIZE);
g.setColor(g.getColor().darker());
g2.drawRect(i*TILE_SIZE,j*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
}
g.drawImage(image, 0, 0, this);
}
void drawMazeArea(Graphics g, int i, int j, int n, char type) {
if (type == 'O')
{
g.setColor(Color.BLUE);
}
if (type == 'P')
{
g.setColor(Color.WHITE);
g.fillRect('P'*TILE_SIZE,'P'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('P'*TILE_SIZE,'P'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
if (type == 'S')
{
g.setColor(Color.RED);
g.fillRect('S'*TILE_SIZE,'S'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('S'*TILE_SIZE,'S'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
if( type == '.')
{
g.setColor(Color.YELLOW);
g.fillRect('.'*TILE_SIZE,'.'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('.'*TILE_SIZE,'.'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
if (type == 'E')
{
g.setColor(Color.GREEN);
g.fillRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
//g.setColor(g.getColor().darker());
g.drawRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
int r = WIDTH / (n + 3);
int x = i * r + r, y = j * r + r, width = r, height = r;
g.fillRect(x, y, width, height);
g.setColor(Color.WHITE);
if (type == 'S')
g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
if (type == 'E'){
g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
g.setColor(Color.black); }
g.fillRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
g.setColor(g.getColor().darker());
g.drawRect('E'*TILE_SIZE,'E'*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
void showMaze(String maze, int milliseconds) {
int a = milliseconds;
a = 1500;
this.maze = maze;
repaint();
try {
Thread.sleep(a);
} catch (Exception e) {
}
}
public void update(Graphics g) {
paint(g);
}
}
public static void main(String [] a)
{ Screen screen = new Screen();
Maze maze = new Maze();
Button sm = new Button(" Solve Maze ");
Button exit = new Button(" Exit ");
TextField tb = new TextField(20 );
Label button = new Label(" Algorithm: ");
sm.setBackground(Color.green);
exit.setBackground(Color.red);
button.setBackground(Color.pink);
screen.add(button);
screen.add(tb );
screen.add(sm );
screen.add(exit );
screen.setLayout(new FlowLayout());
screen.setVisible(true);
int milliseconds = 4000;
maze.searchExit(screen, milliseconds);
}
}
emint 0 Junior Poster in Training
Dear AdataPost
pls help me if error i have made on that program. thx
kvprajapati 1,826 Posting Genius Team Colleague
Write code of main() method of class Maze into actionPerformed() method of class C.
emint 0 Junior Poster in Training
Write code of main() method of class Maze into actionPerformed() method of class C.
I Did but i got error in run time
emint 0 Junior Poster in Training
Did you run maze from class c successfully? i could not do so!
kvprajapati 1,826 Posting Genius Team Colleague
Did you run maze from class c successfully? i could not do so!
I didn't run your program. Can I see the code of your class C?
emint 0 Junior Poster in Training
here is my another class:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Main extends JFrame implements ActionListener
{ public int milliseconds = 0;
public static String a, b, c, d,e,f;
JButton maze1 = new JButton("Maze 1");
JButton maze2 = new JButton("Maze 2");
JButton maze3 = new JButton("Maze 3");
JButton maze4 = new JButton("Maze 4");
JButton exit = new JButton("Exit");
JRadioButton beg = new JRadioButton( "Begginers", true );
JRadioButton dif = new JRadioButton( "Difficult", false );
JRadioButton cg = new JRadioButton( "Computer generate", false );
Border border = LineBorder.createGrayLineBorder();
JLabel level = new JLabel("Maze level");
JLabel info = new JLabel("Note: Select Maze level then click on the maze button!");
ButtonGroup group = new ButtonGroup();
public Main()
{
super("Welcome to Maze program");
maze1.addActionListener(this);
maze2.addActionListener(this);
maze3.addActionListener(this);
maze4.addActionListener(this);
exit.addActionListener(this);
beg.addActionListener(this);
dif.addActionListener(this);
cg.addActionListener(this);
getContentPane().setLayout(null);
group.add(beg);
group.add(dif);
group.add(cg);
//image
level.setLocation(10,10);
level.setSize(180,20);
level.setForeground(Color.blue);
getContentPane().add( level);
//begginers
beg.setLocation(10,40);
beg.setSize(180,20);
getContentPane().add( beg);
//difficult
dif.setLocation(10,70);
dif.setSize(180,20);
getContentPane().add( dif);
//Computer generated
cg.setLocation(10,100);
cg.setSize(180,20);
getContentPane().add( cg);
//Maze 1 Button
maze1.setLocation(15,170);
maze1.setSize(75,30);
maze1.setForeground(Color.blue);
getContentPane().add( maze1);
//Maze 2 Button
maze2.setLocation(105,170);
maze2.setSize(75,30);
maze2.setForeground(Color.blue);
getContentPane().add( maze2);
//maze 3 Button
maze3.setLocation(195,170);
maze3.setSize(75,30);
maze3.setForeground(Color.blue);
getContentPane().add( maze3);
//maze 4 Button
maze4.setLocation(285,170);
maze4.setSize(75,30);
maze4.setForeground(Color.blue);
getContentPane().add( maze4);
//exit Button
exit.setLocation(375,170);
exit.setSize(75,30);
exit.setForeground(Color.blue);
exit.setBackground(Color.red);
getContentPane().add(exit);
//info levle
//image
info.setLocation(110,140);
info.setSize(380,20);
getContentPane().add( info);
}
public static void main(String[] args)
{
Main frame = new Main();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(490, 250);
frame.setResizable(false);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
Object src = event.getSource();
if(src == exit)
{
int exit = JOptionPane.showConfirmDialog(this, " Are you sure? You want to close the program.");
if (exit == JOptionPane.YES_OPTION)
System.exit(0);
}
else if(src == maze1)
{
Maze maze = new Maze();
Maze.Screen screen = new Maze.Screen();
milliseconds = 4000;
maze.searchExit(screen, milliseconds);
//JOptionPane.showMessageDialog(null, "you have Selected beggier level Maze 1, loading....! please wait.....");
}
}
public void myVisiable()
{
//modifyButton.setVisible(false);
}
}
kvprajapati 1,826 Posting Genius Team Colleague
screen.setVisible(true) is missing.
else if(src == maze1)
{
Maze maze = new Maze();
Maze.Screen screen = new Maze.Screen();
milliseconds = 4000;
maze.searchExit(screen, milliseconds);
screen.setLayout(new FlowLayout());
screen.setVisible(true);
}
emint 0 Junior Poster in Training
thank you very much. i works. but i got another problem. if u can help.
when i hit maze1 button it took bout 30 to 40 sec to run maze class is there any reason?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.