圖java代碼
① 怎麼為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();}
}