当前位置:首页 » 编程语言 » 图java代码

图java代码

发布时间: 2023-03-18 08:04:20

① 怎么为java程序添加背景图片代码

仅仅是给窗口添加背景的话是很简单的,添加上以下语句(自己去添加变量哈):x0dx0ax0dx0alabel = new JLabel(background); //background为ImageIconx0dx0a// 把标签的大小位置设置为图片刚好填充整个面板 x0dx0alabel.setBounds(0, 0, this.getWidth(), this.getHeight());x0dx0a//添加图片到frame的第二层(把背景图片添加到分层窗格的最底层作为背景)x0dx0athis.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));x0dx0a//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明x0dx0ajPanel=(JPanel)this.getContentPane();x0dx0a//设置透明x0dx0ajPanel.setOpaque(false);x0dx0ax0dx0a然后你上面那个JPanel p也设置成透明就可以了

② java中输出图片的代码

final ImageView iv=(ImageView)findViewById(R.id.iv);
Button bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View p1)
{
// TODO: Implement this method
if(iv.getDrawable()!=null)
iv.setImageResource(R.id.photo);
else iv.setImageResource(0);
}
});

③ 利用java实现图片翻转的代码

重载渲染控件的paintComponent(Graphics
g)方法.
设你当前图像实例为img,已初始化,需要旋转的角度为ang
public
void
paintComponent(Graphics
g){
super.paintCompoent(g);
Graphics2D
g2d
=
(Graphics2D)g;
g2d.rotate(-angle);
g2d.drawImage(img,0,0,this.getWidth(),this.getHeight(),null);
}
Graphics,Graphics2D
类中有对当前描绘环境进行仿射变换的方法,包括translate,scale,rotate,也可以直接设置仿射变换矩阵,利用这点就可以根据所需要的实现方式来进行描绘.

④ 怎么用java代码模拟一张图片

用java代码模拟一张图片可以这信运样操作:1.创建BufferedImage类
2.根据BufferedImage类得到一个Graphics2D对象
3.根据Graphics2D对象进行逻辑操作
4.处理绘图
5.将绘制好滑脊梁的野冲图片写入到图片

⑤ java 图片缩放代码

直接给你一个类,直接套用就好了

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;

import javax.imageio.ImageIO;

public class Resize {
BufferedImage bufImage;
int width;
int height;

public Resize() {
// TODO Auto-generated constructor stub
}

public Resize(String srcPath,int width,int height) {
this.width = width;
this.height = height;
try{
this.bufImage = ImageIO.read(new File(srcPath));
}catch(Exception e){
e.printStackTrace();
}
}

public static BufferedImage rize(BufferedImage srcBufImage,int width,int height){

BufferedImage bufTarget = null;

double sx = (double) width / srcBufImage.getWidth();
double sy = (double) height / srcBufImage.getHeight();

int type = srcBufImage.getType();
if(type == BufferedImage.TYPE_CUSTOM){
ColorModel cm = srcBufImage.getColorModel();
WritableRaster raster = cm.(width,
height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
}else
bufTarget = new BufferedImage(width, height, type);

Graphics2D g = bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return bufTarget;
}

}

⑥ 按照下列UML图,用java实现代码


Point2D.java



/**

*Title:Point2D.java<br>和袜

*Description:

*

*@author王凯芳

*@date2020年3月5日下午7:09:35

*@version1.0

*/

publicclassPoint2D{

protectedfloatx;

protectedfloaty;


publicPoint2D(){

super();

}


publicPoint2D(floatx,floaty){

super();

this.x=x;

this.y=y;

}


publicfloatgetX(){

returnx;

}


publicvoidsetX(floatx){

this.x=x;

}


publicfloatgetY(){

returny;

}


publicvoidsetY(floaty){

this.y=y;

}


publicfloat[]getXY(){

returnnewfloat[]{x,y};

}


publicvoidsetXY(floatx,floaty){

this.x=x;

this.y=y;

}


@Override

publicStringtoString(){

return"("+x+","+y+察棚信")";

}


}


Point3D.java



/**

*Title:Point3D.java<br>

*Description:

*

*@author王凯芳

*@date2020年3月5日下午7:09:35

*@version1.0

*/

{

privatefloatz;


publicPoint3D(){

super();

}


publicPoint3D(floatx,floaty,floatz){

super();

this.x=x;

this.y=y;

this.z=z;

}


publicfloatgetZ(){

returnz;

}


publicvoidsetZ(float败轮z){

this.z=z;

}


publicfloat[]getXYZ(){

returnnewfloat[]{x,y,z};

}


publicvoidsetXYZ(floatx,floaty,floatz){

this.x=x;

this.y=y;

this.z=z;

}


@Override

publicStringtoString(){

return"("+x+","+y+","+z+")";

}


}

⑦ java编程采用图形界面实现两个内容的交换,图形界面如下图

以下是一个简单的Java GUI程序,可以实现两个内容(字符串、图片等)之间的交换:

Copy code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwapContent extends JFrame implements ActionListener {
private JLabel label1, label2;
private JButton swapBtn;

public SwapContent() {
// 设置窗口大小和标题
setSize(400, 300);
setTitle("Swap Content Demo");

// 创建标签和按钮模唤
label1 = new JLabel("Content A");
label2 = new JLabel("Content B");
swapBtn = new JButton("Swap");

// 设置标签和按钮的位置和大小
label1.setBounds(50, 50, 100, 30);
label2.setBounds(200, 50, 100, 30);
swapBtn.setBounds(150, 150, 100, 30);

// 将标签和按钮添加到窗口中
add(label1);
add(label2);
add(swapBtn);

// 注册按钮的事件监听器
swapBtn.addActionListener(this);

// 设置窗口的默认关闭方式
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {
// 获取标签的内容
String temp = label1.getText();
label1.setText(label2.getText());
label2.setText(temp);
}

public static void main(String[] args) {
// 创建窗口对象并显示
SwapContent sc = new SwapContent();
sc.setVisible(true);
}
}
该程序创建了一个窗口,包括两个标签(label1和label2)和一个按钮(swapBtn)。当用户点击按钮时,程序会交换label1和label2的内容。

在程序的actionPerformed方法中,程序将label1和label2的内容交换。具体实现是,首先使用旦册凯label1.getText()获取label1的内容,并将其保存到一个临时变量temp中;然后,将label1的内容设置为label2的内容,将label2的内容设置为temp的姿裂值。这样,label1和label2的内容就被互换了。

在main方法中,程序创建了SwapContent对象,并将其显示出来。程序运行后,用户可以在窗口中交换label1和label2的内容。

⑧ JAVA的图形用户界面代码

package hao;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class ChatPanel extends JPanel {

private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;

JButton music;
public ChatPanel() {
setLayout(new BorderLayout());

text = new JTextPane();
text.setEditable(false);

doc = text.getStyledDocument();//跟踪文本和图片写到该区域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面对JComboBox的巧用***********************************************************************
String[] str_name = { "宋体", "黑体", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常规", "斜体", "粗体", "粗斜体" };
String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" };
String[] str_BackColor = { "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" };
fontName = new JComboBox(str_name);
fontSize = new JComboBox(str_Size);
fontStyle = new JComboBox(str_Style);
fontColor = new JComboBox(str_Color);
fontBackColor = new JComboBox(str_BackColor);

fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//创建一个可以容纳多个Box组件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字体~~"), b2 = new JLabel("样式~~"),b3 = new JLabel("字号~~"),b4 = new JLabel("颜色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);

textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));

inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //设置文本区的换行策略。88888*********************************

send = new JButton("发送");
record=new JButton("显示记录");
saveRecord=new JButton("储存记录");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //设置按钮间距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置Box的边框线********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);

inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置输入窗口边框线*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//设置输入窗口边框线*******************

JPanel audioPanel = new JPanel();//最上面的边框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));

music = new JButton("想听就听");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//设置输入窗口边框线*******************
audioPanel.add(music);

add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}

void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}

public void insert(MessageStyle attrib) {
try {

doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//写完后接着换行************

} catch (BadLocationException e) {
e.printStackTrace();
}
}

public MessageStyle getMessageStyle(String line) {

MessageStyle att = new MessageStyle();

att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常规")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗体")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜体")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜体")) {
att.setStyle(MessageStyle.BOLD_ITALIC);
}

String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
}
else if (temp_color.equals("红色")) {
att.setColor(new Color(255, 0, 0));
}
else if (temp_color.equals("蓝色")) {
att.setColor(new Color(0, 0, 255));
}
else if (temp_color.equals("黄色")) {
att.setColor(new Color(255, 255, 0));
}
else if (temp_color.equals("绿色")) {
att.setColor(new Color(0, 255, 0));
}

String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("无色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
}
else if (temp_backColor.equals("淡红")) {
att.setBackColor(new Color(255, 200, 200));
}
else if (temp_backColor.equals("淡蓝")) {
att.setBackColor(new Color(200, 200, 255));
}
else if (temp_backColor.equals("淡黄")) {
att.setBackColor(new Color(255, 255, 200));
}
else if (temp_backColor.equals("淡绿")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
}

}

⑨ java绘图代码

画笔定义

importjava.awt.*;

/**

*@authorHardneedl

*/

interfaceBrush{

voiddoPaint(Graphicsg);

}

画笔工厂

importjava.awt.*;

/**

*@authorHardneedl

*/

classBrushFactory{

staticfinalintLINE_BRUSH=0;

staticfinalintELLIPSE_BBRUSH=1;

staticfinalintRECTANGLE_BRUSH=2;

staticfinalintPOLYGON_BRUSH=3;

staticfinalintELLIPSE_FILL_BRUSH=4;

staticfinalintRECTANGLE_FILL_BRUSH=5;

staticfinalBrushNO=newNONE();

staticfinalBrushLINE=newLineBrush();

staticfinalBrushELLIPSE=newEllipseBrush();

staticfinalBrushELLIPSE_FILL=newEllipseFillBrush();

staticfinalBrushRECTANGLE=newRectangleBrush();

staticfinalBrushRECTANGLE_FILL=newRectangleFillBrush();

staticfinalBrushPOLYGON=newPolygonBrush();

BrushgetBrush(intbrushIndex){

switch(brushIndex){

caseLINE_BRUSH:returnLINE;

caseELLIPSE_BBRUSH:returnELLIPSE;

caseRECTANGLE_BRUSH:returnRECTANGLE;

caseRECTANGLE_FILL_BRUSH:returnRECTANGLE_FILL;

casePOLYGON_BRUSH:returnPOLYGON;

caseELLIPSE_FILL_BRUSH:returnELLIPSE_FILL;

default:returnNO;

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLACK);

gg.drawLine(70,70,200,200);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.PINK);

gg.drawOval(100,100,200,50);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.PINK);

gg.fillOval(100,100,200,50);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.RED);

gg.drawPolygon(newint[]{48,50,244,483,310},newint[]{36,192,281,302,77},5);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLUE);

gg.drawRect(70,70,100,100);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLUE);

gg.fillRect(70,70,100,100);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.RED);

gg.drawString("Nobrushselected!",20,30);

gg.dispose();

}

}

}

图形界面

importjavax.swing.*;

importjavax.swing.border.*;

importjava.awt.*;

importjava.awt.event.*;

/**

*@authorHardneedl

*/

finalclassDrawextendsJFrame{

publicStringgetTitle(){return"frametitle";}

=newDimension(600,400);

(){returnsize;}

publicDimensiongetMaximumSize(){returnsize;}

publicDimensiongetMinimumSize(){returnsize;}

publicDimensiongetSize(){returnsize;}

=newLineAction();

=newRectangleAction();

=newRectangleFillAction();

=newEllipseAction();

=newEllipseFillAction();

=newPolygonAction();

=newBrushFactory();

privatestaticBrushbrush;

=newJComponent(){

protectedvoidpaintComponent(Graphicsg){

super.paintComponent(g);

if(brush!=null){

brush.doPaint(g);

}

}

publicBordergetBorder(){returnBorderFactory.createLineBorder(Color.BLACK,2);}

};

Draw()throwsHeadlessException{

init();

attachListeners();

doLay();

}

privatevoidinit(){

JMenuBarmenuBar=newJMenuBar();

menuBar.add(newJMenu(lineAction));

JMenuelp=newJMenu("椭圆");

elp.add(ellipseAction);

elp.add(ellipseFillAction);

menuBar.add(elp);

JMenurct=newJMenu("矩形");

rct.add(rectangleAction);

rct.add(rectangleFillAction);

menuBar.add(rct);

menuBar.add(newJMenu(polygonAction));

setJMenuBar(menuBar);

}

privatevoidattachListeners(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

privatevoiddoLay(){

Containercontainer=getContentPane();

container.add(canvas,BorderLayout.CENTER);

JPanelbuttonsPane=newJPanel(newGridLayout(2,3));

buttonsPane.add(newJButton(lineAction));

buttonsPane.add(newJButton(ellipseAction));

buttonsPane.add(newJButton(rectangleAction));

buttonsPane.add(newJButton(polygonAction));

buttonsPane.add(Box.createHorizontalBox());

buttonsPane.add(newJButton(ellipseFillAction));

buttonsPane.add(newJButton(rectangleFillAction));

container.add(buttonsPane,BorderLayout.SOUTH);

pack();

setVisible(true);

}

{

privateRectangleAction(){super("空心矩形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.RECTANGLE_BRUSH);

canvas.repaint();

}

}

{

privateRectangleFillAction(){super("实心矩形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.RECTANGLE_FILL_BRUSH);

canvas.repaint();

}

}

{

privateEllipseFillAction(){super("实心椭圆");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.ELLIPSE_FILL_BRUSH);

canvas.repaint();

}

}

{

privateEllipseAction(){super("空心椭圆");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.ELLIPSE_BBRUSH);

canvas.repaint();

}

}

{

privatePolygonAction(){super("多边形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.POLYGON_BRUSH);

canvas.repaint();

}

}

{

privateLineAction(){super("直线");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.LINE_BRUSH);

canvas.repaint();

}

}

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

}

热点内容
动态规划01背包算法 发布:2024-11-05 22:17:40 浏览:846
nasm编译器如何安装 发布:2024-11-05 22:01:13 浏览:176
登录密码在微信的哪里 发布:2024-11-05 22:00:29 浏览:735
c防止反编译工具 发布:2024-11-05 21:56:14 浏览:243
安卓虚拟机怎么用 发布:2024-11-05 21:52:48 浏览:340
php时间搜索 发布:2024-11-05 20:58:36 浏览:475
燕山大学编译原理期末考试题 发布:2024-11-05 20:13:54 浏览:524
华为电脑出现临时服务器 发布:2024-11-05 20:05:08 浏览:405
斗战神免费挖矿脚本 发布:2024-11-05 19:53:25 浏览:662
网吧服务器分别是什么 发布:2024-11-05 19:45:32 浏览:389