Tabnine Logo
Graphics.drawImage
Code IndexAdd Tabnine to your IDE (free)

How to use
drawImage
method
in
java.awt.Graphics

Best Java code snippets using java.awt.Graphics.drawImage (Showing top 20 results out of 5,679)

Refine searchRefine arrow

  • Window.setVisible
  • Container.add
  • JFrame.setDefaultCloseOperation
  • JFrame.<init>
  • BufferedImage.getWidth
  • BufferedImage.getHeight
  • Window.pack
  • Dimension.<init>
origin: stackoverflow.com

 File path = ... // base path of the images

// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));

// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);

// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
origin: loklak/loklak_server

  /**
   * show the images as stream of JFrame on desktop
   */
  public void show() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    JLabel label = null;
    while (true) {
      for (int i = 0; i < this.frames.size(); i++) {
        Frame frame = this.frames.get(i);
        if (label == null) {
          label = new JLabel(new ImageIcon(frame.image));
          f.getContentPane().add(label);
          f.pack();
        } else {
          label.getGraphics().drawImage(frame.image,0,0, label);
        }
        try {Thread.sleep(frame.delayMillis);} catch (InterruptedException e) {}
      }
    }
  }
}
origin: stackoverflow.com

  return new Dimension(image.getWidth(), image.getHeight());
  int w = old.getWidth();
  int h = old.getHeight();
  BufferedImage img = new BufferedImage(
      w, h, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2d = img.createGraphics();
  g2d.drawImage(old, 0, 0, null);
  g2d.setPaint(Color.red);
  g2d.setFont(new Font("Serif", Font.BOLD, 20));
  String s = "Hello, world!";
  FontMetrics fm = g2d.getFontMetrics();
  int x = img.getWidth() - fm.stringWidth(s) - 5;
  int y = fm.getHeight();
  g2d.drawString(s, x, y);
protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.drawImage(image, 0, 0, null);
  JFrame f = new JFrame();
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.add(new TextOverlay());
  f.pack();
  f.setVisible(true);
origin: stackoverflow.com

    screen.getWidth(),
    screen.getHeight(),
    screen.getType());
final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
JScrollPane screenScroll = new JScrollPane(screenLabel);
screenScroll.setPreferredSize(new Dimension(
    (int)(screen.getWidth()/3),
    (int)(screen.getHeight()/3)));
panel.add(screenScroll, BorderLayout.CENTER);
panel.add(selectionLabel, BorderLayout.SOUTH);
    Point end = me.getPoint();
    captureRect = new Rectangle(start,
        new Dimension(end.x-start.x, end.y-start.y));
    repaint(screen, screenCopy);
    screenLabel.repaint();
g.drawImage(orig,0,0, null);
if (captureRect!=null) {
  g.setColor(Color.RED);
origin: stackoverflow.com

  JFrame frame = new JFrame("Test");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.add(new TestPane());
  frame.pack();
  op.filter(grayScale, grayScale);
  blackWhite = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
  Graphics2D g2d = blackWhite.createGraphics();
  g2d.drawImage(master, 0, 0, this);
Dimension size = super.getPreferredSize();
if (master != null) {
  size = new Dimension(master.getWidth() * 3, master.getHeight());
if (master != null) {
  int x = (getWidth() - (master.getWidth() * 3)) / 2;
  int y = (getHeight() - master.getHeight()) / 2;
  g.drawImage(master, x, y, this);
  x += master.getWidth();
  g.drawImage(grayScale, x, y, this);
  x += master.getWidth();
  g.drawImage(blackWhite, x, y, this);
origin: stackoverflow.com

  JFrame frame = new JFrame("Testing");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new BorderLayout());
  frame.add(new MaskedPane());
return new Dimension(200, 200);
g.drawImage(masked, 0, 0, this);
origin: stackoverflow.com

  BufferedImage image = ImageIO.read(new File("/path/to/your/image"));
  JFrame frame = new JFrame("Test");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new BorderLayout());
  frame.add(new ScalablePane(image));
int x = (getWidth() - toDraw.getWidth(this)) / 2;
int y = (getHeight() - toDraw.getHeight(this)) / 2;
g.drawImage(toDraw, x, y, this);
origin: stackoverflow.com

+ "The effect we want is a multi-line label.";
JFrame f = new JFrame("Label Render Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  0,
  0,
  bi.getWidth(f),
  bi.getHeight(f),
  15,
  10);
textLabel.paint(g);
Graphics g2 = image.getGraphics();
g2.drawImage(bi, 20, 20, f);
f.getContentPane().add(imageLabel);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
origin: stackoverflow.com

return new Dimension(PREF_W, PREF_H);
super.paintComponent(g);
if (backgroundImage != null) {
  g.drawImage(backgroundImage, 0, 0, null);
  g.drawImage(overlayImage, 0, 0, null);
JFrame frame = new JFrame("TestAlphaComposite");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestAlphaComposite());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
origin: stackoverflow.com

 public static BufferedImage copyImage(BufferedImage source){
  BufferedImage b = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());
  Graphics g = b.getGraphics();
  g.drawImage(source, 0, 0, null);
  g.dispose();
  return b;
}
origin: stackoverflow.com

public void showImage(final BufferedImage image) {
   JFrame frame = new JFrame();
   frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   frame.setLayout(new BorderLayout());
   JPanel imagePanel = new JPanel() {
     @Override
     public void paint(java.awt.Graphics g) {
       g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
     };
   };
   frame.getContentPane().add(imagePanel, BorderLayout.CENTER);
   frame.setSize(new Dimension(image.getWidth() + 100, image.getHeight() + 100));
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
 }
origin: stackoverflow.com

  JFrame frame = new JFrame("Test");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new BorderLayout());
  frame.add(new TestPane());
g.drawImage(dropShadow, x, y, c);
origin: stackoverflow.com

JFrame frame = new JFrame("Image Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
  return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight());
protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.drawImage(image, 0, 0, this);
origin: stackoverflow.com

    JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(N, N, N, N));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for (int i = 0; i < N * N; i++) {
      frame.add(new RotatePanel());
    frame.pack();
    frame.setVisible(true);
this.setPreferredSize(new Dimension(
  image.getWidth(null), image.getHeight(null)));
this.addMouseListener(new MouseAdapter() {
g2d.rotate(theta);
g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2d.drawImage(image, 0, 0, null);
return new Dimension(SIZE, SIZE);
origin: stackoverflow.com

 BufferedImage img = image.getSubimage(startX, startY, endX, endY); //fill in the corners of the desired crop location here
BufferedImage copyOfImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = copyOfImage.createGraphics();
g.drawImage(img, 0, 0, null);
return copyOfImage; //or use it however you want
origin: stackoverflow.com

 public static void main(String[] args) throws IOException {

  JFrame frame = new JFrame("Test");

  frame.setContentPane(new JPanel() {
    BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawImage(image, 0, 0, 300, 300, this);
    }
  });

  frame.add(new JButton("Test Button"));

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300, 300);
  frame.setVisible(true);
}
origin: stackoverflow.com

add(usrNameLabel, labCnst);
add(usrNameFeild, txtCnst);
add(passwordLabel, labCnst);
add(passFeild, txtCnst);
Rectangle2D txRect = new Rectangle2D.Double(0, 0, textureImg.getWidth(), textureImg.getHeight());
TexturePaint txPaint = new TexturePaint(textureImg, txRect);
g2d.setPaint(txPaint);
Graphics2D g2d = (Graphics2D) g.create();
if(gradientImage==null || gradientImage.getHeight() != getHeight())
g2d.drawImage(gradientImage, 0, 0, getWidth(), getHeight(), this);
g2d.dispose();
    JFrame frame = new JFrame("Demo: LogIn Dialogue");
    frame.setSize(new Dimension(500, 300)); 
    MainContainer container = new MainContainer();
    frame.add(new MainContainer());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
origin: stackoverflow.com

JFrame f = new JFrame("AnimationTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
timer.start();
super(true);
this.setOpaque(false);
this.setPreferredSize(new Dimension(WIDE, HIGH));
this.addMouseListener(new MouseHandler());
this.addComponentListener(new ComponentHandler());
int w = this.getWidth();
int h = this.getHeight();
g.drawImage(background, 0, 0, this);
double theta = 2 * Math.PI * index++ / 64;
g.setColor(Color.blue);
  Dimension d = field.getPreferredSize();
  field.setBounds(e.getX(), e.getY(), d.width, d.height);
  add(field);
origin: coobird/thumbnailator

  /**
   * Returns a {@link BufferedImage} with the specified image type, where the
   * graphical content is a copy of the specified image.
   * 
   * @param img        The image to copy.
   * @param imageType    The image type for the image to return.
   * @return            A copy of the specified image.
   */
  public static BufferedImage copy(BufferedImage img, int imageType)
  {
    int width = img.getWidth();
    int height = img.getHeight();
    
    BufferedImage newImage = new BufferedImage(width, height, imageType);
    Graphics g = newImage.createGraphics();
    
    g.drawImage(img, 0, 0, null);
    
    g.dispose();
    
    return newImage;
  }
}
origin: stackoverflow.com

 JPanel panel = new JPanel() {
  private Image img = ImageIO.read(new File("C:\\Users\\DELL\\Desktop\\football.jpg"));
  @Override
  public void paintComponent(Graphics g) { 
    super.paintComponent(g);

    g.drawImage(img, 0,0,1366,730, this);
  }

  etc...
};

final JFrame f1 = new JFrame("Front Main");
f1.add(panel);
f1.setSize(1366,730);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
java.awtGraphicsdrawImage

Javadoc

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.

Transparent pixels are drawn in the specified background color. This operation is equivalent to filling a rectangle of the width and height of the specified image with the given color and then drawing the image on top of it, but possibly more efficient.

This method returns immediately in all cases, even if the image area to be drawn has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that draws the image notifies the specified image observer.

This method always uses the unscaled version of the image to render the scaled rectangle and performs the required scaling on the fly. It does not use a cached, scaled version of the image for this operation. Scaling of the image from source to destination is performed such that the first coordinate of the source rectangle is mapped to the first coordinate of the destination rectangle, and the second source coordinate is mapped to the second destination coordinate. The subimage is scaled and flipped as needed to preserve those mappings.

Popular methods of Graphics

  • setColor
  • fillRect
    Fills the specified rectangle. The left and right edges of the rectangle are atx and x + width - 1.
  • drawLine
    Draws a line, using the current color, between the points(x1, y1) and (x2, y2) in this graphics con
  • drawString
    Draws the text given by the specified iterator, using this graphics context's current color. The ite
  • dispose
    Disposes of this graphics context and releases any system resources that it is using. A Graphics obj
  • setFont
    Sets this graphics context's font to the specified font. All subsequent text operations using this g
  • drawRect
    Draws the outline of the specified rectangle. The left and right edges of the rectangle are atx and
  • getFontMetrics
  • create
    Creates a new Graphics object based on thisGraphics object, but with a new translation and clip area
  • getColor
    Gets this graphics context's current color.
  • translate
    Translates the origin of the graphics context to the point (x,y) in the current coordinate system. M
  • getClipBounds
    Returns the bounding rectangle of the current clipping area. The coordinates in the rectangle are re
  • translate,
  • getClipBounds,
  • setClip,
  • getFont,
  • fillPolygon,
  • fillOval,
  • drawOval,
  • getClip,
  • drawRoundRect

Popular in Java

  • Reactive rest calls using spring rest template
  • putExtra (Intent)
  • startActivity (Activity)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • Point (java.awt)
    A point representing a location in (x,y) coordinate space, specified in integer precision.
  • EOFException (java.io)
    Thrown when a program encounters the end of a file or stream during an input operation.
  • SortedMap (java.util)
    A map that has its keys ordered. The sorting is according to either the natural ordering of its keys
  • HttpServlet (javax.servlet.http)
    Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A sub
  • Logger (org.apache.log4j)
    This is the central class in the log4j package. Most logging operations, except configuration, are d
  • CodeWhisperer alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now