i hav a frame having buttton on it .on click event of the button ,i want to call another frame which implements threading concept. i tried it simply by calling constructor of that frame ,frame is displaying but no other content. that frame contains no of graphics objects moving on screen.
calling frame code .. import java.awt.*; import java.awt.image.*; import javax.swing.*; import java.awt.event.*; import javax.swing.border.*; //import javax.swing.border.*; import java.sql.*;
public class frame8 extends JPanel implements ActionListener
{
public static final int TILED = 0;
public static final int SCALED = 1;
public static final int ACTUAL = 2;
private BufferedImage image;
private int style;
private float alignmentX = 0.5f;
private float alignmentY = 0.5f;
JButton b;
JRadioButton r1,r2,r3;
ButtonGroup gp;
JLabel l1;
public frame8(BufferedImage image) { this(image,TILED); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage( image, 0, 0,null ); } public frame8(BufferedImage image, int style) { this.image = image; this.style = style; setSize(300,400); setLayout( new BorderLayout() ); } public frame8() throws Exception { BufferedImage image = javax.imageio.ImageIO.read( new java.io.File("an.jpg")); frame8 north = new frame8(image); frame8 center = new frame8(image); frame8 south = new frame8(image); center.setLayout(new GridLayout(4,1)); //center.setBackground(Color.GREEN); south.setLayout(new FlowLayout()); l1=new JLabel("SELECT DIFFICULTY LEVEL",JLabel.CENTER); l1.setFont(new Font("monotype Corsiva",Font.BOLD,25)); l1.setForeground(Color.BLUE); gp=new ButtonGroup(); r1=new JRadioButton("EASY"); r1.setForeground(Color.blue); r1.setBackground(Color.GREEN); r2=new JRadioButton("MEDIUM"); r2.setForeground(Color.blue); r2.setBackground(Color.GREEN); r3=new JRadioButton("HARD"); r3.setForeground(Color.blue); r3.setBackground(Color.GREEN); r1.setOpaque(false); r2.setOpaque(false); r3.setOpaque(false); gp.add(r1); gp.add(r2); gp.add(r3); b=new JButton(new ImageIcon("bg2.jpg")); b.setOpaque(false); b.addActionListener(this); b.setFont(new Font("monotype Corsiva",Font.BOLD,20)); b.setToolTipText("click to select given option and go ahead"); //b[1].setToolTipText("click to go back"); // b.setBorder(new LineBorder(Color.GREEN)); b.setBackground(Color.GREEN); b.setForeground(Color.WHITE); south.add(b); center.add(l1); center.add(r1);center.add(r2); center.add(r3); JFrame frame = new JFrame(); b.setEnabled(true); r1.setEnabled(true); r2.setEnabled(true); r3.setEnabled(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.setLocation(450,290); frame.getContentPane().add(center,BorderLayout.CENTER); frame.getContentPane().add(south,BorderLayout.SOUTH); //frame.pack(); frame.setTitle("SELECTION FORM"); frame.setVisible(true); frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } public void actionPerformed(ActionEvent ae) { JButton bb=(JButton)ae.getSource(); b.setEnabled(false); r1.setEnabled(false); r2.setEnabled(false); r3.setEnabled(false); try { if(bb==b) { if(r1.isSelected()) { System.out.println("enterd"); //ImagePanel1 i=new ImagePanel1(); //i.b[0].setEnabled(false); //i.b[1].setEnabled(false); new gameFrame(); System.out.println("started"); } else if(r2.isSelected()) { //med m=new med(); } else if(r3.isSelected()) { System.out.println("hard"); } else { JOptionPane.showMessageDialog(null,"1st SELECT SOME CHOICE","ERROR",JOptionPane.ERROR_MESSAGE,new ImageIcon("A04.gif")); b.setEnabled(true); r1.setEnabled(true); r2.setEnabled(true); r3.setEnabled(true); } } }catch(Exception e) { e.printStackTrace(); } } public static void main(String [] args)throws Exception { new frame8(); }
}
called frame code...
import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.math.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.Timer;
public class TestingArea
{
int counter=0; static File file1,file2,file3,file5; static Vector <Image> list = new Vector<Image>(); static int X = 0; static int Y = 50; static boolean flag=false; Timer clock; public TestingArea() { file1= new File("easy1.jpg"); file2= new File("easy1.jpg"); file3= new File("easy1.jpg"); file5=new File("easy1.jpg"); // TODO, add your application code gameFrame gf = new gameFrame(); Thread ob=new Thread(gf); ob.start(); paintIt obj = new paintIt(); gf.add(obj); flag=true; while (flag) { X += 1; Y +=2; if(X>=200) { X=0; Y=50; } Graphics g; gf.repaint(); try{ //do what you want to do before sleeping Thread.currentThread().sleep(10);//sleep for 1000 ms //do what you want to do after sleeptig } catch(Exception ie) { //If this thread was intrrupted by nother thread } clock=new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { counter++; if(counter==1000) { flag=false; clock.stop(); gameFrame.time.setVisible(false); JOptionPane.showMessageDialog(null,"SCORE: "+Integer.toString(paintIt.sc),"HI",JOptionPane.PLAIN_MESSAGE,new ImageIcon("A04.gif")); try { new ImagePanel1(); }catch(Exception e3) {} } gameFrame.time.setText(Integer.toString(counter)); } } ); clock.start(); }//while } public static void main(String a[]) { new TestingArea(); }
}
class gameFrame extends JFrame implements Runnable//,ActionListener { int mx,my; //int bx1,by1,bx2,by2,bx3,by3; JPanel north = new JPanel(); JPanel east = new JPanel(); static JLabel time,hits,score,clicks; static JButton quit; int counter=1000; paintIt p = new paintIt(); public void run() { System.out.println("Inside"); paintIt p = new paintIt();
} public gameFrame()
{
System.out.println("KK2");
// paintIt p = new paintIt(); setLayout(new BorderLayout()); time=new JLabel("TIME:"); time.setFont(new Font("",Font.ITALIC,20)); time.setForeground(Color.WHITE); hits=new JLabel("HITS:"); hits.setFont(new Font("",Font.ITALIC,20)); hits.setForeground(Color.WHITE); clicks=new JLabel("SHOTS:"); clicks.setFont(new Font("",Font.ITALIC,20)); clicks.setForeground(Color.WHITE); score=new JLabel("SCORE:"); score.setFont(new Font("",Font.ITALIC,20)); score.setForeground(Color.WHITE); east.setBackground(Color.RED); p.setBackground(Color.BLACK); east.setLayout(new GridLayout(1,5)); east.add(time); east.add(score); east.add(hits); quit=new JButton("QUIT"); east.add(clicks); east.add(quit); setLayout(new BorderLayout()); quit.setBackground(Color.RED); quit.setForeground(Color.white); quit.setFont(new Font("monotype Corsiva",Font.ITALIC,25)); quit.setBorder(new LineBorder(Color.RED)); //quit.addActionListener(this); quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JButton b=(JButton)ae.getSource(); if(b==quit) TestingArea.flag=false; east.setVisible(false); try { new ImagePanel1();//System.exit(0); }catch(Exception e2){e2.printStackTrace();} } }); getContentPane().add(east,BorderLayout.SOUTH); getContentPane().add(p,BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(150,5); setSize(900,700); setTitle("Image test"); setVisible(true);
System.out.println("KK");
} /* public void actionPerformed(ActionEvent ae) { JButton b=(JButton)ae.getSource(); if(b==quit) ImagePanel1 i=new ImagePanel1(); }*/
}
class paintIt extends JPanel implements Runnable { BufferedImage pic,pic2,pic3; public int xmin1,ymin1,xmax1,ymax1; public int xmin2,ymin2,xmax2,ymax2; public int xmin3,ymin3,xmax3,ymax3; public int xmin4,ymin4,xmax4,ymax4; public int xmin5,ymin5,xmax5,ymax5; public int xmin6,ymin6,xmax6,ymax6; public int xmin7,ymin7,xmax7,ymax7; public int xmin8,ymin8,xmax8,ymax8; public int xmin9,ymin9,xmax9,ymax9; public int w1,h1,bx2,by2; public int wx,hy; int mx,my; public float avg=0; static int h=0,sc=0,c=0; Graphics g;
public void paintComponent(Graphics g) { super.paintComponent(g); try {
//System.out.println("KK3");
if (TestingArea.list.capacity()> 0 )
{
pic = ImageIO.read(TestingArea.file5);
pic2=ImageIO.read(TestingArea.file2);
pic3=ImageIO.read(TestingArea.file3);
w1=pic.getWidth(this);
h1=pic.getHeight(this);
TestingArea.list.add(pic); TestingArea.list.add(pic2); TestingArea.list.add(pic3); } int a=TestingArea.X+(int)(Math.random()*50); int b=70;//TestingArea.Y;//+(int)(Math.random()*30); xmin1=a; ymin1=b; xmin2=TestingArea.X+700; ymin2=(TestingArea.Y+100)+(int)(Math.random()*90); xmin3=TestingArea.X+400;//+(1+(int)(Math.random()*60)); ymin3=TestingArea.Y+300; xmin4=TestingArea.X-90; ymin4=(TestingArea.Y);//+200)+(50+(int)(Math.random()*200)); xmin5=TestingArea.X+250; ymin5=(TestingArea.Y+300)+(int)(Math.random()*90); xmin6=TestingArea.X+200; ymin6=400;//(TestingArea.Y+200)+(int)(Math.random()*100); xmin7=TestingArea.X+500; ymin7=10;//(TestingArea.Y+300)+(int)(Math.random()*90); xmin8=TestingArea.X+250; ymin8=(TestingArea.Y+50)+(int)(Math.random()*90); xmin9=20;//TestingArea.X+500; ymin9=(TestingArea.Y+30)+(int)(Math.random()*90); g.drawImage(pic,xmin1,ymin1,null); g.drawImage(pic2,xmin2,ymin2,null); g.drawImage(pic3,xmin3,ymin3,null); g.drawImage(pic3,xmin4,ymin4,null); g.drawImage(pic3,xmin5,ymin5,null); g.drawImage(pic3,xmin6,ymin6,null); g.drawImage(pic3,xmin7,ymin7,null); g.drawImage(pic3,xmin8,ymin8,null); g.drawImage(pic3,xmin9,ymin9,null); } catch (Exception e) { System.out.println("DOESNT WORK!"); e.printStackTrace(); } } public void run() { } paintIt() { //System.out.println("KK33"); //Graphics g; /* Timer clock=new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { counter--; gameFrame.time.setText(Integer.toString(counter)); //System.out.print("\n not"+counter ); //update JTextField } } ); clock.start(); if(counter==0) System.exit(0);*/ addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { int f1=0,f2=0; //for pic1 int f3=0,f4=0; //for pic2 int f5=0,f6=0; int f7=0,f8=0; int f9=0,f0=0; //pic5 int fl1=0,fl2=0,fl3=0,fl4=0,fl5=0,fl6=0,fl7=0,fl8=0; mx=me.getX(); my=me.getY(); System.out.println("****************************************"); System.out.println("clicked==x"+mx+" y=="+my); System.out.println("image x=="+xmin1+" y=="+ymin1+"\n\n"); System.out.println(" image width=="+w1+" height=="+h1+"\n\n"); xmax1=xmin1+w1; ymax1=ymin1+h1; xmax2=xmin2+w1; ymax2=ymin2+h1; xmax3=xmin3+w1; ymax3=ymin3+h1; xmax4=xmin4+w1; ymax4=ymin4+h1; xmax5=xmin5+w1; ymax5=ymin5+h1; xmax6=xmin6+w1; ymax6=ymin6+h1; xmax7=xmin7+w1; ymax7=ymin7+h1; xmax8=xmin8+w1; ymax8=ymin8+h1; xmax9=xmin9+w1; ymax9=ymin9+h1; //for pic1 if((xmin1<mx && mx <xmax1) && (ymin1<my && my<ymax1)) { System.out.println("within"); f1=1; sc=sc+20; } else if(((xmin1== mx || xmax1==mx) && (ymin1<my && my<ymax1)) || ((xmin1<mx && mx <xmax1) && (ymin1== my || ymax1==my))) { System.out.println("on boundry"); f2=1; sc=sc+20; } //END OF PIC //start for pic 2 else if((xmin2<mx && mx <xmax2) && (ymin2<my && my<ymax2)) { System.out.println("within2"); f3=1; sc=sc+20; } else if(((xmin2== mx || xmax2==mx) && (ymin2<my && my<ymax2)) || ((xmin2<mx && mx <xmax2) && (ymin2== my || ymax2==my))) { System.out.println("on boundry2"); f4=1; sc=sc+20; } //end of pic2 // start of pic3 else if((xmin3<mx && mx <xmax3) && (ymin3<my && my<ymax3)) { System.out.println("within3"); f5=1; sc=sc+20; } else if(((xmin3== mx || xmax3==mx) && (ymin3<=my && my<=ymax3)) || ((xmin3<=mx && mx<=xmax3) && (ymin3== my || ymax3==my))) { System.out.println("on boundry3"); f6=1; sc=sc+20; } //end of pic 3 //start of pic4 else if((xmin4<mx && mx <xmax4) && (ymin4<my && my<ymax4)) { System.out.println("within4"); f7=1; sc=sc+20; } else if(((xmin4== mx || xmax4==mx) && (ymin4<my && my<ymax4)) || ((xmin4<mx && mx <xmax4) && (ymin4== my || ymax4==my))) { System.out.println("on boundry4"); f8=1; sc=sc+20; } //end of pic 4 //start of pic5 else if((xmin5<mx && mx <xmax5) && (ymin5<my && my<ymax5)) { System.out.println("within5"); f9=1; sc=sc+20; } else if(((xmin5== mx || xmax5==mx) && (ymin5<my && my<ymax5)) || ((xmin5<mx && mx <xmax5) && (ymin5== my || ymax5==my))) { System.out.println("on boundry5"); f0=1; sc=sc+20; } //END OF PIC5 //start for pic 6 else if((xmin6<mx && mx <xmax6) && (ymin6<my && my<ymax6)) { System.out.println("within6"); fl1=1; sc=sc+20; } else if(((xmin6== mx || xmax6==mx) && (ymin6<my && my<ymax6)) || ((xmin6<mx && mx <xmax6) && (ymin6== my || ymax6==my))) { System.out.println("on boundry2"); fl2=1; sc=sc+20; } //end of pic6 // start of pic7 else if((xmin7<mx && mx <xmax7) && (ymin7<my && my<ymax7)) { System.out.println("within7"); fl3=1; sc=sc+20; } else if(((xmin7== mx || xmax7==mx) && (ymin7<=my && my<=ymax7)) || ((xmin7<=mx && mx<=xmax7) && (ymin7== my || ymax7==my))) { System.out.println("on boundry7"); fl4=1; sc=sc+20; }
//END OF PIC
//start for pic 8 else if((xmin8<mx && mx <xmax8) && (ymin8<my && my<ymax8)) { System.out.println("within8"); fl5=1; sc=sc+20; } else if(((xmin8== mx || xmax8==mx) && (ymin8<my && my<ymax8)) || ((xmin8<mx && mx <xmax8) && (ymin8== my || ymax8==my))) { System.out.println("on boundry8"); fl6=1; sc=sc+20; } //end of pic8 // start of pic9 else if((xmin9<mx && mx <xmax9) && (ymin9<my && my<ymax9)) { System.out.println("within9"); fl7=1; sc=sc+20; } else if(((xmin9== mx || xmax9==mx) && (ymin9<=my && my<=ymax9)) || ((xmin9<=mx && mx<=xmax9) && (ymin9== my || ymax9==my))) { System.out.println("on boundry9"); fl8=1; sc=sc+20; }
///////////////////////////////////////////////////////////////////////////////
System.out.println("xmax=="+xmax1+" wy=="+ymax1+"\n\n"); if(f1==1 || f2==1 || f3==1 || f4==1 || f5==1 || f6==1 || f7==1 || f8==1 || f9==1 || f0==1 || fl1==1|| fl2==1|| fl3==1 || fl4==1 || fl5==1 || fl6==1 || fl7==1 || fl8==1) { ++h;++c; gameFrame.hits.setText("HITS :"+Integer.toString(h)); gameFrame.score.setText("SCORE :"+Integer.toString(sc)); gameFrame.clicks.setText("SHOTS :"+Integer.toString(c)); System.out.println("yes"+sc); } else { ++c; gameFrame.clicks.setText("SHOTS :"+Integer.toString(c)); System.out.println("no"); } } }); }
}
Ads