求java编程实现一个正方形放大缩小的代码
package bdzhidao;
创新互联科技有限公司专业互联网基础服务商,为您提供四川电信科技城机房,高防服务器租用,成都IDC机房托管,成都主机托管等互联网服务。
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);//当正方形移到屏幕里面时关闭程序,如果没有该语句则最后屏幕会变成绿色
}
repaint();
}
}
//缩小就只需要把相关数据更改一下就OK了,希望对你有帮助!
//其实就是利用线程不断重新画图
求Java编一个放大缩小一个字符串的代码?
给你一个简单的例子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实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:
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);
}
}
}
}
怎么用java代码放大或缩小图片不失真。
放大图像不会导致失真,而缩小图像将不可避免的失真。Java中也同样是这样。但java提供了4个缩放的微调选项。image.SCALE_SMOOTH //平滑优先image.SCALE_FAST//速度优先image.SCALE_AREA_AVERAGING //区域均值image.SCALE_REPLICATE //像素复制型缩放image.SCALE_DEFAULT //默认缩放模式调用方法Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);得到一张缩放后的新图。怎么用java代码放大或缩小图片不失真。
怎么讲编程软件上的代码放大,比如说将VC++6.0或是java或是VC2008之类的软件上面的代码放大。求高手啊!
vc6.0设置如下:点菜单 工具-》选择,然后弹出一个对话框,找到“格式”选项卡,里面可以设置字体,背景色和前景色。截图如下:
java小程序图片查看器代码,实现放大、缩小、旋转功能
我只实现了放大缩小功能,旋转还有点问题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;
/*
* @功能:实现图片的放大缩小与旋转功能
* @日期: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("图片操作");
//设置窗体大小
setSize(800, 600);
//设置窗体显示在中央位置
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();
//调用布局方法
init();
//设置窗体可见
setVisible(true);
}
public void init(){
setDefaultCloseOperation(this.EXIT_ON_CLOSE); //设置窗体可关闭
imageJpanel=new JPanel();
southJpanel=new JPanel();
imageJpanel.setLayout(new BorderLayout());
imageJpanel.add(canvas,BorderLayout.CENTER);
imageJpanel.add(getJSlider(),BorderLayout.SOUTH);
rorateButton=new JButton("图片旋转");
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); //设置滑动块的最大取值
jSlide.setMinimum(1); //设置滑动块的最小取值
jSlide.setValue(50); //设置滑动块当前值
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();
}
}
}
网站名称:java代码放大 编程放大代码和缩小代码
链接分享:http://scgulin.cn/article/hhcsss.html