Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java image draw

package com.javacodegeeks.snippets.desktop;
 
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
 
public class DrawImage {
 
  static Image image;
 
  public static void main(String[] args) {
 
// The image URL - change to where your image file is located!
 
String imageURL = "image.png";
 
// This call returns immediately and pixels are loaded in the background
 
image = Toolkit.getDefaultToolkit().getImage(imageURL);
 
// Create a frame
 
Frame frame = new Frame();
 
// Add a component with a custom paint method
 
frame.add(new CustomPaintComponent());
 
// Display the frame
 
int frameWidth = 300;
 
int frameHeight = 300;
 
frame.setSize(frameWidth, frameHeight);
 
frame.setVisible(true);
 
 }
 
 /**
  * To draw on the screen, it is first necessary to subclass a Component 
  * and override its paint() method. The paint() method is automatically called 
  * by the windowing system whenever component's area needs to be repainted.
  */
  static class CustomPaintComponent extends Component {
 
public void paint(Graphics g) {
 
  // Retrieve the graphics context; this object is used to paint shapes
 
  Graphics2D g2d = (Graphics2D)g;
 
  /**
   * Draw an Image object
   * The coordinate system of a graphics context is such that the origin is at the 
   * northwest corner and x-axis increases toward the right while the y-axis increases 
   * toward the bottom.
   */
 
  int x = 0;
 
  int y = 0;
 
  g2d.drawImage(image, x, y, this);
 
}
 
  }
 
}
Comment

java draw image

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SwingSandbox {

    public static void main(String[] args) throws IOException {
        JFrame frame = buildFrame();

        final BufferedImage image = ImageIO.read(new File("C:ProjectsMavenSandboxsrcmain
esourcesimg.jpg"));

        JPanel pane = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, null);
            }
        };


        frame.add(pane);
    }


    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


}
Comment

PREVIOUS NEXT
Code Example
Java :: springboot validator manually validate 
Java :: math.pow(x2-x1,2) 
Java :: java enum in switch statement 
Java :: private access modifiers 
Java :: try catch block 
Java :: contain java 
Java :: method overriding in java 
Java :: arraylist methods in java 
Java :: java spring mvc 
Java :: properties object java 
Java :: mcq java 
Java :: java meeting scheduler 
Java :: javafx combobox cell 
Java :: java textwatcher 
Java :: how todo a timeout test java 
Java :: start hadoop and yarn with java 
Java :: java listfiles filter 
Java :: print all prime no java 
Java :: .code domain 
Java :: how to open material navigation view on button click 
Java :: zoom sdk not initialized 
Java :: for loop optimized java 
Java :: 2d matrix multiplication 
Java :: i have use tried catch but still prints exception java 
Java :: output of java file in terminal 
Java :: Java @FunctionalInterface annotation 
Java :: Java Using allOf(Size) 
Java :: facebook share android Studio 
Java :: java remove numbers from set 
Java :: android studio press button programmatically 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =