当前位置:首页 » 编程语言 » 图的java实现

图的java实现

发布时间: 2022-07-14 09:28:34

1. java 实现 简单画图功能(简单点的)

楼主给你一个我编的,直接保存成pb.java编译运行,就是你要的画图功能
____________________________________________________________________

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;

class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;

Point(int x, int y, Color col, int tool, int boarder)
{
this.x = x;
this.y = y;
this.col = col;
this.tool = tool;
this.boarder = boarder;
}
}

class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener
{
int x = -1, y = -1;
int con = 1;//画笔大小
int Econ = 5;//橡皮大小

int toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
//(0--画笔);(1--橡皮);(2--清除);
//(3--直线);(4--圆);(5--矩形);

Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细
Point cutflag = new Point(-1, -1, c, 6, con);//截断标志

Vector paintInfo = null;//点信息向量组
int n = 1;

FileInputStream picIn = null;
FileOutputStream picOut = null;

ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;

// *工具面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/
Panel toolPanel;
Button eraser, drLine,drCircle,drRect;
Button clear ,pen;
Choice ColChoice,SizeChoice,EraserChoice;
Button colchooser;
Label 颜色,大小B,大小E;
//保存功能
Button openPic,savePic;
FileDialog openPicture,savePicture;

paintboard(String s)
{
super(s);
addMouseMotionListener(this);
addMouseListener(this);

paintInfo = new Vector();

/*各工具按钮及选择项*/
//颜色选择
ColChoice = new Choice();
ColChoice.add("black");
ColChoice.add("red");
ColChoice.add("blue");
ColChoice.add("green");
ColChoice.addItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoice.add("1");
SizeChoice.add("3");
SizeChoice.add("5");
SizeChoice.add("7");
SizeChoice.add("9");
SizeChoice.addItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoice.add("5");
EraserChoice.add("9");
EraserChoice.add("13");
EraserChoice.add("17");
EraserChoice.addItemListener(this);
////////////////////////////////////////////////////
toolPanel = new Panel();

clear = new Button("清除");
eraser = new Button("橡皮");
pen = new Button("画笔");
drLine = new Button("画直线");
drCircle = new Button("画圆形");
drRect = new Button("画矩形");

openPic = new Button("打开图画");
savePic = new Button("保存图画");

colchooser = new Button("显示调色板");

//各组件事件监听
clear.addActionListener(this);
eraser.addActionListener(this);
pen.addActionListener(this);
drLine.addActionListener(this);
drCircle.addActionListener(this);
drRect.addActionListener(this);
openPic.addActionListener(this);
savePic.addActionListener(this);
colchooser.addActionListener(this);

颜色 = new Label("画笔颜色",Label.CENTER);
大小B = new Label("画笔大小",Label.CENTER);
大小E = new Label("橡皮大小",Label.CENTER);
//面板添加组件
toolPanel.add(openPic);
toolPanel.add(savePic);

toolPanel.add(pen);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drRect);

toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(大小B); toolPanel.add(SizeChoice);
toolPanel.add(colchooser);

toolPanel.add(eraser);
toolPanel.add(大小E); toolPanel.add(EraserChoice);

toolPanel.add(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayout.NORTH);

setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load

openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);
openPicture.setVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE);
savePicture.setVisible(false);

openPicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicture.setVisible(false); }
});

savePicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ savePicture.setVisible(false); }
});

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});

}

public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;

Point p1,p2;

n = paintInfo.size();

if(toolFlag==2)
g.clearRect(0,0,getSize().width,getSize().height);//清除

for(int i=0; i<n ;i++){
p1 = (Point)paintInfo.elementAt(i);
p2 = (Point)paintInfo.elementAt(i+1);
size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

g2d.setColor(p1.col);
g2d.setStroke(size);

if(p1.tool==p2.tool)
{
switch(p1.tool)
{
case 0://画笔

Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line1);
break;

case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
break;

case 3://画直线
Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line2);
break;

case 4://画圆
Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(ellipse);
break;

case 5://画矩形
Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;

case 6://截断,跳过
i=i+1;
break;

default :
}//end switch
}//end if
}//end for
}

public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==ColChoice)//预选颜色
{
String name = ColChoice.getSelectedItem();

if(name=="black")
{c = new Color(0,0,0); }
else if(name=="red")
{c = new Color(255,0,0);}
else if(name=="green")
{c = new Color(0,255,0);}
else if(name=="blue")
{c = new Color(0,0,255);}
}
else if(e.getSource()==SizeChoice)//画笔大小
{
String selected = SizeChoice.getSelectedItem();

if(selected=="1")
{
con = 1;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}
else if(selected=="3")
{
con = 3;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}
else if(selected=="5")
{con = 5;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}
else if(selected=="7")
{con = 7;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}
else if(selected=="9")
{con = 9;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}
}
else if(e.getSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoice.getSelectedItem();

if(Esize=="5")
{ Econ = 5*2; }
else if(Esize=="9")
{ Econ = 9*2; }
else if(Esize=="13")
{ Econ = 13*2; }
else if(Esize=="17")
{ Econ = 17*3; }

}

}

public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
break;

case 1://橡皮
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfo.addElement(p1);
repaint();
break;

default :
}
}

public void mouseMoved(MouseEvent e) {}

public void update(Graphics g)
{
paint(g);
}

public void mousePressed(MouseEvent e)
{
Point p2;
switch(toolFlag){
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;

case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;

case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;

default :
}
}

public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfo.addElement(cutflag);
break;

case 1: //eraser
paintInfo.addElement(cutflag);
break;

case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;

case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;

case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;

default:
}
}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void actionPerformed(ActionEvent e)
{

if(e.getSource()==pen)//画笔
{toolFlag = 0;}

if(e.getSource()==eraser)//橡皮
{toolFlag = 1;}

if(e.getSource()==clear)//清除
{
toolFlag = 2;
paintInfo.removeAllElements();
repaint();
}

if(e.getSource()==drLine)//画线
{toolFlag = 3;}

if(e.getSource()==drCircle)//画圆
{toolFlag = 4;}

if(e.getSource()==drRect)//画矩形
{toolFlag = 5;}

if(e.getSource()==colchooser)//调色板
{
Color newColor = JColorChooser.showDialog(this,"调色板",c);
c = newColor;
}

if(e.getSource()==openPic)//打开图画
{

openPicture.setVisible(true);

if(openPicture.getFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();

try{
paintInfo.removeAllElements();
File filein = new File(openPicture.getDirectory(),openPicture.getFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VIn.readObject();
VIn.close();
repaint();
toolFlag = tempflag;

}

catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read file");
}
}

}

if(e.getSource()==savePic)//保存图画
{
savePicture.setVisible(true);
try{
File fileout = new File(savePicture.getDirectory(),savePicture.getFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOut.writeObject(paintInfo);
VOut.close();
}
catch(IOException IOe)
{
System.out.println("can not write object");
}

}
}
}//end paintboard

public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}

2. 怎么用java实现抠图功能

package com.thinkgem.jeesite.moles.file.utils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImageUtils {

/**
* 图片去白色的背景,并裁切
*
* @param image 图片
* @param range 范围 1-255 越大 容错越高 去掉的背景越多
* @return 图片
* @throws Exception 异常
*/
public static byte[] transferAlpha(Image image, InputStream in, int range) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ImageIcon imageIcon = new ImageIcon(image);
BufferedImage bufferedImage = new BufferedImage(imageIcon
.getIconWidth(), imageIcon.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon
.getImageObserver());
int alpha = 0;
int minX = bufferedImage.getWidth();
int minY = bufferedImage.getHeight();
int maxX = 0;
int maxY = 0;

for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage
.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage
.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1);

int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0xff00) >> 8;
int B = (rgb & 0xff);
if (((255 - R) < range) && ((255 - G) < range) && ((255 - B) < range)) { //去除白色背景;
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
} else {
minX = minX <= j2 ? minX : j2;
minY = minY <= j1 ? minY : j1;
maxX = maxX >= j2 ? maxX : j2;
maxY = maxY >= j1 ? maxY : j1;
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
int width = maxX - minX;
int height = maxY - minY;
BufferedImage sub = bufferedImage.getSubimage(minX, minY, width, height);
int degree = getDegree(in);
sub = rotateImage(sub,degree);
ImageIO.write(sub, "png", byteArrayOutputStream);

} catch (Exception e) {
e.printStackTrace();
throw e;
}

return byteArrayOutputStream.toByteArray();
}

/**
* 图片旋转
* @param bufferedimage bufferedimage
* @param degree 旋转的角度
* @return BufferedImage
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
w, h)), degree);
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(rect_des.width, rect_des.height, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.translate((rect_des.width - w) / 2,
(rect_des.height - h) / 2);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}

/**
* 计算旋转后图像的大小
* @param src Rectangle
* @param degree 旋转的角度
* @return Rectangle
*/
public static Rectangle CalcRotatedSize(Rectangle src, int degree) {
if (degree >= 90) {
if(degree / 90 % 2 == 1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
degree = degree % 90;
}

double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(degree) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(degree)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);

int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}

/**
* byte[] ------>BufferedImage
*
* @param byteImage byteImage
* @return return
* @throws IOException IOException
*/
public static BufferedImage ByteToBufferedImage(byte[] byteImage) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(byteImage);
return ImageIO.read(in);
}

/**
* 获取照片信息的旋转角度
* @param inputStream 照片的路径
* @return 角度
*/
public static int getDegree(InputStream inputStream) {
try {
Metadata metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(inputStream),true);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
if ("Orientation".equals(tag.getTagName())) {
return turn(getOrientation(tag.getDescription()));
}
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
return 0;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
return 0;
}

/**
* 获取旋转的角度
* @param orientation orientation
* @return 旋转的角度
*/
public static int turn(int orientation) {
Integer turn = 360;
if (orientation == 0 || orientation == 1) {
turn = 360;
} else if (orientation == 3) {
turn = 180;
} else if (orientation == 6) {
turn = 90;
} else if (orientation == 8) {
turn = 270;
}
return turn;
}

/**
* 根据图片自带的旋转的信息 获取 orientation
* @param orientation orientation
* @return orientation
*/
public static int getOrientation(String orientation) {
int tag = 0;
if ("Top, left side (Horizontal / normal)".equalsIgnoreCase(orientation)) {
tag = 1;
} else if ("Top, right side (Mirror horizontal)".equalsIgnoreCase(orientation)) {
tag = 2;
} else if ("Bottom, right side (Rotate 180)".equalsIgnoreCase(orientation)) {
tag = 3;
} else if ("Bottom, left side (Mirror vertical)".equalsIgnoreCase(orientation)) {
tag = 4;
} else if ("Left side, top (Mirror horizontal and rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 5;
} else if ("Right side, top (Rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 6;
} else if ("Right side, bottom (Mirror horizontal and rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 7;
} else if ("Left side, bottom (Rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 8;
}
return tag;
}
}

3. 怎么编写java程序实现图片的移动(最好有例子)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class DrawTest extends JFrame {
private int x = 50;
private int y = 50;
private Image offScreenImage = null;

@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, 30, 30);
g.setColor(c);

}

public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(500, 500);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, 500, 500);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}

public static void main(String[] args) {
DrawTest d = new DrawTest();

}

public DrawTest() {
init();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
y -= 5;
break;
case KeyEvent.VK_RIGHT:
x += 5;
break;
case KeyEvent.VK_DOWN:
y += 5;
break;
case KeyEvent.VK_LEFT:
x -= 5;
break;
}
}
});
}

public void init() {
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBackground(Color.GREEN);
this.setResizable(false);
this.setBounds(140, 140, 500, 500);
this.setVisible(true);
MyThread mt = new MyThread();
new Thread(mt).start();
}

class MyThread implements Runnable {

public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}
以上

4. javaweb平面图怎么实现

javaweb平面图实现的话需要用java的applet即可。

拓展资料:

(IDEA和Eclipse上均可运行)

设计一个简易地图(类似房间平面图),图上标有多个检测区域(下面已标注要检测哪五个),区域上显示当前的检测数值和状态(红,黄,绿)。

点击检测区域,弹出对话框,对话框中内容包括:

检测数值的实时曲线,可能有多个(实时的意思是要显示检测区域部分的后端数据,以echarts的曲线图形式展示)

同时,也可以用表格的方式,显示上述检测数值,同时表格数据可以导出EXCEL格式文件

检测区域的对话框可以同时显示(非模态方式),也可以只能单一显示(模态方式)(不使用数据库调用,只用表格)

客户端的所有请求(如:温度等的数值记录)都通过服务端的TOMCAT上的java程序实现4.服务端编写所有请求的实现过程,以JSON格式方式返回请求内容

多行显示不同的检测内容,把状态按钮调到每行的前面作为各自的状态(红黄绿三种)。

检测区域:

厨房:一氧化碳浓度,温度

主卧:温度,湿度,噪声

阳台:温度,湿度,风力

餐厅:温度,湿度,光亮

主卫:温度,湿度

5. 用JAVA写图的操作与实现

...
这不是数据结构么
书上应该有算法, 好好看看书吧
图应该是邻接表存储方式, 存储方式弄好了, 就是算法的事情了, 书上都有

不过毕业这么多年了, 除了遍历, 其他都记不清了...

6. 这个图的代码用JAVA实现,怎么做啊

其实是页面的问题:若用java解决的话,用jsp文件。。
最好还是用html文件
然后就是标签的问题:
<div align="center">
<table>
<tr>
<td>作业名称:</td>
<td>期中考试</td>
</tr>
<tr>
<td>作业方式:</td>
<td>个人</td>
</tr>
<tr>
<td>作业内容:</td>
<td>某单位车辆管理</td>
</tr>
</table>
</div>
。。。记得点赞!!

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

java实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:


importjava.awt.Graphics;
importjava.awt.MouseInfo;
importjava.awt.Point;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.io.File;

importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.filechooser.FileNameExtensionFilter;

,ActionListener{

intx=0;
inty=0;
File[]selectedFiles=null;
intfileIndex=0;
intwidth=200;
intheight=200;

publicApp(){

setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400,300);
setResizable(false);
getContentPane().setLayout(null);

JPanelpanel=newImagePanel();
panel.setBounds(12,40,370,218);
getContentPane().add(panel);

addMouseListener(this);
JButtonbtnBrowse=newJButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12,9,91,21);
getContentPane().add(btnBrowse);
setVisible(true);
}

publicstaticvoidmain(String[]args){
newApp();
}

publicvoidactionPerformed(ActionEvente){
JFileChooserchooser=newJFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilterfilter=newFileNameExtensionFilter(
"JPG&GIFImages","jpg","gif");
//设置文件类型
chooser.setFileFilter(filter);
//打开选择器面板
intreturnVal=chooser.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
selectedFiles=chooser.getSelectedFiles();
repaint();
}
}

publicvoidmouseClicked(MouseEvente){

}

publicvoidmouseEntered(MouseEvente){

}

publicvoidmouseExited(MouseEvente){

}

publicvoidmousePressed(MouseEvente){
Pointpoint=MouseInfo.getPointerInfo().getLocation();
x=point.x;
y=point.y;
}

publicvoidmouseReleased(MouseEvente){
Pointpoint=MouseInfo.getPointerInfo().getLocation();
intthisX=point.x;
intthisY=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();
}

classImagePanelextendsJPanel{

publicvoidpaint(Graphicsg){
super.paint(g);

if(selectedFiles!=null){
ImageIconicon=newImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(),0,0,width,height,this);
}
}
}
}

8. java 如何实现树、图结构

他们的实现是底层程序员早都写好了的,他们的原理确实是树、图结构。

热点内容
斗罗大陆怎么自己建服务器 发布:2024-05-21 16:03:23 浏览:738
河南网通服务器托管云主机 发布:2024-05-21 15:46:00 浏览:170
sqlserver数据库连接数 发布:2024-05-21 15:37:32 浏览:22
安卓一体机如何设置开机直达信源 发布:2024-05-21 15:31:58 浏览:37
纠错码的编译过程 发布:2024-05-21 15:31:56 浏览:240
电脑三千五怎么配置 发布:2024-05-21 15:27:49 浏览:935
买车不能要什么配置 发布:2024-05-21 14:56:20 浏览:427
无锡皮箱密码锁哪里卖 发布:2024-05-21 14:31:03 浏览:472
如何下载泡泡安卓版 发布:2024-05-21 14:27:22 浏览:297
python初始化对象 发布:2024-05-21 14:22:27 浏览:254