Showing posts with label Image. Show all posts
Showing posts with label Image. Show all posts

Friday, 27 April 2012

Dragging image in java

Draging Image dragme.java

import javax.swing.JFrame;
public class dragme extends JFrame{
 
 public dragme(){
  
  add(new dragged());
  
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 600);
        setLocationRelativeTo(null);
        setTitle("Dragging App");

        setResizable(false);
        setVisible(true);
  
 }
 
 public static void main(String[] args){
  new dragme();
 }
 
}


Dragged Image Class dragged.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;

import javax.swing.*;

public class dragged extends JPanel implements MouseMotionListener, MouseListener {
 private Image Dragged;
 private Image background;
 private static int x_coord = 150;
 private static int y_coord = 150;
 private boolean isdragged;
 private static int x_dragFrom = 0;
 private static int y_dragFrom = 0;
 public dragged(){
  
  setPreferredSize(new Dimension(300,300));
  setBackground(Color.WHITE);
  this.addMouseMotionListener(this);
  this.addMouseListener(this);
  LoadImage();
  
 }
 
 public void LoadImage(){
  
  Dragged = new ImageIcon("C:\\javapics\\deneme.png").getImage();
  background = new ImageIcon("C:\\javapics\\daaad.png").getImage();
 }
 
 public void paintComponent(Graphics g){
  
  super.paintComponent(g);
  g.drawImage(background, 0, 0, null);
  g.drawImage(Dragged, x_coord, y_coord, this);
  
 }
 public void mousePressed(MouseEvent e) {
        int x = e.getX();   // Save the x coord of the click
        int y = e.getY();   // Save the y coord of the click
        
        if (x >= x_coord && x <= (x_coord + Dragged.getWidth(null))
                && y >= y_coord && y <= (y_coord + Dragged.getWidth(null))) {
            isdragged = true;
            x_dragFrom = x - x_coord;  // how far from left
            y_dragFrom = y - y_coord;  // how far from top
        } else {
            isdragged = false;
        }
    }
  public void mouseDragged(MouseEvent e) {
         if (isdragged) {  
            
             x_coord = e.getX() - x_dragFrom;//to better dragging view
             y_coord = e.getY() - y_dragFrom;//to better dragging view
             
            //to prevent overflow left and right side
             x_coord  = Math.max(x_coord , 0);
             x_coord  = Math.min(x_coord , getWidth() - Dragged.getWidth(null));
             
             //to prevent overflow up and down side
             y_coord  = Math.max(y_coord , 0);
             y_coord  = Math.min(y_coord , getHeight() - Dragged.getHeight(null));
             
             this.repaint(); // Repaint because position changed.
         }
     }

     //we should change isdragged variable when mouse released for dragging
     public void mouseExited(MouseEvent e) {
         isdragged = false;
     }

     public void mouseMoved   (MouseEvent e) {}  // ignore these events
     public void mouseEntered (MouseEvent e) {}  // ignore these events
     public void mouseClicked (MouseEvent e) {}  // ignore these events
     public void mouseReleased(MouseEvent e) {}  // ignore these events
}

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);
  }
  
 }
 
 
}