dcsimg
About XORMode and Strokes.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   vijay_solanki
Posted On:   Saturday, October 22, 2005 04:18 AM

Hi, I have an application in which I have to display one rectangular box moving while mouse is dragged. I wrote the code. When I use draw() method of Graphics2D class with stroked shape, the box does appear, but suddenly it disappears giving a blinking box. On the other hand, when I use drawRect() method the green box is shown correctly. Also, When I use draw() with stroked shape as parameter, the rectangle drawn is fully shown above all other background colors ( in my case it is an image on which i m drawing rectangle ). But when I use drawRect() method, the rectangle is hidden behind the colors except black. code is like this: Graphics2D g2d = getGraphics();    More>>

Hi,

I have an application in which I have to display one rectangular box moving while mouse is dragged. I wrote the code. When I use draw() method of Graphics2D class with stroked shape, the box does appear, but suddenly it disappears
giving a blinking box. On the other hand, when I use drawRect() method the green box is shown correctly.



Also, When I use draw() with stroked shape as parameter, the rectangle drawn is fully shown above all other background colors ( in my case it is an image on which i m drawing rectangle ). But when I use drawRect() method, the rectangle is hidden behind the colors except black.


code is like this:



Graphics2D g2d = getGraphics();

g2d.setXORMode(Color.green);


BasicStroke b = new BasicStroke(...);



/*The box drawn with this is not fully drawn (only the lines above black background are showing)*/

//g2d.drawRect(rect.x, rect.y, rtect.width, rect.height);


g2d.draw(b.createStrokedShape(rect));


getGraphics().setPaintMode();





Thanks in anticipation.

- Vijay

   <<Less

Re: About XORMode and Strokes.

Posted By:   Craig_Wood  
Posted On:   Sunday, October 23, 2005 03:44 AM

Try this

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;

public class StrokeTest extends JPanel
{
BufferedImage image;
Rectangle r;
BasicStroke stroke;

public StrokeTest(BufferedImage image)
{
this.image = image;
r = new Rectangle(50,50,100,100);
stroke = new BasicStroke(3f);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int x = (w - image.getWidth())/2;
int y = (h - image.getHeight())/2;
g2.drawImage(image, x, y, this);
g2.setXORMode(Color.green);
//g2.drawRect(r.x, r.y, r.width, r.height);
g2.draw(r);
AffineTransform at = AffineTransform.getTranslateInstance(150, 150);
g2.fill(at.createTransformedShape(stroke.createStrokedShape(r)));
g2.setPaintMode();
}

public void setRect(int x, int y)
{
r.x = x;
r.y = y;
repaint();
}

public static void main(String[] args) throws IOException
{
String path = "images/owls.jpg";
BufferedImage image = ImageIO.read(StrokeTest.class.getResource(path));
StrokeTest test = new StrokeTest(image);
Dragster dragster = new Dragster(test);
test.addMouseListener(dragster);
test.addMouseMotionListener(dragster);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}

class Dragster extends MouseInputAdapter
{
StrokeTest strokeTest;
Point offset;
boolean dragging;

public Dragster(StrokeTest st)
{
strokeTest = st;
offset = new Point();
dragging = false;
}

public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
Rectangle r = strokeTest.r;
if(r.contains(p))
{
offset.x = p.x - r.x;
offset.y = p.y - r.y;
dragging = true;
}
}

public void mouseReleased(MouseEvent e)
{
dragging = false;
}

public void mouseDragged(MouseEvent e)
{
if(dragging)
{
int x = e.getX() - offset.x;
int y = e.getY() - offset.y;
strokeTest.setRect(x, y);
}
}
}
About | Sitemap | Contact