package bdzhidao;
創(chuàng)新互聯(lián)科技有限公司專業(yè)互聯(lián)網(wǎng)基礎(chǔ)服務(wù)商,為您提供四川電信科技城機(jī)房,高防服務(wù)器租用,成都IDC機(jī)房托管,成都主機(jī)托管等互聯(lián)網(wǎng)服務(wù)。
import java.awt.*;
import javax.swing.*;
public class Square extends JFrame{
public static void main(String[] args){
JFrame frame=new JFrame("可變正方形");
Spanel jp=new Spanel();
frame.add(jp);
frame.setSize(1366,768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocation(0,0);
frame.setVisible(true);
}
}
class Spanel extends JPanel{
int x=640;
int Width=8;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(new Color(10,200,40));
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
g.drawRect(x-=20,x-=20,Width+=10,Width+=10);
if(x=0){
System.exit(0);//當(dāng)正方形移到屏幕里面時關(guān)閉程序,如果沒有該語句則最后屏幕會變成綠色
}
repaint();
}
}
//縮小就只需要把相關(guān)數(shù)據(jù)更改一下就OK了,希望對你有幫助!
//其實就是利用線程不斷重新畫圖
給你一個簡單的例子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);// 關(guān)閉方式
setLayout(null);
JButton b1 = new JButton("放大");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(size = 46){
size += 2;
}else{
JOptionPane.showMessageDialog(null, "字體達(dá)到最大值");
}
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, "字體達(dá)到最小值");
}
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實現(xià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");
//?設(shè)置文件類型
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范圍內(nèi)移動認(rèn)為是水平移動
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);
}
}
}
}
放大圖像不會導(dǎo)致失真,而縮小圖像將不可避免的失真。Java中也同樣是這樣。但java提供了4個縮放的微調(diào)選項。image.SCALE_SMOOTH //平滑優(yōu)先image.SCALE_FAST//速度優(yōu)先image.SCALE_AREA_AVERAGING //區(qū)域均值image.SCALE_REPLICATE //像素復(fù)制型縮放image.SCALE_DEFAULT //默認(rèn)縮放模式調(diào)用方法Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);得到一張縮放后的新圖。怎么用java代碼放大或縮小圖片不失真。
vc6.0設(shè)置如下:點菜單 工具-》選擇,然后彈出一個對話框,找到“格式”選項卡,里面可以設(shè)置字體,背景色和前景色。截圖如下:
我只實現(xiàn)了放大縮小功能,旋轉(zhuǎn)還有點問題package com.lzw;import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*
* @功能:實現(xiàn)圖片的放大縮小與旋轉(zhuǎn)功能
* @日期:2011.6.29
*/
public class OperationOnPic extends JFrame{
private JPanel imageJpanel;
private JPanel southJpanel;
private JButton rorateButton;
private Mycanvas canvas ;
private int imgWidth, imgHeight;
private JSlider jSlide; //滑動塊組建
// private JScrollPane jScrollPane ;
Image img;
private static int num=0;
private static int newWidth=0;
private static int newHeigth=0;
public static boolean isroate=false;
public OperationOnPic(){
setTitle("圖片操作");
//設(shè)置窗體大小
setSize(800, 600);
//設(shè)置窗體顯示在中央位置
setLocationRelativeTo(null);
//URL imgUrl = OperationOnPic.class.getResource("2.jpg"); //獲得圖片路徑
//img = Toolkit.getDefaultToolkit().getImage(imgUrl);// 獲取圖片資源
ImageIcon image=new ImageIcon(OperationOnPic.class.getResource("2.jpg"));
img=image.getImage();
canvas=new Mycanvas();
//調(diào)用布局方法
init();
//設(shè)置窗體可見
setVisible(true);
}
public void init(){
setDefaultCloseOperation(this.EXIT_ON_CLOSE); //設(shè)置窗體可關(guān)閉
imageJpanel=new JPanel();
southJpanel=new JPanel();
imageJpanel.setLayout(new BorderLayout());
imageJpanel.add(canvas,BorderLayout.CENTER);
imageJpanel.add(getJSlider(),BorderLayout.SOUTH);
rorateButton=new JButton("圖片旋轉(zhuǎn)");
rorateButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
num+=5;
isroate=true;
canvas.repaint();
isroate=false;
}
});
southJpanel.add(rorateButton); //將按鈕添加到面板上
this.getContentPane().add(imageJpanel,BorderLayout.CENTER);
this.getContentPane().add(southJpanel,BorderLayout.SOUTH);
}
public JSlider getJSlider(){
if(jSlide==null){
jSlide=new JSlider(); //實例化一個滑動塊對象
jSlide.setMaximum(500); //設(shè)置滑動塊的最大取值
jSlide.setMinimum(1); //設(shè)置滑動塊的最小取值
jSlide.setValue(50); //設(shè)置滑動塊當(dāng)前值
jSlide.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
canvas.repaint(); //重新繪制圖像
}
});
}
return jSlide;
}
public static void main(String[] args) {
new OperationOnPic();
}
class Mycanvas extends Canvas{
public void paint(final Graphics g){ //重寫paint
if(isroate==false){
imgWidth=img.getWidth(this);
imgHeight=img.getHeight(this);
float value=jSlide.getValue(); //取得滑動塊的值
newWidth=(int)(imgWidth*value/100);
newHeigth=(int)(imgHeight*value/100);
g.drawImage(img,0,0,newWidth,newHeigth,this);
g.dispose();
}else{
num+=5;
Graphics2D g2=(Graphics2D)g;
g2.rotate(Math.toRadians(num));
g2.drawImage(img,0,0,400,400,this);
g.dispose();
}
}
}