Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with image display  RSS feed

 
joan wo
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm developing an application to show an image and draw over it.
I have class that extends a JFrame where I build oll the interface, and call
a class that extends JPaint where I display the image and where I can draw.

It run's ok, but I have two problems:
I must read ever the image from a file (because the image can be very big) and show only the pierce of the image that can be displayed (it depends of the size of the component i every moment).

- If I resize the main application (jframe) the image is resized too. And I want that image shows it's real size ever. What I have to do to prevent the resized of the image.
- When I resize, I read the image again to display the pierce of the image that can be displayed, and run's ok, but appear a flicker effect. How can I remove this effect.

The code of the JPaint class is:
public class ImageVisor extends JPanel implements MouseListener, MouseMotionListener{

private static final long serialVersionUID = 1L;
public static JScrollBar hbar;
public static JScrollBar vbar;
public static JLabel label;
public static Image image;
Shape shape;

public ImageVisor(){
super();
this.setLayout(new BorderLayout());
this.setOpaque(true);

this.setDoubleBuffered(true);

this.addMouseMotionListener(this);
this.addMouseListener(this);
}

public void newShape(int x, int y){
shape = new Shape();
Color color = new Color(40,40,40);
shape.setColor(color);
shape.addPoint(x, y);
}

public void addPointToShape(int x,int y){
shape.addPoint(x, y);
}

public void updateImage(){
this.repaint();
}

public void displayImage(BufferedImage bufImage ){
image = (Image) bufImage;
this.repaint();
}

public void paintComponent(Graphics g){
if (image!=null){
g.drawImage(image,0,0, this.getWidth(),this.getHeight(),this);
}
if (shape!=null){
g.setColor(shape.getColor());
Point2D p0 = shape.getPoint(0);
for (int i = 0; i < shape.getPointsNumber () - 1; i++)
{
Point2D p1 = shape.getPoint(i + 1);
g.drawLine((int) p0.getX(), (int) p0.getY(), (int) p1.getX(),
(int) p1.getY());
p0 = p1;
}
}
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
newShape(e.getX(),e.getY());
}

public void mouseReleased(MouseEvent e) {
updateImage();
}

public void mouseDragged(MouseEvent e) {
addPointToShape(e.getX(),e.getY());
updateImage();
}

public void mouseMoved(MouseEvent e) {
}
}



Thank's in advance.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method

attempts to scale the image to the current width and height of this (enclosing class, ImageVisor) component. To show the image at its original size you can use the Graphics class method
 
jad yuuu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the size of the panel is 0,0 . replace the g.drawImage(..,..,getWidth(),getHeight()...
with g.drawImage(..,..,400,400... for example.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!