14 Replies - 683 Views - Last Post: 22 January 2012 - 01:38 AM Rate Topic: -----

#1 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Colour isn't saved in the file, but it's displaied on the scre

Posted 18 January 2012 - 09:27 PM

Hello, I would be glad if anyone could help me with this. I'm trying to save a picture painted by a user. It's like a colouring picture - user gets template and I want him to colour it. The problem is, that on the screen it looks like the brush has chosen colour, nevertheless in the saved file there are only white lines from the brush.The background (template) is saved to the file colourfully. There is my mouse event handler code, which handles drawing and saving(I know it's a dummy code, but it's not importaint app)

  private void myMouseDragPaint(java.awt.event.MouseEvent evt) {
        switch (evt.getModifiers()) {
            case InputEvent.BUTTON1_MASK: {
                Graphics G = drawPanel.getGraphics();

                // Create a buffered image in which to draw
                BufferedImage bufferedImage = null;
                try {
                    bufferedImage = ImageIO.read(new File("newimage.jpg"));
                } catch (IOException e) {
                    createImagetoDrawIn(drawPanel);
                    try {
                        bufferedImage = ImageIO.read(new File("newimage.jpg"));
                    } catch (IOException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }

                Graphics2D g2d = bufferedImage.createGraphics();

                int currX = evt.getX();
                int currY = evt.getY();

                if (prevX == 0) {
                    prevX = currX;
                }
                if (prevY == 0) {
                    prevY = currY;
                }

                if (myColorString == "Red") {
                    G.setColor(Color.red);
                    g2d.setColor(Color.red);
                } else if (myColorString == "Orange") {
                    G.setColor(Color.orange);
                    g2d.setColor(Color.orange);
                } else {
                    G.setColor(Color.black);
                    g2d.setColor(Color.black);
                }


                switch (lineThickness) {
                    case 1:
                        G.drawLine(prevX, prevY, currX, currY);
                        g2d.drawLine(prevX, prevY, currX, currY);
                        RenderedImage rendImage = bufferedImage;

                        try {
                            // Save as JPG
                            File file = new File("newimage.jpg");
                            ImageIO.write(rendImage, "jpg", file);
                        } catch (IOException e) {
                        }
                        break;
               }
            }         
        }
    }



The lines are saved to the picture but they are only white...and I need them to be colourful...Can anybody please help and tell my, why the colour isn't saved too? Thaks

This post has been edited by Dworza: 18 January 2012 - 09:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Colour isn't saved in the file, but it's displaied on the scre

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 18 January 2012 - 09:45 PM

You can't compare Object (like String) using the == operator you need the equals() method to compare Object

myColorString == "Red") {

should be

myColorString.equals("Red")) {
Was This Post Helpful? 1
  • +
  • -

#3 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 18 January 2012 - 09:48 PM

Aaahhh :) so dumb bug :) Thanks :) Hmm but I still wonder, how is then possible that printing on the screen worked allways well?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 18 January 2012 - 09:56 PM

depends where myColorString is defined and your paint() method works

== may work in the same class because the Java compiler is brillant enough not to duplicate String constants

   String a = "ABC";
   String b = "ABC";

   a == b because both a and b point to the same and [b]unique[/b] String constant "ABC"

   but 

   String x = "AB";
   String y = "C";

   String c = x + y;

   c != a and c != b but c.equals(a) and c.equals(B)/>

   otherMethod(a);

...

   void otherMethod(String str) {
      str may not == "ABC" but str.equals("ABC")


Was This Post Helpful? 1
  • +
  • -

#5 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 18 January 2012 - 10:01 PM

Ah so..I got it now :) Thank you for the explanation.
Was This Post Helpful? 0
  • +
  • -

#6 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 06:54 PM

I've got another problem with my colouring picture app. I've implemented adding a JTextField into the image
JTextField l = new JTextField();
l.contains(evt.getX(), evt.getY());
int x = evt.getX();
int y = evt.getY();
l.setLocation(x - 15, y - 15);
l.setSize(40, 40);
l.setVisible(true);
jLabel1.add(l);
jLabel1.repaint();;



,but after adding JTextField and repainting, the graphic painted by the user of course disappears...
Any idea, how to have the drawing in the JPanel on which user draws, still after the repaintg? I already tried saving the screenshot of whole JPanel to the file and after that loading it as an Icon of a JLabel which was added to the JPanel...But I can't use it because of the low quality of the picture :(
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 07:23 PM

l.contains(evt.getX(), evt.getY());

return true or false depending if the JTextField contains X,Y
What do you do with the returned value ? If you don't save it why calling the method ?

what is ?
jLabel1.add(l);
Was This Post Helpful? 0
  • +
  • -

#8 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 07:36 PM

Sorry..that contains() function should be deleted - it's useless.

jLabel1 is a JLabel placed on the "drawing panel". This jLabel1 is used for displaying it's Icon as a background-a template that should be coloured...So it's something like:
JPanel drawPanel = new JPanel();

JLabel jLabel1 = new JLabel();
jLabel1.setIcon(new ImageIcon("someimage.jpg"))
drawPanel.add(jLabel1);

JTextField l = new JTextField();
jLabel1.add(l);


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 07:41 PM

never tried to add() a JComponet to a JLabel
What do you think it is supposed to do ? I'll try it and comme back
Was This Post Helpful? 0
  • +
  • -

#10 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 07:44 PM

it works quite well..the only thing is, that it deletes the graphic..as well as moving the window or resizing it does :-/
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 07:52 PM

This works

public class LabelOver extends JFrame {

	LabelOver() {
		super("Test");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		setSize(200, 200);
		JLabel label = new JLabel("");
		label.setOpaque(true);
		label.setBackground(Color.YELLOW);
//		JTextField tf = new JTextField("abc");
//		label.add(tf);
		add(label);
	}
	
	public static void main(String[] args) {
		new LabelOver();
	}
}


but this does not work, the frame is not YELLOW
public class LabelOver extends JFrame {

	LabelOver() {
		super("Test");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		setSize(200, 200);
		JLabel label = new JLabel("");
		label.setOpaque(true);
		label.setBackground(Color.YELLOW);
		JTextField tf = new JTextField("abc");
		label.add(tf);
		add(label);
	}
	
	public static void main(String[] args) {
		new LabelOver();
	}
}


but this does not work neither.. I am really puzzled but really puzzled
public class LabelOver extends JFrame {

	LabelOver() {
		super("Test");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		setSize(200, 200);
		JLabel label = new JLabel("");
		label.setOpaque(true);
		label.setBackground(Color.YELLOW);
		JTextField tf = new JTextField("abc");
//		label.add(tf);
		add(label);
	}
	
	public static void main(String[] args) {
		new LabelOver();
	}
}


:)

This post has been edited by pbl: 21 January 2012 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 08:02 PM

OK works, ouf !!! if you put the

setVisible(true);

at the end. I though I was turning nuts
But the JTextField does not show as expected. You cannot add a JComponent to a JLabel
make it a JPanel if you want to do that kind of thing :^:
Was This Post Helpful? 0
  • +
  • -

#13 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 08:14 PM

uh..that's really weird :-S It works fine by me..In my code I init the components and define there jLabel1.setIcon(someimage.jpg) and adding the JTexetField is handled by the MouseListener.
Nevertheless I'm not sure if you understood the point of my post..
User draws a picture by dragging the mouse using Graphic which is painted on a JPanel drawPanel...and after adding new JTextField and repainting jLabel1 the drawing disappears :(
Was This Post Helpful? 0
  • +
  • -

#14 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 21 January 2012 - 08:14 PM

uh..that's really weird :-S It works fine by me..In my code I init the components and define there jLabel1.setIcon(someimage.jpg) and adding the JTexetField is handled by the MouseListener. But you're right adding JTextField to a JPanel would be smarter..
Nevertheless I'm not sure if you understood the point of my post..
User draws a picture by dragging the mouse using Graphic which is painted on a JPanel drawPanel...and after adding new JTextField and repainting jLabel1 the drawing disappears :(

This post has been edited by Dworza: 21 January 2012 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#15 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Colour isn't saved in the file, but it's displaied on the scre

Posted 22 January 2012 - 01:38 AM

solved...But not the solution is not smart...But nevermind - it's not important application... :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1