Drawing on BufferedImage in thread - java

I'm trying to draw on a BufferedImage from a thread, then displaying said image on a canvas.
Everything works fine when I alter the BufferedImage from within the paintComponent-method of the Canvas, but as soon as I try to alter it from a running thread - nothing happens.
In other words; If I copy the Graphics2D-code from the thread to the paintComponent-method, everything works fine.
A snippet of my code (some of the variables/values are temporary):
class DrawThread implements Runnable
{
#Override
public void run()
{
while(true)
{
Graphics2D g2d = canvas.getImage().createGraphics();
g2d.setColor(Color.GREEN);
Rectangle rect2 = new Rectangle(10, 0, 20, 20);
g2d.draw(rect2);
g2d.dispose();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class RectangleCanvas extends Canvas
{
public BufferedImage image;
public RectangleCanvas()
{
this.setPreferredSize(new Dimension(280, 280));
image = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
}
public synchronized BufferedImage getImage()
{
return this.image;
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(getImage(), 0, 0, this);
}
}

Related

image is drawn half second later then other paint compontents

After I start my applet every component is drawn alright, besides my background image that is drawn with about a half second delay. I deleted my thread thinking it's maybe the cause of my problem, but it's not, so i didn't include it here.... I use Double Buffering, because I would have flickering of my components that are repainted by thread. I tried to provide as little code as possible....
public class balg extends Applet implements Runnable {
private Image i;
private Graphics doubleG;
URL url;
Image city; //background image
public void init(){
setSize(800, 600);
try{
url = getDocumentBase();
}catch(Exception e){
}
city = getImage(url , "multiplen/images/SPACE.png");
}
public void start(){
Thread thread = new Thread(this);
thread.start();
}
public void run(){
// here goes the repiant();
}
public void stop(){
}
public void destroy(){
}
#Override
public void update(Graphics g) {
if(i == null){
i = createImage(this.getSize().width, this.getSize().height);
doubleG = i.getGraphics();
}
doubleG.setColor(getBackground());
doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
doubleG.setColor(getForeground());
paint(doubleG);
g.drawImage(i, 0,0, this);
}
public void paint(Graphics g){
g.drawImage(city,(int) 800 , 0 , this); // it's drawn here
String s = "15";
g.setColor(Color.BLACK);
g.drawString(s, getWidth() - 150, 50);
}
}
It takes that much time to read the image, about 100-200 ms.

Scaled image does not appear correctly until Frame is resized

I am trying to scale an image with AffineTransform. However, the image does not appear correctly when the Frame appears initially.
But after the frame is resized, the image shows perfectly.
Here is the code.
public class MainFrame extends javax.swing.JFrame {
Line line;
BufferedImage bi;
public MainFrame() {
initComponents();
try {
bi = ImageIO.read(new File("map.png"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform old = g2.getTransform();
g2.scale(0.25, 0.25);
g2.drawImage(bi, 0, 0, null);
g2.setTransform(old);
}
...
}

How to change background color of jDesktopPane which is created usning tools in netbeans

By unsing netbeans ide , I created a JDesktopPane inside the JFrame. and I cannot change the color of the jdesktopPane.. I tried all I can. But when I open the JFrame .. the JDesktopPane inside that JFrame is in some blue color background.
Please help me to change the background of JDesktopPane
I'm going to assume you're using GUI Builder with the default Nimbus look and feel (because you said you've tried everything, and I'll assume you've tried setBackground). The look and feel has the background set. But you have options around it.
You can just paint the background. You want to look at this answer for how to edit the auto-generated code. Then you can just to this, when you edit the code. Don't forget to hit ctrl+shift+I afterwards, to resolve all imports. I'm too lazy to write fully qualified names.
jDesktopPane1 = new javax.swing.JDesktopPane() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
If you want an image, you can paint an image
jDesktopPane1 = new javax.swing.JDesktopPane() {
private Image image;
{
try {
image = ImageIO.read(new URL("http://www.hdbackgroundspoint.com/wp-content/uploads/2013/12/16/345t34.jpeg"));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
};
You could also override the Nimbus default DesktopPane[Enabled].backgroundPainter. See Nimbus Defaults here
public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo laf : UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
UIManager.getLookAndFeelDefaults().put(
"DesktopPane[Enabled].backgroundPainter",
new DesktopPainter());
}
}
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDesktopPaneDemo();
}
});
}
static class DesktopPainter implements Painter<JComponent> {
private Image image;
public DesktopPainter() {
try {
image = ImageIO.read(new URL("http://www.hdbackgroundspoint.com/wp-content/uploads/2013/09/hh.jpeg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.drawImage(image, 0, 0, width, height, null);
}
}

drawImage within separate jpanel draws just a piece

I'm not that familiar with Java and I'm a little clueless about my current problem.
I'm trying to draw an Image within a separate class of my Main JFrame, but it
always draw just a little piece of the picture (maybe 10x10px).
(A test with a Label worked)
Maybe I didn't used the g.drawImage method correct, or the JPanel don't have enough space??
MainWindow:
public class Deconvolutioner extends JFrame {
Draw z;
Picturearea picturearea;
class Draw extends JPanel {
public void paint(Graphics g) {
}
}
public Deconvolutioner() {
setTitle("Deconvolutioner");
setLocation(30,1);
setSize(1300,735);
super.setFont(new Font("Arial",Font.BOLD,11));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
this.setLayout(flow);
picturearea = new Picturearea();
this.add(picturearea);
add(z = new Draw());
setVisible(true);
}
class Open implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fileOpen = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("png & jpg files", "png",
"jpg");
fileOpen.addChoosableFileFilter(filter);
int returnVal = fileOpen.showDialog(null, "Open file");
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
String path = fileOpen.getSelectedFile().getPath();
URL url = new File(path).toURI().toURL();
BufferedImage img = ImageIO.read(url);
picturearea.setPicture(img);
} catch (IOException ex) {
System.err.println("Some IOException accured (set the right path?): ");
System.err.println(ex.getMessage());
}
} else {
}
repaint();
}
}
And the separate class:
public class Picturearea extends JPanel {
public BufferedImage image;
Draw z;
public Picturearea() {
add(z = new Draw());
setVisible(true);
}
class Draw extends JPanel {
#Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}
public void setPicture(BufferedImage picture) {
try {
image = picture;
} catch (Exception e) {
System.err.println("Some IOException accured (did you set the right path?): ");
System.err.println(e.getMessage());
}
repaint();
}
}
I'm grateful for every help.
thanks for your time.
You are using FlowLayout for JFrame's contentPane's layout. FlowLayout obeys component's preferredSize. Try setting preferredSize of your pictureArea by pictureArea.setPreferredSize(Dimension) or overriding getPreferredSize(Dimension) function in PictureArea class.
you are using paint(Graphics g) for custom painting:
#Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
Do not override paint() for custom painting override paintComponent(Graphics g) instead. And You may need to scale your image to fit in the size of the JPanel. you can use g.drawImage(x, y, width, height, observer) function if you need to scale your image.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}

panel background image take screenshot

I'm trying to draw over an image (with the mouse) in a JPanel, this is working, but when I try to take an screenshot of the panel and generate an image of this, I only can see the image background without drawn with the mouse.
This is my code to generate the background Panel.java
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.createImage("/imagenes/cuerpoHumano.png").getImage(), 0, 0, null);
}
This is my code to draw as a pencil over the image: Panel.java
private void formMouseDragged(java.awt.event.MouseEvent evt) {
x = evt.getX();
y = evt.getY();
this.getGraphics().setColor(Color.RED);
this.getGraphics().fillOval(x, y, 4, 4);
}
This is the code to generate an screenshot
Dimension size = panel.getSize();
BufferedImage image = (BufferedImage) panel.createImage(size.width, size.height);
Graphics g = image.getGraphics();
panel.paint(g);
g.dispose();
try {
String fileName = UUID.randomUUID().toString().substring(0, 18);
ImageIO.write(image, "jpg", new File(path, fileName + ".jpg"));
} catch (IOException e) {
e.printStackTrace();
}
When you are taking the screenshot, the paintComponent() method is called. This means it will only paint you the image. You have to store the mouse move inside some model and paint the contents of the model in the paintComponent() method. This method is triggered by calling repaint() on the panel during the mouse move.
I think this is code that works .
public class PanelImagenCuerpoHumano extends JPanel {
private int x = -1;
private int y = -1;
private Image image = null;
private ArrayList<Point> puntos = new ArrayList<Point>();
public PanelImagenCuerpoHumano() {
image = new ImageIcon(getClass()
.getResource("/imagenes/cuerpoHumano.png")).getImage();
this.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
puntos.add(new Point(x, y));
repaint();
}
#Override
public void mouseMoved(MouseEvent e) {
}
});
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
for (Point p : puntos) {
g.setColor(Color.red);
g.fillOval(p.x, p.y, 3, 3);
}
}
}

Categories

Resources