Package jtrackbase.db

Source Code of jtrackbase.db.StockArea

/* License see bottom */
package jtrackbase.db;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.UniqueConstraint;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@Entity
@Table(
      uniqueConstraints={@UniqueConstraint(columnNames={"NAME"})}
)
public class StockArea extends CommentedEntity {
  @Lob
    @Basic(fetch=FetchType.LAZY)
  private byte[] imageData;
 
  @Id
  @GeneratedValue(generator = "StockAreaIdGenerator")
  @TableGenerator(name = "StockAreaIdGenerator",
      table = "ID_GENERATOR",
      pkColumnName = "ID_NAME",
      valueColumnName = "ID_VAL",
      pkColumnValue = "StockArea")
  private long id;
 
  public void setImage(ImageIcon ii) {
    int resizeWidth = ii.getIconWidth();
    int resizeHeight = ii.getIconHeight();

    JPanel p = new JPanel();
    BufferedImage bi = new BufferedImage(resizeWidth, resizeHeight,
    BufferedImage.TYPE_INT_RGB);

    Graphics2D big = bi.createGraphics();
    big.drawImage(ii.getImage(), 0, 0, p);

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
    try {
      encoder.encode(bi);
    } catch (IOException ex) {
      throw new IllegalArgumentException(ex);
    }
    imageData = os.toByteArray();
  }
 
  public ImageIcon getImage() {
    return new ImageIcon(imageData);
  }

  @Override
  public long getId() {
    return id;
  }

  @Override
  public void setId(long id) {
    this.id=id;
  }
}
/*
Copyright (C) 2008  Onkobu Tanaake

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program (gplv3.txt).
*/
TOP

Related Classes of jtrackbase.db.StockArea

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.