Skip to main content

drawImage problem.

7 replies [Last post]
Darrin
Offline
Joined: 2006-02-17

I have a JApplet that loads images from a jar file and then displays each
image on a seperate tab (JTabbedPane). These images are placed onto labels
which the tabs use and they can be scaled and moved (placed into a scroll
pane), but I am having problems trying to draw on them.

When I try to draw on an image, I get a duplicate image drawn in the upper
left hand corner with the marks I added (just working with lines for now).

I know that this image can be drawn at differet locations by using the
Graphics2D.drawImage(image, x, y,...) where x and y refer to the point
where the image is to be drawn, but what I REALLY want is to have the image
on the tab replaced by the newly drawn image instead of drawing a seperate
one to begin with. Is this even possible? Do I always have to draw a
seperate image?

Again, I want to draw a line on an image I got from a tab, and then replace
the original image with the one I just drew on so when you go back to the
tab the image with a line in it will be shown. For some reason, it just
never seems to work that way!

I guess what it boils down to is once I do a drawImage, is the image that was used in that method (drawImage(image, x, y,..) modifed with the lines being drawn on it and if so, should i be able to use image by saving it off into the tab again? That's what I've been trying to do, but it hasn't worked so far.

Here is a code snippet of what I am currently doing:

<br />
private void drawLine()<br />
{<br />
        //Get the active scroll pane<br />
        m_jsPane = (JScrollPane) m_tabbedPane.getSelectedComponent();<br />
        m_port = m_jsPane.getViewport();</p>
<p>        //My version of a scroll label that allows grabbing to move it<br />
        GrabAndScrollLabel gnsLabel = (GrabAndScrollLabel) m_port.getView();</p>
<p>        m_currentImageIcon = (ImageIcon) gnsLabel.getIcon();</p>
<p>        m_currentImage = m_currentImageIcon.getImage();</p>
<p>        try<br />
        {<br />
            MediaTracker tracker = new MediaTracker(this);<br />
            tracker.addImage(m_currentImage, 0);<br />
            tracker.waitForID(0);<br />
        }<br />
        catch (InterruptedException ie)<br />
        {<br />
            //Harmless...but show signs of for testing<br />
            System.out.println("Interupted in draw");<br />
        }</p>
<p>        int iw = m_currentImage.getWidth(this);<br />
        int ih = m_currentImage.getHeight(this);</p>
<p>        m_buffIm = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);</p>
<p>        //Big is a BufferedImage at class level<br />
        big = m_buffIm.createGraphics();</p>
<p>        //The X and Y offset the image within the rect, not the rect itself<br />
        big.drawImage(m_currentImage, 0, 0, this);</p>
<p>        //Oraline just holds the begin and end points<br />
        OraLine oraLine = new OraLine(m_startX, m_startY, m_endX, m_endY);</p>
<p>        //The imageModHolder keeps track of all the lines added to an image<br />
        gnsLabel.get_oraImage().getImageModHolder().add_shape(oraLine);</p>
<p>        gnsLabel.applyModifications(big);</p>
<p>        //Now that the line is added, reset the image with  the new one<br />
        ImageIcon newIcon = new ImageIcon(m_currentImage);</p>
<p>        gnsLabel.setIcon(newIcon);</p>
<p>        m_port.setView(gnsLabel);</p>
<p>        m_jsPane.setViewport(m_port);</p>
<p>        getRootPane().revalidate();</p>
<p>        getRootPane().repaint();<br />
}</p>
<p>public void paint(Graphics g)<br />
{<br />
        super.paint(g);</p>
<p>        Graphics2D g2 = (Graphics2D) g;</p>
<p>        if (m_buffIm != null)<br />
        {<br />
            g2.drawImage(m_buffIm, 0, 0, this);<br />
        }<br />
}</p>
<p>//This is in the GrabAndScrollLabel class<br />
public BufferedImage applyModifications(Graphics2D g2D)<br />
{<br />
      ImageIcon icon = oraImage.getImageIcon();<br />
      BufferedImage bi = new BufferedImage(icon.getIconWidth(),<br />
                                           icon.getIconHeight(),<br />
                                           BufferedImage.TYPE_INT_ARGB);</p>
<p>      ImageModHolder mods = oraImage.getImageModHolder();</p>
<p>      g2D.rotate(mods.get_rotation());<br />
      g2D.scale(mods.get_zoomFactor(), mods.get_zoomFactor());</p>
<p>      Iterator shapeIter = mods.get_shapeList().iterator();</p>
<p>      while(shapeIter.hasNext())<br />
      {<br />
        OraShape shape = (OraShape)shapeIter.next();<br />
        shape.draw(g2D);<br />
      }</p>
<p>      return bi;<br />
}</p>
<p>

That's about it. THANKS for helping.

Message was edited by: Darrin

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zander
Offline
Joined: 2003-06-13

Just an idea;

[code]
icon = (ImageIcon) gnsLabel.getIcon();
ImageIcon newIcon = new ImageIcon(icon.getImage()) {
public void paintIcon(Component c, Graphics g, int x, int y) {
super.paintIcon(c, g, x, y);
g.setColor(Color.BLACK);
g.drawLine(0,0, 10, 10);
}
};

gnsLabel.setIcon(newIcon);

[/code]

ps. please add some sort of formatting tags around your code.. [/code] is use here.

Darrin
Offline
Joined: 2006-02-17

Thanks for the fast reply.

At first I thought it made a difference, but it really doesn't.

The line isn't drawn on the image, but rather in the upper left hand corner of the applet.

Here is the code I changed in drawLine()

[code]
private void drawLine()
{
m_jsPane = (JScrollPane) m_tabbedPane.getSelectedComponent();
m_port = m_jsPane.getViewport();

GrabAndScrollLabel gnsLabel = (GrabAndScrollLabel) m_port.getView();

m_currentImageIcon = (ImageIcon) gnsLabel.getIcon();

m_currentImage = m_currentImageIcon.getImage();

try
{
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(m_currentImage, 0);
tracker.waitForID(0);
}
catch (InterruptedException ie)
{
//Harmless...but show signs of for testing
System.out.println("Interupted in draw");
}

int iw = m_currentImage.getWidth(this);
int ih = m_currentImage.getHeight(this);

m_buffIm = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);

m_XOffset = m_jsPane.getX();
m_YOffset = m_jsPane.getY();

// AffineTransform af = new AffineTransform();
// af.translate(80.0d, 80.0d);
big = m_buffIm.createGraphics();

//The X and Y offset the image within the rect, not the rect itself
// big.setTransform(af);
big.drawImage(m_currentImage, 0, 0, this);

////////////////////////////////////////////////////
OraLine oraLine = new OraLine(m_startX, m_startY, m_endX, m_endY);
gnsLabel.get_oraImage().getImageModHolder().add_shape(oraLine);

gnsLabel.applyModifications(big);
////////////////////////////////////////////////////

//Begin new code
ImageIcon icon = (ImageIcon) gnsLabel.getIcon();
ImageIcon newIcon = new ImageIcon(icon.getImage())
{
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y);
g.setColor(Color.BLACK);
g.drawLine(0,0, 20, 20);
}
};

//End new code...uncomment next line though
//ImageIcon newIcon = new ImageIcon(m_currentImage);

gnsLabel.setIcon(newIcon);

m_port.setView(gnsLabel);

m_jsPane.setViewport(m_port);

getRootPane().revalidate();
getRootPane().repaint();

}
[/code]

Message was edited by: Darrin

Message was edited by: Darrin

zander
Offline
Joined: 2003-06-13

> Thanks for the fast reply.
>
> At first I thought it made a difference, but it
> really doesn't.
>
> The line isn't drawn on the image, but rather in the
> upper left hand corner of the applet.

Sorry, I make that mistake every time..
The 0,0 in drawLine should be x and y.
Or even simpler:

[code]
public void paintIcon(Component c, Graphics g, int x, int y) {
super.paintIcon(c, g, x, y);
g.translate(x, y); // don't forget this one!
g.setColor(Color.BLACK);
g.drawLine(0,0, 20, 20);
}
[/code]

Darrin
Offline
Joined: 2006-02-17

Thanks again!

I've made some modifications in the interum, let me back them out try what you sent and see what happens. I'll let you know how it went.

I really appreciate the help!

Darrin

Darrin
Offline
Joined: 2006-02-17

Well, after I commented out my overridden paint, it sure seems to work!

Now I just need to get the line stored off into my ImageModHolder object so it will be applied each time...that I should be able to handle!

Thanks!

Darrin
Offline
Joined: 2006-02-17

One quick followup if you wouldn't mind.

When I click the mouse on the image I create a line by dragging the mouse and then releasing it (the line is not drawn real time..yet). So the begin point is where the left click down happens, the drag takes place, and the end point is where the release happens.

When I draw a line from the begin to the end points, it draws off to the right of the image and a little low.

What is the best (easiest) way to get the upper left hand corner of the image relative to the component it is drawn on so I can adjust my begin and end points?

I was trying to use the JApplet.this.getWidth etc. like this:

[code]
private Point calculateOffset(Point oldPoint)
{
int tW = this.getWidth();
int tH = this.getHeight();
int iW = m_buffIm.getWidth();
int iH = m_buffIm.getHeight();

int nX = (tW / 2) - (iW / 2);
int nY = (tH / 2) - (iH / 2);

//Now at the upper left hand corner of the image

//Adjust points
oldPoint.x -= nX;
oldPoint.y -= nY;

Point newPoint = new Point(oldPoint);

return newPoint;
}

[/code]

That didn't really quite work (the X is correct, but the Y is too high..must need to use the tab instead of the applet), and I would think that there should be an easier way.

Thanks again, you have been a great help.

Message was edited by: Darrin

zander
Offline
Joined: 2003-06-13

See SwingUtilities for things like: convertPointToScreen or convertPoint.