Java by API/java.awt/Image
Содержание
Image: getHeight(ImageObserver observer)
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
Main() {
add(new ContentPanel());
setSize(500, 300);
}
public static void main(String[] args) {
Main jrframe = new Main();
jrframe.setVisible(true);
}
}
class ContentPanel extends JPanel {
Image bgimage = null;
ContentPanel() {
MediaTracker mt = new MediaTracker(this);
bgimage = Toolkit.getDefaultToolkit().getImage("a.jpg");
mt.addImage(bgimage, 0);
try {
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int imwidth = bgimage.getWidth(null);
int imheight = bgimage.getHeight(null);
g.drawImage(bgimage, 1, 1, null);
}
}
Image: getScaledInstance(int width, int height, int hints)
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame implements ActionListener {
Image img;
JButton getPictureButton = new JButton("Get Picture");
public static void main(String[] args) {
new Main();
}
public Main() {
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel picPanel = new PicturePanel();
this.add(picPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
getPictureButton.addActionListener(this);
buttonPanel.add(getPictureButton);
this.add(buttonPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String file = "a.png";
if (file != null) {
Toolkit kit = Toolkit.getDefaultToolkit();
img = kit.getImage(file);
img = img.getScaledInstance(300, -1, Image.SCALE_SMOOTH);
this.repaint();
}
}
class PicturePanel extends JPanel {
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
}
Image: getWidth(ImageObserver observer)
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
Main() {
add(new ContentPanel());
setSize(500, 300);
}
public static void main(String[] args) {
Main jrframe = new Main();
jrframe.setVisible(true);
}
}
class ContentPanel extends JPanel {
Image bgimage = null;
ContentPanel() {
MediaTracker mt = new MediaTracker(this);
bgimage = Toolkit.getDefaultToolkit().getImage("a.jpg");
mt.addImage(bgimage, 0);
try {
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int imwidth = bgimage.getWidth(null);
int imheight = bgimage.getHeight(null);
g.drawImage(bgimage, 1, 1, null);
}
}
Image.SCALE_DEFAULT
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.RepaintManager;
public class Main extends JComponent implements MouseMotionListener {
static int imageWidth = 60, imageHeight = 60;
int imageX, imageY;
Image image;
public Main(Image i) {
image = i;
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e) {
imageX = e.getX();
imageY = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image, imageX, imageY, this);
}
public static void main(String[] args) {
String imageFile = "A.jpg";
RepaintManager.currentManager(null).setDoubleBufferingEnabled(false);
Image image = Toolkit.getDefaultToolkit().getImage(Main.class.getResource(imageFile));
image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_DEFAULT);
JFrame frame = new JFrame("DragImage");
frame.getContentPane().add(new Main(image));
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Image.SCALE_SMOOTH
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame implements ActionListener {
Image img;
JButton getPictureButton = new JButton("Get Picture");
public static void main(String[] args) {
new Main();
}
public Main() {
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel picPanel = new PicturePanel();
this.add(picPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
getPictureButton.addActionListener(this);
buttonPanel.add(getPictureButton);
this.add(buttonPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String file = "a.png";
if (file != null) {
Toolkit kit = Toolkit.getDefaultToolkit();
img = kit.getImage(file);
img = img.getScaledInstance(300, -1, Image.SCALE_SMOOTH);
this.repaint();
}
}
class PicturePanel extends JPanel {
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
}