Solved

Images in canvas

Posted on 2000-05-16
2
237 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

23 Experts available now in Live!

Get 1:1 Help Now