java图形处理代码,Java图形

如何用Java实现图形的放大和缩小?

要用Java实现图形的放大和缩小,可以使用以下代码:

成都创新互联是一家专业提供班玛企业网站建设,专注与网站建设、成都网站设计H5开发、小程序制作等业务。10年已为班玛众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

import java.awt.*;import java.awt.event.*;import javax.swing.*;//实现矩形在规定时间间隔里循环放大缩小;

public class Test02 extends JFrame implements Runnable{ static int w = 0,h = 0; //w:矩形宽度,h:矩形高度 ;

boolean flag = false;//false:放大,true:缩小 public Test02(){    this.setSize (500,500);  this.setVisible (true);  this.setTitle (w+","+h);  this.setLocationRelativeTo (this);  this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); }  public void paint(Graphics g){  int width = getWidth();

//窗口宽度  int height = getWidth();  //窗口高度  g.setColor (Color.WHITE); //设置画笔颜色  g.fillRect (0,0,width,height);  //填充窗口  int x = (width-w)/2; //x:矩形左上角横坐标  int y = (height-h)/2; //y:矩形左上角纵坐标  g.setColor (Color.BLUE); //同上...  g.drawRect (x,y,w,h);

//画矩形,实心矩形为fillRect(....)  g.setColor (Color.RED); //同上...  g.drawOval (x,y,w,h);     //画椭圆  setTitle (w+","+h);     //以矩形宽和高来设置窗口标题 } public void setSize(){  float n = getWidth()/getWidth();  if(w==0||h==0)   flag = false;

if(w==getWidth()||h==getWidth())   flag = true;  if(!flag){   w+=1;   h+=n;  }  if(flag){   w-=1;   h-=n;  }   } public void run(){  while(true){   try{    Thread.sleep (5);//间隔   }catch(InterruptedException ie){}      setSize();   repaint();  } } public static void main(String[] args){  Test02 t = new Test02();  Thread th = new Thread(t);  th.start(); }}

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

解析下这个生成图片的Java代码

1.调用"images"文件夹里的文件放在ImageIcon里显示2.在windows下面显示正常Java在图形处理时调用了本地的图形处理库。在利用Java作图形处理(比如:图片缩放,图片签名,生成报表)时,如果运行在windows上不会出问题。如果将程序移植到Linux/Unix上的时候有可能出现图形不能显示的错误。3.提示信息:"Can'tconnecttoX11windowserver"这是由于Linux的图形处理需要一个XServer服务器。(关于Linux的图形处理模式参考Tomcat的修改如下:打开%Tomcat_home%/bin/catalina.sh文件在文件的86%处,启动Tomcat的脚本中加上:-Djava.awt.headless=true/参考:"$_RUNJAVA"$JAVA_OPTS$CATALINA_OPTS/-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS"-classpath"$CLASSPATH"/-Dcatalina.base="$CATALINA_BASE"/-Dcatalina.home="$CATALINA_HOME"/-Djava.io.tmpdir="$CATALINA_TMPDIR"/-Djava.awt.headless=true/org.apache.catalina.startup.Bootstrap"$@"start/"$CATALINA_BASE"/logs/catalina.out21if[!-z"$CATALINA_PID"];thenecho$!$CATALINA_PIDfifielif["$1"="stop"];then保存后,重新启动就可以处理图形了。

求一个简单的java代码:(图形界面)

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class vv extends JDialog {

private static final long serialVersionUID = 1L;

private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);

private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);

private JTextField t_Id = new JTextField(10);

private JPasswordField t_pw = new JPasswordField(10);

private JButton btnLogin;

private JButton btnClose;

public vv() {

super();

setResizable(false);

getContentPane().setBackground(new Color(225, 225, 225));

getContentPane().setLayout(null);

initialize();

}

protected void initialize() {

setTitle("系统登录");

l_Id.setBounds(48, 43, 53, 25);

t_Id.setBounds(110, 43, 150, 25);

l_pw.setBounds(48, 93, 53, 25);

t_pw.setBounds(110, 93, 150, 25);

getContentPane().add(l_Id);

getContentPane().add(l_pw);

getContentPane().add(t_Id);

getContentPane().add(t_pw);

btnLogin = new JButton();

btnLogin.setText("登 录");

btnLogin.setBounds(70, 142, 85, 28);

btnLogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

addBtnLoginActionListener();

}

});

getContentPane().add(btnLogin);

btnClose = new JButton();

btnClose.setText("关 闭");

btnClose.setBounds(175, 142, 85, 28);

btnClose.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dispose();

System.exit(-1);

}

});

getContentPane().add(btnClose);

}

private void addBtnLoginActionListener() {

String user = t_Id.getText();

String password = new String(t_pw.getPassword());

if (user.equals("")) {

JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",

JOptionPane.WARNING_MESSAGE);

return;

}

if (password.equals("")) {

JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",

JOptionPane.WARNING_MESSAGE);

return;

}

String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"

+ password + "'";

boolean success = false;

// TODO:数据校验 success = executeQuery(sql);

if (success) {

// TODO: 如果数据校验成功 显示主界面 并关闭登录界面

JOptionPane.showMessageDialog(this, "成功登录", "提示",

JOptionPane.INFORMATION_MESSAGE);

this.dispose();

} else {

JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",

JOptionPane.WARNING_MESSAGE);

t_pw.requestFocus(); // 密码框选中

}

}

public Dimension getPreferredSize() {

return new Dimension(320, 170);

}

public void show() {

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension screen = tk.getScreenSize();

Dimension d = getSize();

this.setLocation((screen.width - d.width) / 2,

(screen.height - d.height) / 2);

// 输入密码后回车相当于点击了登录按钮

getRootPane().setDefaultButton(btnLogin);

t_pw.requestFocus();

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setSize(300, 220);

super.show();

}

public static void main(String[] args) {

vv loginFrame = new vv();

loginFrame.setVisible(true);

}

}

希望对你有帮助

java代码怎么实现计算图像二值连通区域的质心

一:几何距(Geometric Moments)知识与质心寻找原理

1. Image Moments是图像处理中非常有用的算法,可以用来计算区域图像的质心,方向等几何特性,同时Mpq的高阶具有旋转不变性,可以用来实现图像比较分类,正是因为Moments有这些特性,很多手绘油画效果也会基于该算法来模拟实现。它的数学表达为:

它的低阶M00,M01, M10可以用来计算质心,中心化以后M11,M02,M20可以用来计算区域的方向/角度

2. 什么是质心

就是通过该点,区域达到一种质量上的平衡状态,可能物理学上讲的比较多,简单点的说就是规则几何物体的中心,不规则的可以通过挂绳子的方法来寻找。

二:算法流程

1. 输入图像转换为二值图像

2. 通过连通组件标记算法找到所有的连通区域,并分别标记

3. 对每个连通区域运用计算几何距算法得到质心

4. 用不同颜色绘制连通区域与质心,输出处理后图像

三:算法效果

左边为原图, 右边蓝色为连通组件标记算法处理以后结果,白色点为质心

四:关键代码解析

1. 计算几何距算法代码

doublem00 = moments(pixels, width, height, 0, 0);

doublexCr = moments(pixels, width, height, 1, 0) / m00;// row

doubleyCr = moments(pixels, width, height, 0, 1) / m00;// column

return new double[]{xCr, yCr};

新手学习使用Java,尝试着做一个项目使用Java做一个视频图像的处理。

Java图像处理技巧四则

下面代码中用到的sourceImage是一个已经存在的Image对象

图像剪切

对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:

//import java.awt.*;

//import java.awt.image.*;

Image croppedImage;

ImageFilter cropFilter;

CropFilter =new CropImageFilter(25,30,75,75); //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API

CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));

如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProducer对象。

图像缩放

对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一个100X100的图像

Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。

//其它情况请参考API

灰度变换

下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个颜色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知道具体算法是什么样的,效果和上述不同。

/* GrayFilter.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class GrayFilter extends RGBImageFilter {

int modelStyle;

public GrayFilter() {

modelStyle=GrayModel.CS_MAX;

canFilterIndexColorModel=true;

}

public GrayFilter(int style) {

modelStyle=style;

canFilterIndexColorModel=true;

}

public void setColorModel(ColorModel cm) {

if (modelStyle==GrayModel

else if (modelStyle==GrayModel

}

public int filterRGB(int x,int y,int pixel) {

return pixel;

}

}

/* GrayModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class GrayModel extends ColorModel {

public static final int CS_MAX=0;

public static final int CS_FLOAT=1;

ColorModel sourceModel;

int modelStyle;

public GrayModel(ColorModel sourceModel) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=0;

}

public GrayModel(ColorModel sourceModel,int style) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

modelStyle=style;

}

public void setGrayStyle(int style) {

modelStyle=style;

}

protected int getGrayLevel(int pixel) {

if (modelStyle==CS_MAX) {

return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));

}

else if (modelStyle==CS_FLOAT){

return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);

}

else {

return 0;

}

}

public int getAlpha(int pixel) {

return sourceModel.getAlpha(pixel);

}

public int getRed(int pixel) {

return getGrayLevel(pixel);

}

public int getGreen(int pixel) {

return getGrayLevel(pixel);

}

public int getBlue(int pixel) {

return getGrayLevel(pixel);

}

public int getRGB(int pixel) {

int gray=getGrayLevel(pixel);

return (getAlpha(pixel)24)+(gray16)+(gray8)+gray;

}

}

如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。

色彩变换

根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class ReverseColorModel extends ColorModel {

ColorModel sourceModel;

public ReverseColorModel(ColorModel sourceModel) {

super(sourceModel.getPixelSize());

this.sourceModel=sourceModel;

}

public int getAlpha(int pixel) {

return sourceModel.getAlpha(pixel);

}

public int getRed(int pixel) {

return ~sourceModel.getRed(pixel);

}

public int getGreen(int pixel) {

return ~sourceModel.getGreen(pixel);

}

public int getBlue(int pixel) {

return ~sourceModel.getBlue(pixel);

}

public int getRGB(int pixel) {

return (getAlpha(pixel)24)+(getRed(pixel)16)+(getGreen(pixel)8)+getBlue(pixel);

}

}

/* ReverseColorModel.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.image.*;

public class ReverseFilter extends RGBImageFilter {

public ReverseFilter() {

canFilterIndexColorModel=true;

}

public void setColorModel(ColorModel cm) {

substituteColorModel(cm,new ReverseColorModel(cm));

}

public int filterRGB(int x,int y,int pixel) {

return pixel;

}

}

要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。

下面是上面的效果的一个总的演示程序。

/*GrayImage.java*/

/*@author:cherami */

/*email:cherami@163.net*/

import java.awt.*;

import java.awt.image.*;

import javax.swing.*;

import java.awt.color.*;

public class GrayImage extends JFrame{

Image source,gray,gray3,clip,bigimg;

BufferedImage bimg,gray2;

GrayFilter filter,filter2;

ImageIcon ii;

ImageFilter cropFilter;

int iw,ih;

public GrayImage() {

ii=new ImageIcon(\"images/11.gif\");

source=ii.getImage();

iw=source.getWidth(this);

ih=source.getHeight(this);

filter=new GrayFilter();

filter2=new GrayFilter(GrayModel.CS_FLOAT);

gray=createImage(new FilteredImageSource(source.getSource(),filter));

gray3=createImage(new FilteredImageSource(source.getSource(),filter2));

cropFilter=new CropImageFilter(5,5,iw-5,ih-5);

clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));

bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);

MediaTracker mt=new MediaTracker(this);

mt.addImage(gray,0);

try {

mt.waitForAll();

} catch (Exception e) {

}

java程序,用图形实现乘除运算

简易的计算器代码如下,请参照:

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**

* 计算器类

* @author Administrator

*

*/

public class Calculator extends JFrame implements ActionListener {

private static final long serialVersionUID = 3868243398506940702L;

// 文本框

private JTextField result;

// 按钮数组

private JButton[] buttons;

// 按钮文本

private final String[] characters = { "7", "8", "9", "/", "4", "5", "6",

"*", "1", "2", "3", "-", "0", ".", "=", "+" };

// 是否为第一个输入的数字

private boolean isFirstDigit = true;

// 运算结果

private double resultNum = 0.0;

// 运算符

private String operator = "=";

public Calculator(String title) {

// 设置标题栏

super(title);

// 初始化各组件

init();

// 注册各组件监听器

registerListener();

// 显示窗体

setVisible(true);

}

/**

* 初始化各组件

*/

private void init() {

// 常用属性初始化

setSize(220, 200);

setResizable(false);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

/* 文本框对象初始化 */

result = new JTextField("0");

// 文本右对齐

result.setHorizontalAlignment(JTextField.RIGHT);

// 设置是否可编辑

result.setEditable(false);

/* 按钮初始化 */

buttons = new JButton[characters.length];

for (int i = 0; i  buttons.length; i++) {

buttons[i] = new JButton(characters[i]);

buttons[i].setFocusable(false); // 不允许按钮定位焦点

}

/* 将文本框与按钮添加到窗体中 */

add(result, BorderLayout.NORTH);

JPanel pnl = new JPanel(new GridLayout(4, 4, 5, 5));

for (JButton jButton : buttons) {

pnl.add(jButton);

}

add(pnl);

this.getContentPane().setFocusable(true);

}

/**

* 注册监听器

*/

private void registerListener() {

for (JButton jButton : buttons) {

jButton.addActionListener(this);

}

// 注册键盘事件

this.getContentPane().addKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

String text = String.valueOf(e.getKeyChar());

if (Character.isDigit(text.charAt(0)) || ".".equals(text)) { // 数字或小数点

handleNumber(text);

} else if ("+-*/=".indexOf(text) != -1) { // 运算符

handleOperator(text);

} else if (e.getKeyCode() == 8) { // 退格键

String tmp = result.getText().trim();

if (tmp.length() == 1) {

result.setText("0");

isFirstDigit = true;

} else {

result.setText(tmp.substring(0, tmp.length() - 1));

}

}

}

});

}

@Override

public void actionPerformed(ActionEvent e) {

JButton btn = (JButton) e.getSource();

String text = btn.getText().trim();

if (Character.isDigit(text.charAt(0)) || ".".equals(text)) { // 处理数字和小数点

handleNumber(text);

} else { // 处理运算符

handleOperator(text);

}

}

/**

* 处理数字和小数点

* @param text

*/

private void handleNumber(String text) {

if (isFirstDigit) { // 第一次输入

if (".".equals(text)) {

this.result.setText("0.");

} else {

this.result.setText(text);

}

} else if ("0".equals(text)  "0".equals(this.result.getText())) {

isFirstDigit = true;

return;

} else if (".".equals(text)  this.result.getText().indexOf(".") == -1) {

this.result.setText(this.result.getText() + ".");

} else if (!".".equals(text)) {

this.result.setText(this.result.getText() + text);

}

isFirstDigit = false;

}

/**

* 处理运算符

* @param text

*/

private void handleOperator(String text) {

switch (operator) { // 处理各项运算  适用于JDK1.7版本的

case "+":

resultNum += Double.parseDouble(this.result.getText());

break;

case "-":

resultNum -= Double.parseDouble(this.result.getText());

break;

case "*":

resultNum *= Double.parseDouble(this.result.getText());

break;

case "/":

resultNum /= Double.parseDouble(this.result.getText());

break;

case "=":

resultNum = Double.parseDouble(this.result.getText());

break;

}

// 将文本框的值修改为运算结果

this.result.setText(String.valueOf(resultNum));

// 将点击的运算符放入operator保存

operator = text;

// 下一个数字第一次点击

isFirstDigit = true;

}

public static void main(String[] args) {

 new Calculator("My Calculator");

 }

}

运行结果如下:

网站标题:java图形处理代码,Java图形
文章URL:https://www.cdcxhl.com/article0/hshpio.html

成都网站建设公司_创新互联,为您提供虚拟主机Google移动网站建设软件开发云服务器手机网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设