Solved

Images in canvas

Posted on 2000-05-16
2
238 Views
Last Modified: 2010-04-01
Hi!
I have 2 img and I would use them to create my slider. I know I have to use canvas. One img suppose to goes strieght back and force meantime other is not moving.Please show some example.
0
Comment
Question by:gee
2 Comments
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 90 total points
ID: 2816427
Here is an example of a component that draws an image and allows dragging it around with your mouse. From what I understand you want something similar but that moves just around the x OR y axis... should be easy to modify:


public 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)!=(int)getIBounds().getWidth())||
(dbImage.getHeight(null)!=(int)getIBounds().getHeight()))){
       dbImage = createImage((int)Math.max(getIBounds().getWidth(),1),
                                              (int)Math.max(getIBounds().getHeight(),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();
  }

}



Hope this helps, Sasha.

0
 
LVL 7

Expert Comment

by:vladi21
ID: 2816446
R u using Swing?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

863 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now