「java图像显示」java实现图像识别

博主:adminadmin 2022-11-29 14:45:08 42

今天给各位分享java图像显示的知识,其中也会对java实现图像识别进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中怎样让panel完全透明,只显示上面的图片?

import javax.swing.* ;

public class baiduzhidao1 {

public static void main(String args[]){

ImageIcon img = new ImageIcon("1.jpg"); /*负责显示图像的JLable对象j1*/

JLabel jl = new JLabel(img);

JPanel j = new JPanel() ; /*在JFrame上add上面板j*/

JFrame jf = new JFrame() ;

jf.setContentPane(j) ;

j.add(jl) ; /*在面板j上add上JLable对象j1*/

JPanel jjj = new JPanel() ; /*创建一个透明的面板放在JLable对象j1上负责放控件,此例中放置了一个按钮*/

jl.add(jjj) ;

jjj.setSize(200, 100) ; /*这里注意要设置大小*/

jjj.setOpaque(false) ; /*将负责放置控件的面板设置成透明,详细请查API*/

jjj.add(new JButton("确定")) ; /*添加按钮*/

jf.setVisible(true) ;

jf.setSize(500, 300) ;

}

}

容器从顶层开始顺序如下:

jf(JFrame对象实例) -- j (JPanel对象实例,无须设置透明) -- j1(JLable对象实例,负责显示图片)

-- jjj(JPanel对象实例,负责放置控件,需要设置成透明才不会让自身的底色覆盖了上层图片)

java从GUI中读入图像并显示,求解答

fileChooser1.showOpenDialog(null)

得到的路径、得到Image,在JFrame的paint事件中,绘上去

请教Java 如何显示本地图像 application的

package guitest.myboard;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

//the point

//impress the info of one point,the x and y

class OnePoint implements Serializable {

int x;

int y;

int tool;

Color c;

int border;

public OnePoint(int x,int y,int tool,Color cc,int border){

this.x=x;

this.y=y;

this.tool=tool;

this.c=cc;

this.border=border;

}

}

class DrawingBoard extends Frame implements MouseListener,ItemListener,ActionListener,MouseMotionListener{

Button pen;

Button line ;

Button ellipse ;

Button rect ;

Button clear ;

Button colorboard ;

Button storebutton;

Button openbutton;

Choice sizechoice ;

Choice colorchoice ;

Label pensize;

Label pencolor;

Panel panel ;

FileDialog storefile;

FileDialog openfile;

FileInputStream filein;

FileOutputStream fileout;

ObjectInputStream objectin;

ObjectOutputStream objectout;

int flagtool=0;

Color flagcolor;

int border;

BasicStroke size;

OnePoint p1,p2;

VectorOnePoint points=new VectorOnePoint();

public DrawingBoard(){

pen=new Button("画笔");

line=new Button("直线");

ellipse=new Button("圆");

rect=new Button("矩形");

clear=new Button("清除");

colorboard=new Button("调色板");

storebutton=new Button("存储文件");

openbutton=new Button("打开文件");

pensize=new Label("画笔大小");

pencolor=new Label("画笔颜色");

storefile=new FileDialog(this,"存储文件",FileDialog.SAVE);

storefile.setVisible(false);

storefile.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

storefile.setVisible(false);

}

});

openfile=new FileDialog(this,"打开文件",FileDialog.LOAD);

openfile.setVisible(false);

openfile.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

openfile.setVisible(false);

}

});

sizechoice=new Choice();

sizechoice.add("1");

sizechoice.add("2");

sizechoice.add("4");

sizechoice.add("6");

sizechoice.add("8");

sizechoice.addItemListener(this);

colorchoice=new Choice();

colorchoice.add("black");

colorchoice.add("red");

colorchoice.add("blue");

colorchoice.add("green");

colorchoice.addItemListener(this);

pen.addActionListener(this);

line.addActionListener(this);

ellipse.addActionListener(this);

rect.addActionListener(this);

clear.addActionListener(this);

colorboard.addActionListener(this);

storebutton.addActionListener(this);

openbutton.addActionListener(this);

panel=new Panel();

panel.add(storebutton);

panel.add(openbutton);

panel.add(pen);

panel.add(line);

panel.add(ellipse);

panel.add(rect);

panel.add(clear);

panel.add(sizechoice);

panel.add(pensize);

panel.add(colorchoice);

panel.add(pencolor);

panel.add(colorboard);

add(panel,BorderLayout.NORTH);

setBounds(100,100,700,600);

setVisible(true);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

/**

* 添加鼠标事件的监听器,否则,鼠标的移动和点击都将无法识别!

* */

addMouseListener(this);

addMouseMotionListener(this);

}

public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;

if(flagtool==2){ //qing chu

g.clearRect(0,0,getSize().width,getSize().height);

}

for(int i=0;ipoints.size()-1;i++){

p1=(OnePoint)points.elementAt(i);

p2=(OnePoint)points.elementAt(i+1);

g2d.setColor(p1.c); //////////////需要使用Graphics2D从Graphics类中继承下来的方法 setColor()设置当前的颜色

size=new BasicStroke(p1.border,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

g2d.setStroke(size);

if(p1.tool==p2.tool){

switch(p1.tool){

case 0:

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

g2d.draw(line1);

break;

case 1:

Line2D.Double line2=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);

g2d.draw(line2);

break;

case 3:

Ellipse2D.Double 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 4:

Rectangle2D.Double rect=new Rectangle2D.Double(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));

g2d.draw(rect);

break;

default:

}

}

}

}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) { //鼠标点下时候,将当前的点信息记录

OnePoint pp1=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);

points.addElement(pp1);

//repaint();

}

public void mouseReleased(MouseEvent e) {//鼠标松开时候,如果是画笔,则当前截断,是其余状态记下一枚点信息

if(flagtool==0){

points.addElement(new OnePoint(-1,-1,22,flagcolor,border));

}

else{

OnePoint pp2=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);

points.addElement(pp2);

points.add(new OnePoint(-1,-1,22,flagcolor,border));

}

repaint();

}

public void itemStateChanged(ItemEvent e) {

if(e.getSource()==colorchoice){

String selected=colorchoice.getSelectedItem();

if(selected=="black"){

flagcolor=new Color(0,0,0);

}

else if(selected=="red"){

flagcolor=new Color(255,0,0);

}

else if(selected=="blue"){

flagcolor=new Color(0,0,255);

}

else if(selected=="green"){

flagcolor=new Color(0,255,0);

}

}

else if(e.getSource()==sizechoice){

String selected=sizechoice.getSelectedItem();

if (selected=="1"){

border=1;

}

else if(selected=="2"){

border=2*2;

}

else if(selected=="4"){

border=4*2;

}

else if(selected=="6"){

border=6*2;

}

else if(selected=="8"){

border=8*2;

}

}

}

public void update(Graphics g) { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

paint(g);

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==pen){

flagtool=0;

}

else if(e.getSource()==line){

flagtool=1;

}

else if(e.getSource()==clear){

flagtool=2;

points.removeAllElements();

repaint(); //此语要有,否则今生无法删除!

}

else if(e.getSource()==ellipse){

flagtool=3;

}

else if(e.getSource()==rect){

flagtool=4;

}

else if(e.getSource()==colorboard){

/*

* 使用 javax.swing.×包中的 JColorChooser 类的静态方法showDialog(Component component,String title,Color color ),

* 该方法的参数,component是当前显示对话框的父框架,color是设置调色板初始的被选颜色

*

* 该方法返回被选的颜色,类型为Color

* */

Color color=JColorChooser.showDialog(this, "调色板",flagcolor);

flagcolor=color;

}

else if(e.getSource()==openbutton){

openfile.setVisible(true);

if(openfile.getFile()!=null){

int temp=flagtool;

flagtool=2;

repaint();

try{

points.removeAllElements();

File file=new File(openfile.getDirectory(),openfile.getFile());

filein=new FileInputStream(file);

objectin=new ObjectInputStream(filein);

points=(Vector)objectin.readObject();

objectin.close();

filein.close();

flagtool=temp;

repaint();

}

catch(Exception ee){

System.out.println(ee.toString());

}

}

}

else if(e.getSource()==storebutton){

storefile.setVisible(true);

if(storefile.getFile()!=null){

try {

File file=new File(storefile.getDirectory(),storefile.getFile());

fileout=new FileOutputStream(file);

objectout=new ObjectOutputStream(fileout);

objectout.writeObject(points);

objectout.close();

fileout.close();

repaint();

}

catch (FileNotFoundException e1) {

System.out.println(e1.toString());

e1.printStackTrace();

} catch (IOException ee) {

System.out.println(ee.toString());

ee.printStackTrace();

}

}

}

}

public void mouseDragged(MouseEvent e) {//鼠标拖动时候,//当且仅当 flagtool==0,或者表示为橡皮的时候

//才将拖动过程中涉及到的点全部记录下来,并且调用repain()方法,重画当前

// TODO Auto-generated method stub

if(flagtool==0){

OnePoint pp3=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);

points.addElement(pp3);

repaint();

}

}

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

}

}

public class PaintBoard{

public static void main(String[] args){

DrawingBoard oneBorder=new DrawingBoard();

}

}

java程序中显示图像

Image image;

Spicture()

{

this.setSize(200,200);

tool=getToolkit();

s="123.jpg";

//image=tool.getImage("dog.gif");

image = Toolkit.getDefaultToolkit().getImage("dog.gif");

}

class1.loadpicture=(c\: )

关于java图像显示和java实现图像识别的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-29,除非注明,否则均为首码项目网原创文章,转载请注明出处。