代码如下:import javax.microedition.lci.Canvas;import javax.microedition.lci.Display;import javax.microedition.lci.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 实现绘制图形并移动代码
② 求一个,java图形化界面的源码
自己写的石头剪子布代码,有图形界面,可以把自己选中的图片命名为.jpg放到工程目录下做背景图片。//////////////////////////// package HomeWork;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class StoneShearsSack { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new StoneWindow("Sample Game");
}
}
final class StoneWindow extends JFrame implements ActionListener,ItemListener{
JLabel win,draw,lose,score,count;
JTextArea text;
JComboBox list;
JButton fight,clear;
int swin,sdraw,slose,sscore;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
String str1,str2,str3;
Random rdm = new Random();
int flag,random ,flag1, count1;
//final ImageIcon con = new ImageIcon("a.jpg") ;
public StoneWindow(String s){
setTitle("Sample Game");
fight = new JButton("出招");
clear = new JButton("清除分数");
//final ImageIcon con = new ImageIcon("b.jpg") ;
text = new JTextArea(){
final ImageIcon con = new ImageIcon("a.jpg");
Image mg = con.getImage();
{setOpaque(false);}
public void paintComponent(Graphics g){
g.drawImage(mg, 0, 0, this);
super.paintComponent(g);
}
};
text.setForeground(Color.black);
Font f = new Font("华文行楷",Font.BOLD,50);
text.setFont(f);
list = new JComboBox();
list.addItem("石头");
list.addItem("剪子");
list.addItem("布");
panel2.setBackground(Color.blue);
panel2.add(list);
panel2.add(fight);
panel2.add(clear);
panel2.setPreferredSize(new Dimension(900,50));
list.addItemListener(this);
fight.addActionListener(this);
clear.addActionListener(this);
win = new JLabel("赢:" + swin);
draw = new JLabel(" 和:" + sdraw);
lose = new JLabel(" 输:" + slose);
score = new JLabel(" 得分:" + sscore);
count = new JLabel(" 总局:" + count1);
Font f1 = new Font("宋体",1,30);
win.setFont(f1);
draw.setFont(f1);
lose.setFont(f1);
score.setFont(f1);
count.setFont(f1);
win.setForeground(Color.cyan);
draw.setForeground(Color.green);
lose.setForeground(Color.blue);
score.setForeground(Color.black);
count.setForeground(Color.pink);
panel1.setBackground(Color.red);
panel1.add(win);
panel1.add(draw);
panel1.add(lose);
panel1.add(score);
panel1.add(count);
panel1.setPreferredSize(new Dimension(900,50));
add(panel1,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
add(panel2,BorderLayout.SOUTH);
setBounds(100,100,900,450);
validate();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == fight){
if(list.getSelectedItem() == "石头"){
flag = 0;
}
else if(list.getSelectedItem() == "剪子"){
flag = 1;
}
else if(list.getSelectedItem() == "布"){
flag = 2;
}
str1 = (String)list.getSelectedItem();
text.setText("");
text.append("\n");
text.append(" 你 出: " + str1 + "\n");
text.append(" 电脑出: " + this.random() + "\n");
text.append(" 结 果: " + this.result() + "\n");
this.finalresult();
}
if(e.getSource() == clear){
swin = 0;
sdraw = 0;
slose = 0;
sscore = 0;
count1 = 0;
text.setText("");
win.setText(" 赢:" + swin);
draw.setText(" 和:" + sdraw);
lose.setText(" 输:" + slose);
score.setText(" 得分:" + sscore);
count.setText(" 总局:" + count1);
}
}
public String random(){
random = rdm.nextInt(3);
if(random == 0)
str2 = "石头";
else if(random == 1)
str2 = "剪子";
else if(random == 2)
str2 = "布";
return str2;
}
public String result(){
if(random == flag){
str3 ="打平";
flag1 = 0;
}
else if(random < flag &&flag == 1){
str3 = "你输了";
flag1 = 1;
}
else if(random <flag && random ==1){
str3 = "你输了";
flag1 = 1;
}
else if(random ==2 && flag == 0){
str3 = "你输了";
flag1 = 1;
}
else{
str3 = "你赢了";
flag1 = 2;
}
return str3;
}
public void finalresult(){
if(flag1 == 0){
sdraw ++;
draw.setText(" 平:" + sdraw);
score.setText(" 得分:" + sscore);
}
if(flag1 == 1){
slose ++;
sscore = sscore - 10;
lose.setText(" 输:" + slose);
score.setText(" 得分:" + sscore);
}
if(flag1 == 2) {
swin ++;
sscore = sscore + 10;
win.setText(" 赢:" + swin);
score.setText(" 得分:" + sscore);
}
count1 = swin + slose + sdraw;
count.setText(" 总局:" + count1);
}
public void itemStateChanged(ItemEvent e){
}
}
③ java图形用户界面的选择一个文件并复制(另存为)的代码,麻烦了。
可以给JTextField t1 一个初始路径,那样,如果每次都是备份同一个文件直接点保存选路径就可以,如果想备份其他文件再选其他文件就可以了
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class BakTo extends JFrame implements ActionListener {
JLabel l1 = new JLabel("原始文件");
JTextField t1 = new JTextField(40);
JButton b1 = new JButton("选择");
JLabel l2 = new JLabel("保存目录");
JTextField t2 = new JTextField(40);
JButton b2 = new JButton("保存");
JFileChooser j1 = new JFileChooser();
JFileChooser j2 = new JFileChooser();
static File fileFlag = new File("");
public BakTo() {
setBounds(200, 200, 600, 140);
setLayout(new FlowLayout());
add(l1);
add(t1);
add(b1);
add(l2);
add(t2);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setResizable(false);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b1) {
int n = j1.showOpenDialog(null);
String filename = j1.getSelectedFile().toString();
if (n == JFileChooser.APPROVE_OPTION) {
t1.setText(filename);
fileFlag = new File(filename);
}
}
else if (e.getSource() == b2) {
j2.setCurrentDirectory(fileFlag);// 设置打开对话框的默认路径
j2.setSelectedFile(fileFlag);// 设置选中原来的文件
int n = j2.showSaveDialog(null);
String filename2 = j2.getSelectedFile().toString();
if(filename2.indexOf(".")!=-1){
filename2=filename2.substring(0,filename2.indexOf("."));
}
// 以下两句是获得原文件的扩展名
int flag = t1.getText().lastIndexOf(".");
String kuozhan = t1.getText().substring(flag);
String date = getDate();// 取得当前日期
if (n == JFileChooser.APPROVE_OPTION) {
t2.setText(filename2 +date+ kuozhan);// 把日期和扩展名添加到原来文件的后面
}
int b;
char[] t = new char[25];
// 这里我改用了文件流
FileInputStream input = new FileInputStream(t1.getText());
FileOutputStream output = new FileOutputStream(filename2+date
+ kuozhan);// 把扩展名添加到原来文件的后面
int in = input.read();
while (in != -1) {
output.write(in);
in = input.read();
}
input.close();
output.close();
}
} catch (Exception x) {
System.out.println(x);
}
}
public String getDate() {
Calendar rightNow = Calendar.getInstance();
System.out.println(rightNow.toString());
int year = rightNow.YEAR;
int date = rightNow.DATE;
int month = rightNow.MONTH + 1;
String d = year + "年" + month + "月" + date + "日";
return d;
}
public static void main(String args[]) {
BakTo c1 = new BakTo();
}
}
④ 求一个java程序:绘图程序包括画圆,椭圆,线,矩形,自定义。并且可以调图形颜色!
publicenumShapeTypes{
LINE,CIRCLE,RECTANGLE
}
publicinterfaceShape{
voidpaint(Graphicsg);
}
{
//矩形左上角的坐标
privateintx,y;
//矩形的宽度和高度
privateintwidth,height;
privateColorrectangleColor;
publicRectangle(){
super();
}
publicRectangle(intx,inty,intwidth,intheight,ColorrectangleColor){
super();
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.rectangleColor=rectangleColor;
}
@Override
publicvoidpaint(Graphicsg){
g.setColor(rectangleColor);
g.drawRect(x,y,width,height);
}
}
{
//直线的起始位置
privateintx1,y1;
//直线的终止位置
privateintx2,y2;
privateColorlineColor;
publicLine(intx1,inty1,intx2,inty2,ColorlineColor){
super();
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.lineColor=lineColor;
}
publicLine(){
super();
}
@Override
publicvoidpaint(Graphicsg){
g.setColor(lineColor);
g.drawLine(x1,y1,x2,y2);
}
}
{
//圆的颜色
privateColorcircleColor;
//圆心的坐标
privateintx,y;
//圆的半径
privateintradius;
publicCircle(){
super();
}
publicCircle(intx,inty,intradius,ColorcircleColor){
super();
this.circleColor=circleColor;
this.x=x;
this.y=y;
this.radius=radius;
}
@Override
publicvoidpaint(Graphicsg){
g.setColor(circleColor);
//画弧,当弧的宽度和高度一致且从0~360度时就是原形了
g.drawArc(x,y,radius,radius,0,360);
}
}
,MouseMotionListener{
=-5229161042153132522L;
//鼠标点击起始坐标和当前坐标
privateintbeginX=0,beginY=0,currentX=0,currentY=0;
//判断鼠标是否被按下
privatebooleanisMousePressing=false;
//保存当前的图形,在撤销和恢复时使用
privatefinalStack<Shape>currentShapes=newStack<Shape>();
//保存已经删除过的图形
privatefinalStack<Shape>deletedShapes=newStack<Shape>();
privateShapeTypestype;
privateColorcolor;
publicSketchpadPanel(){
addMouseListener(this);
addMouseMotionListener(this);
}
/**
*撤销方法
*/
publicvoindo(){
if(currentShapes.size()>0){
//从所有保存过的图形中取出最后一个,放入到已删除的图形中去
Shapeshape=currentShapes.pop();
deletedShapes.push(shape);
repaint();
}
}
/**
*恢复撤销方法
*/
publicvoidredo(){
if(deletedShapes.size()>0){
//从所有删除的图形中取出最后一个,放入保存的图形中
Shapeshape=deletedShapes.pop();
currentShapes.push(shape);
repaint();
}
}
/**
*设置命令
*
*@paramtype
*/
publicvoidsetShapeType(ShapeTypestype){
this.type=type;
}
/**
*设置颜色
*
*@paramcolor
*/
publicvoidsetColor(Colorcolor){
this.color=color;
}
publicvoipdete(Graphicsg){
paint(g);
}
/**
*绘制画板
*/
@Override
publicvoidpaint(Graphicsg){
//绘制画板
Dimensionsize=getSize();
intwidth=size.width;
intheight=size.height;
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
//绘制所有图形
Shapeshape=null;
Enumeration<Shape>e=currentShapes.elements();
while(e.hasMoreElements()){
shape=e.nextElement();
shape.paint(g);
}
//如果当前鼠标没有释放
if(isMousePressing){
g.setColor(color);
switch(type){
//绘制直线
caseLINE:
g.drawLine(beginX,beginY,currentX,currentY);
break;
//绘制矩形
caseRECTANGLE:
if(currentX<beginX){
if(currentY<beginY){
//如果当前位置在起始位置的左上方,则以鼠标当前位置为矩形的左上角位置
g.drawRect(currentX,currentY,beginX-currentX,beginY-currentY);
}else{
//如果当前位置在起始位置的左下方,则以鼠标当前位置的横坐标和起始位置的纵坐标作为矩形的左上角位置
g.drawRect(currentX,beginY,beginX-currentX,currentY-beginY);
}
}else{
if(currentY<beginY){
//如果当前位置在起始位置的右上方,则以鼠标起始位置的很坐标和当前位置的纵坐标作为矩形的左上角位置
g.drawRect(beginX,currentY,currentX-beginX,beginY-currentY);
}else{
//如果当前位置在起始位置的右下方,则已起始位置作为矩形的左上叫位置
g.drawRect(beginX,beginY,currentX-beginX,currentY-beginY);
}
}
break;
//绘制圆形
caseCIRCLE:
//半径为a*a+b*b的平方根
intradius=(int)Math
.sqrt((beginX-currentX)*(beginX-currentX)+(beginY-currentY)*(beginY-currentY));
g.drawArc(beginX-radius/2,beginY-radius/2,radius,radius,0,360);
break;
}
}
}
@Override
publicvoidmouseClicked(MouseEvente){
}
@Override
publicvoidmouseEntered(MouseEvente){
}
@Override
publicvoidmouseExited(MouseEvente){
}
/**
*当鼠标按下的时候获得起始坐标
*/
@Override
publicvoidmousePressed(MouseEvente){
beginX=e.getX();
beginY=e.getY();
isMousePressing=true;
}
/**
*当鼠标释放时获得当前坐标
*/
@Override
publicvoidmouseReleased(MouseEvente){
currentX=e.getX();
currentY=e.getY();
isMousePressing=false;
//当释放鼠标时,将绘制的图形保存到shapes中
switch(type){
//绘制直线
caseLINE:
Lineline=newLine(beginX,beginY,currentX,currentY,color);
currentShapes.push(line);
break;
//绘制圆形
caseCIRCLE:
//半径为a*a+b*b的平方根
intradius=(int)Math
.sqrt((beginX-currentX)*(beginX-currentX)+(beginY-currentY)*(beginY-currentY));
Circlecircle=newCircle(beginX-radius/2,beginY-radius/2,radius,color);
currentShapes.push(circle);
break;
//绘制矩形
caseRECTANGLE:
Rectanglerectangle=null;
if(currentX<beginX){
if(currentY<beginY){
rectangle=newRectangle(currentX,currentY,beginX-currentX,beginY-currentY,color);
}else{
rectangle=newRectangle(currentX,beginY,beginX-currentX,currentY-beginY,color);
}
}else{
if(currentY<beginY){
rectangle=newRectangle(beginX,currentY,currentX-beginX,beginY-currentY,color);
}else{
rectangle=newRectangle(beginX,beginY,currentX-beginX,currentY-beginY,color);
}
}
currentShapes.push(rectangle);
break;
}
repaint();
}
@Override
publicvoidmouseDragged(MouseEvente){
currentX=e.getX();
currentY=e.getY();
this.repaint();
}
@Override
publicvoidmouseMoved(MouseEvente){
}
}
{
=-7080053971741609904L;
=newJPanel();//存放命令的面板
privatefinalJPanelcolorPanel=newJPanel();//存放颜色的面板
privatefinalJPanelmainPanel=newJPanel();//主面板
privatefinalJButtonredButton=newJButton("红色");
privatefinalJButtonblueButton=newJButton("蓝色");
=newJButton("绿色");
privatefinalJButtonlineButton=newJButton("直线");
=newJButton("圆");
=newJButton("矩形");
privatefinalJButtonundoButton=newJButton("撤销");
privatefinalJButtonredoButton=newJButton("恢复撤销");
privatefinalJButtonexitButton=newJButton("退出");
SketchpadPanelsketchPanel=newSketchpadPanel();
privatevoidinitFrame(){
commandPanel.setLayout(newFlowLayout());
commandPanel.add(lineButton);
commandPanel.add(circleButton);
commandPanel.add(rectangleButton);
commandPanel.add(undoButton);
commandPanel.add(redoButton);
commandPanel.add(exitButton);
colorPanel.setLayout(newFlowLayout());
colorPanel.add(redButton);
colorPanel.add(blueButton);
colorPanel.add(greenButton);
mainPanel.setLayout(newBorderLayout());
mainPanel.add(commandPanel,BorderLayout.NORTH);
mainPanel.add(colorPanel,BorderLayout.CENTER);
getContentPane().add("South",mainPanel);
getContentPane().add("Center",sketchPanel);
//初始化设置:颜色和命令
lineButton.setForeground(Color.RED);
sketchPanel.setColor(Color.RED);
redButton.setForeground(Color.RED);
sketchPanel.setShapeType(ShapeTypes.LINE);
}
privatevoidinitListener(){
redButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
redAction(e);
}
});
blueButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
blueAction(e);
}
});
greenButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
greenAction(e);
}
});
undoButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
undoAction(e);
}
});
redoButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
redoAction(e);
}
});
exitButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
exitAction(e);
}
});
lineButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
lineAction(e);
}
});
circleButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
circleAction(e);
}
});
rectangleButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
rectangleAction(e);
}
});
}
publicSketchpadFrame(){
initFrame();
initListener();
this.setSize(500,600);
setLocationByPlatform(true);
setResizable(true);
}
/*********************处理事件**********************/
privatevoindoAction(ActionEvente){
sketchPanel.undo();
}
privatevoidredoAction(ActionEvente){
sketchPanel.redo();
}
privatevoidexitAction(ActionEvente){
System.exit(0);
}
privatevoidlineAction(ActionEvente){
//选中按钮为红色,其余为黑色
lineButton.setForeground(Color.RED);
circleButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.LINE);
}
privatevoidcircleAction(ActionEvente){
circleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.CIRCLE);
}
privatevoidrectangleAction(ActionEvente){
rectangleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
circleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.RECTANGLE);
}
privatevoidredAction(ActionEvente){
redButton.setForeground(Color.RED);
blueButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.RED);
}
privatevoidblueAction(ActionEvente){
blueButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.BLUE);
}
privatevoidgreenAction(ActionEvente){
greenButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
blueButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.GREEN);
}
}
/**
*
*@author不落的太阳(SeanYang)
*@version1.0
*@sinceJDK1.8
*
*/
publicclassSketchpadMain{
/**
*测试方法
*
*@paramargs命令行参数
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
@Override
publicvoidrun(){
JFrameframe=newSketchpadFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
⑤ java Graphics 画了很多线,如何鼠标点击选中其中一根线
把线或者点用容器保存下来,然后绘制,点击鼠标的时候遍历容器,比较鼠标点击点,确定选中的点或线,绘制的时候改变这条线的颜色,达到选中的效果
⑥ JAVA也可以用于图像的设计吗
是的,Java可以用于图像的设计。Java提供了许多图形库和工具,可以用于创建和处理图像。以下是一些常用的Java图形库和工具:搭衡
1. Java 2D API:Java 2D API提供了一组用于创建和处理2D图形的类和接口,包括绘图、图像处理、颜色管理和字体等方面的功能。
2. JavaFX:JavaFX是一个用于创建富客户端应用程序的凯困框架,包括图形、媒体、界面和动画等方面的功能。JavaFX提供了一组用于创建和处理图像的类和接口,可以用于创建各种类型的图像和动画效果。
3. AWT:Abstract Window Toolkit(AWT)是Java的原始图形库,提供了一组用于创建和处理图形用户界面(GUI)的类和接口。AWT包括一些基本的绘图和事件处理功能,但是相对于Java 2D API和JavaFX,其功能较为有限。
4. Java图像处理库(Java Image Processing Library,JIPL):JIPL是一个开源的Java图像处理库,提供了一组用于处理和编辑图像的类和方法,包括图像过滤器盯枝念、噪声消除、边缘检测、图像变换和颜色空间转换等方面的功能。
总之,Java可以用于图像的设计,开发人员可以根据具体需求选择适合的图形库和工具。