getGraphics的图形对象和paintComponent之间的区别 [英] Difference between Graphics object of getGraphics and paintComponent
问题描述
如果我使用JPanel,getGraphics返回的Graphics对象和paintComponent方法中传递的Graphics对象有什么不同?
getGraphics
- 可以是
null
- 是上一次绘制过程的快照
- 在下一个绘图周期中丢失
您应该避免使用 getGraphics
什么是过去 paintComponent
方法。
从理论上讲,它们之间没有区别,但如果你想为了在重绘之间生存,你应该使用 paintComponent
/ strong>
没有示例代码,我猜...但是...
JPanel ,它充当绘制图像的主要画布, ,当设置时,一个 Rectangle
作为选择。
它使用第二个类作为 MouseListener
实际上决定要绘制什么
import java.awt.AlphaComposite ;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MouseSelection {
public static void main(String [] args){
new MouseSelection();
$ b public MouseSelection(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
}
ImagePane imgPane = new ImagePane();
new MouseHandler(imgPane);
JFrame frame = new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(imgPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame。 setVisible(true);
}
});
公共类MouseHandler扩展MouseAdapter {
私有ImagePane imgPane;
私人点clickPoint;
public MouseHandler(ImagePane imgPane){
this.imgPane = imgPane;
imgPane.addMouseMotionListener(this);
imgPane.addMouseListener(this);
}
@Override
public void mousePressed(MouseEvent e){
imgPane.clearSelection();
clickPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e){
clickPoint = null;
}
@Override
public void mouseDragged(MouseEvent e){
if(clickPoint!= null){
Point dragPoint = e.getPoint );
int x = Math.min(clickPoint.x,dragPoint.x);
int y = Math.min(clickPoint.y,dragPoint.y);
int width = Math.max(clickPoint.x,dragPoint.x) - x;
int height = Math.max(clickPoint.y,dragPoint.y) - y;
imgPane.setSelection(new Rectangle(x,y,width,height));
public class ImagePane extends JPanel {
private BufferedImage img;
专用矩形选择;
$ b $ public ImagePane(){
try {
img = ImageIO.read(new File(C:\\\\\\\\\\\\\\') .JPG));
} catch(IOException ex){
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize(){
return img == null?新维(200,200):新维(img.getWidth(),img.getHeight());
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g.create();
if(img!= null){
int x =(getWidth() - img.getWidth())/ 2;
int y =(getHeight() - img.getHeight())/ 2;
g2d.drawImage(img,x,y,this);
if(selection!= null){
Color color = UIManager.getColor(Table.selectionBackground);
g2d.setColor(color);
Composite comp = g2d.getComposite();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
g2d.fill(选择);
g2d.setComposite(comp);
g2d.draw(选择);
}
g2d.dispose();
}
保护无效clearSelection(){
选择=空;
repaint();
}
保护无效setSelection(矩形矩形){
选择=矩形;
repaint();
}
}
}
If I'm working with a JPanel, what's the difference between the Graphics object returned by getGraphics, and the Graphics object that is passed in paintComponent method?
getGraphics
- Can be
null
- Is a "snap shot" of the last paint process
- Anything painted to it will be lost on the next paint cycle
You should avoid using getGraphics
and simply use what is past to the paintComponent
method.
In theory, there is no difference between them, but if you want what you have painted to survive between repaints, then you should be using paintComponent
Updated with example
Without an example code to go by I'm guessing...but...
Basically, this has a JPanel
that acts as the primary "canvas" that draws an image and, when set, a Rectangle
which acts as the selection.
It uses a second class as the MouseListener
to actually make the decisions about what to paint
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MouseSelection {
public static void main(String[] args) {
new MouseSelection();
}
public MouseSelection() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
ImagePane imgPane = new ImagePane();
new MouseHandler(imgPane);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(imgPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MouseHandler extends MouseAdapter {
private ImagePane imgPane;
private Point clickPoint;
public MouseHandler(ImagePane imgPane) {
this.imgPane = imgPane;
imgPane.addMouseMotionListener(this);
imgPane.addMouseListener(this);
}
@Override
public void mousePressed(MouseEvent e) {
imgPane.clearSelection();
clickPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
clickPoint = null;
}
@Override
public void mouseDragged(MouseEvent e) {
if (clickPoint != null) {
Point dragPoint = e.getPoint();
int x = Math.min(clickPoint.x, dragPoint.x);
int y = Math.min(clickPoint.y, dragPoint.y);
int width = Math.max(clickPoint.x, dragPoint.x) - x;
int height = Math.max(clickPoint.y, dragPoint.y) - y;
imgPane.setSelection(new Rectangle(x, y, width, height));
}
}
}
public class ImagePane extends JPanel {
private BufferedImage img;
private Rectangle selection;
public ImagePane() {
try {
img = ImageIO.read(new File("C:\\hold\\thumbnails\\issue459.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (img != null) {
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight() - img.getHeight()) / 2;
g2d.drawImage(img, x, y, this);
}
if (selection != null) {
Color color = UIManager.getColor("Table.selectionBackground");
g2d.setColor(color);
Composite comp = g2d.getComposite();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
g2d.fill(selection);
g2d.setComposite(comp);
g2d.draw(selection);
}
g2d.dispose();
}
protected void clearSelection() {
selection = null;
repaint();
}
protected void setSelection(Rectangle rectangle) {
selection = rectangle;
repaint();
}
}
}
这篇关于getGraphics的图形对象和paintComponent之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!