6/10/2012

drawing image through graphics in java

 easywayprogramming.com drawing image through graphics in java

we are going to see how we can draw a image into frame using graphics

print.java

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.awt.Color;

import javax.swing.JFrame;
import java.util.Properties;

public class print extends JFrame
{
      PrintCanvas my_canvas = new PrintCanvas();

      public print()
      {
        add("Center", my_canvas);
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Ashish Wagh");
        setVisible(true);
        String name = "Ashish Wagh";

        Properties properties = new Properties();
        PrintJob pj = Toolkit.getDefaultToolkit().getPrintJob(print.this, name, properties);
        if (pj != null)
        {
              my_canvas.printAll(pj.getGraphics());
              pj.end();
        }
      }

      public static void main(String args[])
      {
        print p= new print();
      }
}

class PrintCanvas extends Canvas
{
  public void paint(Graphics g)
  {
    Dimension size = getSize();
    int width = size.width;
    int height = size.height;
    int x1 = (int) (width * 0.1);
    int x2 = (int) (width * 0.9);
    int y1 = (int) (height * 0.1);
    int y2 = (int) (height * 0.9);

    g.setColor(Color.YELLOW);
    g.fillRect(x1, y1, x2 - x1, y2 - y1);
    g.setColor(Color.GREEN);
    g.drawRect(x1, y1, x2 - x1, y2 - y1);
    g.drawOval(x1, y1, x2 - x1, y2 - y1);
    g.setColor(Color.RED);
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(x2, y1, x1, y2);
    g.setColor(Color.BLACK);
    String text = "Ashish!";
    text += text;
    text += text;
    g.drawString(text, x1, (int) ((y1 + y2) / 2));
    g.drawString("Ashish",(int)(width/2)-(width/20), (int)(y1+(y2/4)));
    g.drawString("Ashish",(int)(width/2)-(width/20), (int)(y1+((3*y2)/4)));

    Image img = new ImageIcon("Ashish_photo.JPG").getImage();    
    Image newimg = img.getScaledInstance(100, 120,  java.awt.Image.SCALE_SMOOTH); 
    ImageIcon newIcon = new ImageIcon(img);    
    g.drawImage(newIcon.getImage(), 250, 70, 100,120, null);
    g.dispose();
  }
}

        In above example we, create a image icon of photo and get image from that photo. Then we create new image from old image by using method getScaledInstance(new Width, new height, java.awt.Image.SCALE_SMOOTH);
        Then we create new image icon from new image, and get image from that ImageIcon.
       now using drawImage(image, position x, position y, new width, new height, null) method in graphics we can draw that image.

2 comments:

  1. Fantastic work! This is the type of information that should follow collective approximately the web. Embarrassment captivating position Google for not positioning this transmit higher! Enlarge taking place greater than and visit my web situate
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  2. How To Play Baccarat - Wirione.com
    This is a popular 메리트 카지노 쿠폰 variant of the game Baccarat. There is a simple game called baccarat, 바카라 사이트 where choegocasino the player bets on two teams of two. Baccarat players bet

    ReplyDelete