Solved

Images in canvas

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

688 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