0

I'm trying to use Graphics to draw a rectangle that select an area to crop an image, I can load the images and cut, but my rectangle doesn't draw in my form and I don't know why.

The program load images of a specific folder and pass the next with a button, then I select the part of imagen (here I have to see a rectangle), then I press "Recortar" and my cropped image is saved in other folder.

I have been reading and trying a lot of things and I haven't found a solution.

This is my code:

public class NewJFrame extends javax.swing.JFrame {

    int z1=0;
    int z2=0;
    int z=0;
    float x=0;
    float y=0;
    float ancho=0;
    float alto=0;

Graphics2D g2D;
Image img;
BufferedImage img_bufi;
BufferedImage Imagmemoria;
BufferedImage imgrecortada;

public NewJFrame() {
    add(new Dibujar()); //here I add Dibujar and this have paintComponent I want to use
    initComponents();
    setVisible(true);
    super.repaint() ;
}


private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    x = evt.getX();
    y = evt.getY();
}                                    

private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    ancho = evt.getX()-x;
    alto = evt.getY()-y;
    if(ancho<0) ancho=0;
    if(alto<0) alto=0;
    if(x > this.getWidth()) x = this.getWidth() - ancho ;
    if(y > this.getHeight()) y = this.getHeight() - alto ;
    System.out.println("X: "+x+"  Y: "+y+" Ancho: "+ancho+" Alto: "+alto);
                }                                    

private void jLabel1MouseReleased(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:

}                                     


public class Dibujar extends JLabel{

    @Override
    public void paintComponent(Graphics g) {
    //public void paint(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      if(img!=null){
        Imagmemoria = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
        g2D = Imagmemoria.createGraphics();
        g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2D.drawImage(img,0, 0, img.getWidth(this), img.getHeight(this), this);
        g2D.setStroke(new BasicStroke(2f));
        g2D.setColor(Color.WHITE);
        Rectangle2D r2 = new Rectangle2D.Float( x, y, ancho, alto );
        g2D.draw(r2);
        g2.drawImage(Imagmemoria, 0, 0, this);
        System.out.println("paintComponent");
      }
    }
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
// End of variables declaration                   
}
  • 2
    Whoa, you've got a *lot* of unrelated code contained in your question. Please help us help you: create a **new** smaller program just for this question, one that gets its image from an online URL source available to all and not from an image on your computer, one that has code that allows the program to compile and that shows us your problem, but no other unrelated code. I'm asking you to create a valid [mcve]. Do this, and we'll be in a much better position to answer your question and help you. – Hovercraft Full Of Eels Nov 24 '17 at 21:32
  • Also you've got some issues popping up that code including trying to read in an image file from within a paintComponent method -- **never** do that. Read the image once and elsewhere, and only paint within that method and nothing else. – Hovercraft Full Of Eels Nov 24 '17 at 21:34
  • Also, what are you expecting `leerArreglo()` and `contarLineas_llenarArreglo(BufferedReader br)` to be doing? You do know that these methods are being called before the user has any opportunity to interact with the GUI, right? – Hovercraft Full Of Eels Nov 24 '17 at 21:39
  • Why do you draw a Rectangle and then draw an Image. Won't the image paint over top of the Rectangle. I see no reason to draw the Image. That is the job of the JLabel to paint the image. – camickr Nov 24 '17 at 21:41
  • Also your Dibujar object is created, added to the JFrame, but you have no variable reference to it in the program, and you add the object without respecting (or understanding) the layout managers that your JFrame's contentPane is using. This is dangerous code. – Hovercraft Full Of Eels Nov 24 '17 at 21:42
  • 1
    You have a couple of major issues. The first is using the netbeans design while trying to also trying the manually update the UI. `GroupLayout` is not the easiest layout manager to deal with at the best of times. IMHO, you should ditch the form editor and focus on generating the UI manually, you'll have better success – MadProgrammer Nov 24 '17 at 21:42
  • Because of the way all this should work, the `JLabel` should be wrapped in a `JScrollPane`, which will allow it the possibility of a variable size without adversely affecting the rest of the layout. You also have no control over where the label will be positioned in the icon (you can "guess", but I don't like "guessing"). A better solution would be to use a `JPanel` as the base component and take full control. You "selectable component" should also be self contained, meaning the `MouseLIstener` should be attached to it and the properties need to maintain the selection controlled by it – MadProgrammer Nov 24 '17 at 21:46
  • I need to leave a default directory, for that I read a txt with all my images, before the user does something more (maybe ther is a better option to do that). – Gladis Vite Nov 24 '17 at 21:49
  • [That's one basic example](https://stackoverflow.com/questions/32981758/easier-way-to-make-a-paint-application-in-java/32981915#32981915), [that's another](https://stackoverflow.com/questions/21129138/difference-between-graphics-object-of-getgraphics-and-paintcomponent/21129165#21129165) – MadProgrammer Nov 24 '17 at 21:50
  • I did another example with JPanel, but I couldn't update the next image, the panel never change and I saw the same image. – Gladis Vite Nov 24 '17 at 21:51
  • I need draw a rectangle with the area to cut, not exactly draw a rectangle, just the borders, so the user can see the crop image before to save it. @MadProgrammer I will check your examples, thanks :) – Gladis Vite Nov 24 '17 at 21:54
  • Now your posted code won't compile as you're calling methods that no longer exist. Please only post code that you've tested first. – Hovercraft Full Of Eels Nov 24 '17 at 21:54
  • @GladisVite You mean something like [this](https://stackoverflow.com/questions/35094421/translucent-jpanel-not-clearing-background-showing-background-artifacts-in-lin/35119129#35119129) and [this](https://stackoverflow.com/questions/23709070/how-to-disable-java-awt-graphics-fillrectint-x-int-y-int-width-int-heights/23709320#23709320) – MadProgrammer Nov 24 '17 at 21:56
  • @MadProgrammer Thanks!! your examples help me a lot!! I cuold resolve my problem :), but the image no update in ImagePane :(, I pass to the next image of my folder and this doesn't update, but at least I can draw a rectangle and save the crop image :), thanks a lot!!! – Gladis Vite Nov 27 '17 at 15:32

0 Answers0