java中图形的代码 用java编写图形

求Java 实现绘制图形并移动代码

代码如下:import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Graphics;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class Zfx extends MIDlet { private Display display; public Zfx() { display=Display.getDisplay(this); Zfxc qs=new Zfxc(); display.setCurrent(qs); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { }}class Zfxc extends Canvas implements Runnable{ private int x,y,x1,y1,i; private boolean flag; Zfxc(){ init(); } private void init(){ Thread thread=new Thread(this); thread.start(); } protected void paint(Graphics g) { g.setColor(255,255,255); g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.setColor(0); g.fillRect(x, y, 10, 10); } private void logic(){ if(x=this.getWidth()-10){ x1=2; } if(x1==1){ x+=3; }else if(x1==2){ x-=3; } if(y=this.getHeight()-10){ y1=2; } if(y1==1){ y+=3; }else if(y1==2){ y-=3; } } protected void keyPressed(int keyCode) { if(keyCode==-5){ if(i==0){ flag=true; i=1; }else if(i==1){ flag=false; i=0; } } } public void run() { while(true){ if(flag){ logic(); } repaint(); try { Thread.sleep(80); } catch (InterruptedException e) { e.printStackTrace(); } } }}求Java 实现绘制图形并移动代码

我们提供的服务有:成都网站设计、成都网站建设、微信公众号开发、网站优化、网站认证、金山ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的金山网站制作公司

用JAVA写一个简单图形类

public class Test013 {

/**

* 编写一个图形类MyGraphic。 1)它有两个基本属性:图形线条的颜色String lineColor和图形的填充颜色String

* fillColor。 2)设计矩形类CRectangle,有属性double rLong和宽double rWidth, 使用方法 float

* calCircum()可以返回矩形的周长,使用方法float calSquare()可以返回矩形的面积。

* 编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。 3)设计圆形类CCircle,定义属性:半径double

* radius,可以通过同名方法计算周长和面积。 编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。

* 4)编写出应用程序对CRectangle类和CCircle类进行验证。 完成上述要求即可

*/

public static void main(String[] args) {

MyGraphic rectangle = new CRectangle(10, 5);

rectangle.setFillColor("紫色"); //设定矩形填充颜色

rectangle.setLineColor("白色"); //设定矩形线条颜色

rectangle.show();

System.out.println("矩形周长 = " + rectangle.calCircum());

System.out.println("矩形面积 = " + rectangle.calSquare());

MyGraphic circle = new CCircle(3);

circle.setFillColor("红色");

circle.setLineColor("黄色");

circle.show();

System.out.println("园形周长 = " + circle.calCircum());

System.out.println("园形面积 = " + circle.calSquare());

}

}

/**

* 图形类

*

*/

abstract class MyGraphic {

private String lineColor; // 图形线条的颜色

private String fillColor; // 图形的填充颜色

public String getLineColor() {

return lineColor;

}

public void setLineColor(String lineColor) {

this.lineColor = lineColor;

}

public String getFillColor() {

return fillColor;

}

public void setFillColor(String fillColor) {

this.fillColor = fillColor;

}

public MyGraphic(String lineColor, String fillColor) {

this.lineColor = lineColor;

this.fillColor = fillColor;

}

public MyGraphic() {

}

/**

* 显示图形的颜色

*/

public abstract void show();

/**

* 计算图形的周长

*/

public abstract float calCircum();

/**

* 计算图形的面积

*/

public abstract float calSquare();

}

/**

* 矩形类

*

*/

class CRectangle extends MyGraphic {

private double rLong; // 长

private double rWidth; // 宽

/**

* 通过构造函数为图形的属性赋值

*

* @param rLong

* @param rWidth

*/

public CRectangle(double rLong, double rWidth) {

this.rLong = rLong;

this.rWidth = rWidth;

}

/**

* @return 矩形的周长

*/

@Override

public float calCircum() {

return (float) (2 * (rLong + rWidth));

}

/**

* @return 矩形的面积

*/

@Override

public float calSquare() {

return (float) (rLong * rWidth);

}

@Override

public void show() {

System.out.println("矩形线条的颜色: " + super.getLineColor());

System.out.println("矩形填充颜色: " + super.getFillColor());

}

public double getrLong() {

return rLong;

}

public void setrLong(double rLong) {

this.rLong = rLong;

}

public double getrWidth() {

return rWidth;

}

public void setrWidth(double rWidth) {

this.rWidth = rWidth;

}

}

/**

* 圆形类

*

*/

class CCircle extends MyGraphic {

private double radius; // 圆形半径

public CCircle(double radius) {

this.radius = radius;

}

/**

* @return 圆形的周长

*/

@Override

public float calCircum() {

return (float) (2 * Math.PI * radius);

}

/**

* @return 圆形的面积

*/

@Override

public float calSquare() {

return (float) (Math.PI * radius * radius);

}

@Override

public void show() {

System.out.println("圆形线条的颜色: " + super.getLineColor());

System.out.println("圆形填充颜色: " + super.getFillColor());

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

}

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打印出图片中的图形,求完整代码。

您好,您这样:

public class search {

public int counter(String inputs, String word) {

int counter = 0;

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

if(word.equals(inputs.charAt(i)+"")){

counter++;

}

}

return counter;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("请输入一个字符串:");

String str = sc.next();

System.out.print("请输入要查找的字符:");

String s = sc.next();

search sear = new search();

int c = sear.counter(str, s);

System.out.println("\"" + str + "\"中包含" + c + "个\"爱\"");

}

}


当前名称:java中图形的代码 用java编写图形
网站URL:http://pwwzsj.com/article/ddgpejh.html