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 :: jsp get query parameter 
Java :: string to bigdecimal 
Java :: view binding in recyclerview adapter android java 
Java :: map interation in java 
Java :: bytes to string solidity 
Java :: get average of all the array items android 
Java :: android studio constraint layout proportional height 
Java :: how to overwrite a text file in java 
Java :: java map to list or array 
Java :: java convert to roman numerals 
Java :: hello world in different programming languages 
Java :: android get list element by index 
Java :: Who is James Gosling 
Java :: caused by: java.lang.noclassdeffounderror: org/springframework/boot/configurationprocessor/json/jsonexception 
Java :: string reverse in java 
Java :: core java tutorial 
Java :: image cropper implementation 
Java :: Index through 2d array 
Java :: How to initialize a 2d array in Java? 
Java :: java create file in folder 
Java :: java comments 
Java :: spring boot save file to static folder 
Java :: clone array in java 
Java :: java arraylist deepcopy 
Java :: resize image icon to fit jlabel 
Java :: min max heap java 
Java :: intent android 
Java :: icon label java 
Java :: find length of array java 
Java :: get device token firebase 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =