java實現(xiàn)圖形的放大和縮小,其實就是在畫圖時,改變圖片的長和寬。以下代碼參考一下:
通榆網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選成都創(chuàng)新互聯(lián)公司。
import?java.awt.Graphics;
import?java.awt.MouseInfo;
import?java.awt.Point;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseListener;
import?java.io.File;
import?javax.swing.ImageIcon;
import?javax.swing.JButton;
import?javax.swing.JFileChooser;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.filechooser.FileNameExtensionFilter;
public?class?App?extends?JFrame?implements?MouseListener,?ActionListener?{
int?x?=?0;
int?y?=?0;
File[]?selectedFiles?=?null;
int?fileIndex?=?0;
int?width?=?200;
int?height?=?200;
public?App()?{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400,?300);
setResizable(false);
getContentPane().setLayout(null);
JPanel?panel?=?new?ImagePanel();
panel.setBounds(12,?40,?370,?218);
getContentPane().add(panel);
addMouseListener(this);
JButton?btnBrowse?=?new?JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12,?9,?91,?21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public?static?void?main(String[]?args)?{
new?App();
}
public?void?actionPerformed(ActionEvent?e)?{
JFileChooser?chooser?=?new?JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter?filter?=?new?FileNameExtensionFilter(
"JPG??GIF?Images",?"jpg",?"gif");
//?設置文件類型
chooser.setFileFilter(filter);
//?打開選擇器面板
int?returnVal?=?chooser.showOpenDialog(this);
if?(returnVal?==?JFileChooser.APPROVE_OPTION)?{
selectedFiles?=?chooser.getSelectedFiles();
repaint();
}
}
public?void?mouseClicked(MouseEvent?e)?{
}
public?void?mouseEntered(MouseEvent?e)?{
}
public?void?mouseExited(MouseEvent?e)?{
}
public?void?mousePressed(MouseEvent?e)?{
Point?point?=?MouseInfo.getPointerInfo().getLocation();
x?=?point.x;
y?=?point.y;
}
public?void?mouseReleased(MouseEvent?e)?{
Point?point?=?MouseInfo.getPointerInfo().getLocation();
int?thisX?=?point.x;
int?thisY?=?point.y;
System.out.println("thisX="?+?thisX?+?"??"?+?"thisY="?+?thisY);
if?((y?-?thisY??20??y?-?thisY??0)
||?(y?-?thisY??0??y?-?thisY??-20))?{
//?Y?在20范圍內移動認為是水平移動
if?(x??thisX)?{
//?right
if?(selectedFiles?!=?null
?fileIndex??selectedFiles.length?-?1)?{
fileIndex++;
}
}?else?{
//?left
if?(selectedFiles?!=?null??fileIndex??0)?{
fileIndex--;
}
}
}?else?{
if?(x??thisX)?{
//?右下
width?+=?20;
height?+=?20;
}?else?{
//?左上
width?-=?20;
height?-=?20;
}
}
repaint();
}
class?ImagePanel?extends?JPanel?{
public?void?paint(Graphics?g)?{
super.paint(g);
if?(selectedFiles?!=?null)?{
ImageIcon?icon?=?new?ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(),?0,?0,?width,?height,?this);
}
}
}
}
選擇Window-Preferences,然后選擇General-Appearance-Colors and Fonts-Java-Java Editor Text Font
然后選擇Change按鈕就可以改變字體大小了.
如果是swing的話
JLabel jl = new JLabel("標簽");
jl.setBounds(10, 10, 10, 10);這是控制標簽位置;
控制文本的位置,你可以通過在“”里邊加空格控制
jl.setFont(new Font("宋體", Font.BOLD, 20));
這是控制文本字體,那個20就是大小選擇,可以改變的
vc6.0設置如下:點菜單 工具-》選擇,然后彈出一個對話框,找到“格式”選項卡,里面可以設置字體,背景色和前景色。截圖如下:
給你一個簡單的例子import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class ImageDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel panel;
private int size = 14;
public ImageDemo() {
super("test");
setVisible(true);// 顯示
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();// 屏幕大小
setBounds((d.width - 300) / 2, (d.height - 200) / 2, 300, 200);// 大小,定位
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 關閉方式
setLayout(null);
JButton b1 = new JButton("放大");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(size = 46){
size += 2;
}else{
JOptionPane.showMessageDialog(null, "字體達到最大值");
}
panel.repaint();
}
});
b1.setBounds(75, 2, 150, 30);
JButton b2 = new JButton("縮小");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(size = 12){
size -= 2;
}else{
JOptionPane.showMessageDialog(null, "字體達到最小值");
}
panel.repaint();
}
});
b2.setBounds(75, 90, 150, 30);
setPanel();
panel.setBounds(0, 40, 300, 50);
add(b1);
add(panel);
add(b2);
validate();
JPanel pane = (JPanel) this.getContentPane();
pane.updateUI();
}
private void setPanel() {
panel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setFont(new Font("", Font.BOLD, size));
String intNumber = "這是一個簡單的JFrame";
g2D.drawString(intNumber, 70, 32);
}
};
}
public static void main(String[] args) {
new ImageDemo();
}
}
放大圖像不會導致失真,而縮小圖像將不可避免的失真。Java中也同樣是這樣。但java提供了4個縮放的微調選項。image.SCALE_SMOOTH //平滑優(yōu)先image.SCALE_FAST//速度優(yōu)先image.SCALE_AREA_AVERAGING //區(qū)域均值image.SCALE_REPLICATE //像素復制型縮放image.SCALE_DEFAULT //默認縮放模式調用方法Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);得到一張縮放后的新圖。怎么用java代碼放大或縮小圖片不失真。