?
Solved

dragging & resize image on canvas with out flickering

Posted on 2000-03-08
1
Medium Priority
?
435 Views
Last Modified: 2012-05-04
sir,
whenever i  drag the image on the
canvas the image is getting flickered so i want to reduce the flickering of image and also flush the old image on the canvas.i tried out with double
buffering concept,but i coudn't get it.
so please give the suggestion.
0
Comment
Question by:amarnathbabu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 300 total points
ID: 2695999
Ok, here's a small applet that allows dragging an image around, it uses double buffering and doesn't flicker.


import java.awt.*;
import java.applet.*;


public class MyApplet extends Applet{

  public void init(){
    setLayout(null);
    MyComponent comp = new MyComponent(getCodeBase(),"image.gif");
    comp.reshape(20,20,300,300);
    add(comp);
  }

}
   

class MyComponent extends Canvas{

  private Image dbImage; // Image for double buffering, to avoid flickering.
  private Image myImage;
  private int imageX=0, imageY=0; // The location of our image;

  private int lastDragX, lastDragY;

  public MyComponent(Image image){
    myImage = image;
  }


  public void update(Graphics g){
     Graphics dbGraphics = getDBGraphics();
     dbGraphics.setColor(getBackground());
     dbGraphics.fillRect(0,0,bounds().width,bounds().height);
     paint(dbGraphics);
     g.drawImage(dbImage,0,0,null);
  }

 
  public void paint(Graphics g){
     g.drawImage(myImage,imageX,imageY,null);
  }


   public boolean mouseDown(Event e, int x, int y){
      lastDragX = x;
      lastDragY = y;
      return false;
   }


  public boolean mouseDrag(Event e, int x, int y){
     imageX += x-lastDragX;
     imageY += y-lastDragY;
     lastDragX = x;
     lastDragY = y;
     repaint();
     return false;
  }



 

  private final Graphics getDBGraphics(){
 if ((dbImage == null) || ((dbImage.getWidth(null)!=bounds().width)||
(dbImage.getHeight(null)!=bounds().height))){
   dbImage = createImage((int)Math.max(bounds().width,1),
                      (int)Math.max(bounds().height,1));
        // Using Math.max is needed because createImage only takes positive
        // values, which seems strange to me, since components can even
        // have negative size.
    }
    return dbImage.getGraphics();
  }

}


I'm really surprised to find this question unanswered.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question