Saturday, 11 February 2012

Java-Adding an Image

images.java

If you want to debug this class, then you should use class that FullScreen.java
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
public class images extends JFrame{

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
  images i = new images();
  i.run(dm);
 } 
  
 private FullScreen s; 
 private Image bg;
 private Image pic;
        private Image ani;
 private boolean loaded;
 //run method
 public void run(DisplayMode dm){
  setBackground(Color.WHITE);
  setForeground(Color.BLACK);
  setFont(new Font("Arial",Font.PLAIN,24));
  loaded = false;
  s = new  FullScreen();
  try{
   s.setFullScreen(dm, this);
   loadpics();
   try{
    
    Thread.sleep(3000);//milliseconds
    
   }catch(Exception ex){}
  }finally{
   s.restoreScreen();
  }
 }
 
 //load pictures
 public void loadpics(){
  bg = new ImageIcon("C:\\anywhere.jpg").getImage();
  pic = new ImageIcon("C:\\anywhere.png").getImage();
                ani = new ImageIcon("C:\\anywhere.gif").getImage();
  loaded = true;
  repaint();
 }
 
 public void paint(Graphics g){
  if(g instanceof Graphics2D){
   Graphics2D g2 = (Graphics2D)g;
   g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  }
  if(loaded){
   g.drawImage(bg, 30, 30, null);
   g.drawImage(pic, 170, 180, null);
   g.drawImage(ani,100,100,null);
  }
  
 }
 
 
}

Friday, 10 February 2012

Java-Full Screen App

burkan.java


import java.awt.*;
import javax.swing.JFrame;
public class burkan extends JFrame{

 /**
  * @param args
  */
public static void main(String[] args) {
 // TODO Auto-generated method stub
 DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
 burkan b = new burkan();
 b.run(dm);
}

public void run(DisplayMode dm){
    setBackground(Color.WHITE);
    setForeground(Color.BLACK);
    setFont(new Font("Arial",Font.PLAIN,24));
  
    FullScreen s = new  FullScreen();
    try{
    s.setFullScreen(dm, this);
    try{
  
    Thread.sleep(3000);
   
    }catch(Exception ex){}
    }finally{
    s.restoreScreen();
    }
}
 
public void paint(Graphics g){
 if(g instanceof Graphics2D){
  Graphics2D g2 = (Graphics2D)g;
  g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
 }
  g.drawString("Hello World!!!", 200, 200);
 } 
}

FullScreen.java

import javax.swing.JFrame;
import java.awt.*;
public class FullScreen {
 private GraphicsDevice vc;
 
 public FullScreen(){
  
  GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  vc = env.getDefaultScreenDevice();
  
 }
 
 public void setFullScreen(DisplayMode dm,JFrame window){
  
  window.setUndecorated(true);
  window.setResizable(false);
  vc.setFullScreenWindow(window);
  
  if(dm != null && vc.isDisplayChangeSupported()){
   
   try{
    vc.setDisplayMode(dm);
   }catch(Exception ex){}
   
  }
 }
 public Window getFullScreenWindow(){
 
  return vc.getFullScreenWindow();
 
 }
 
 public void restoreScreen(){
  Window w = vc.getFullScreenWindow();
  if(w != null){
   w.dispose();
  }
  vc.setFullScreenWindow(null);
 }
  
}

Saturday, 4 February 2012

Java - Thread

burkan.java

import javax.swing.*;
public class burkan {
 public static void main(String[] args){
   
  
 Thread t1 = new Thread(new threads(300,300,1500));
 Thread t2 = new Thread(new threads(250,250,3500));
 Thread t3 = new Thread(new threads(200,200,6500));
 Thread t4 = new Thread(new threads(100,100,8500));
 Thread t5 = new Thread(new threads(50,50,10500));
   
 t1.start();
 t2.start();
 t3.start();
 t4.start();
 t5.start();
 }
}



graphics.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class graphics extends JPanel{
 private int a,b;
 public graphics(int i, int j) {
   //constructor
   a=i;
   b=j;
 }

 public void paintComponent(Graphics g){
 super.paintComponents(g);
 this.setBackground(Color.WHITE);
   
 g.setColor(Color.blue);
 g.drawRect(100,100, a, b);
    
  }
    
  
   
  
}


threads.java

import java.util.*;
import java.awt.*;
import javax.swing.*;
 public class threads implements Runnable{
 private int x,y;
 public int time;
 public JFrame f = new JFrame("LOOP");
 
 public threads(int a,int b,int t){
  //constructor
  x=a;
  y=b;
  time = t;
  
 }
 
 
    public void run(){
     
     try{
      Thread.sleep(time);
   graphics p = new graphics(x,y);

   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.setSize(800,600);
   f.setVisible(true);
   f.add(p);
     
     }catch(Exception e){}
     
    }
}