import java.awt.*;
成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、布爾津網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為布爾津等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不規(guī)則圖形的繪制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath對象實例
Point aPoint;
//構(gòu)造函數(shù)
public IrregularShapeDemo() {
super("不規(guī)則圖形的繪制"); //調(diào)用父類構(gòu)造函數(shù)
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允許事件
setSize(300, 200); //設(shè)置窗口尺寸
setVisible(true); //設(shè)置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時退出程序
}
public void paint(Graphics g) { //重載窗口組件的paint()方法
Graphics2D g2D = (Graphics2D)g; //獲取圖形環(huán)境
g2D.draw(gPath); //繪制路徑
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠標事件處理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到當前鼠標點
gPath = new GeneralPath(); //重新實例化GeneralPath對象
gPath.moveTo(aPoint.x,aPoint.y); //設(shè)置路徑點
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠標運動事件處理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到當前鼠標點
gPath.lineTo(aPoint.x, aPoint.y); //設(shè)置路徑
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重繪組件
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class MyLine
{
private int x1,x2,y1,y2;
public MyLine(int x1,int x2,int y1,int y2)
{
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public void drawMe(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
g.drawString("起始坐標:"+new String(x1+","+y1),x1,y1);
}
}
public class DrawLine extends Frame
{
Vector v=new Vector();
public static void main(String[]args)
{
DrawLine dl=new DrawLine();
dl.init();
}
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
Enumeration en=v.elements();
while(en.hasMoreElements())
{
MyLine m=(MyLine)en.nextElement();
m.drawMe(g);
}
}
public void init()
{
setSize(300,300);
setVisible(true);
addMouseListener(new MouseAdapter()
{
int x1,x2,y1,y2;
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Graphics g=e.getComponent().getGraphics();
g.setColor(Color.BLUE);
g.drawLine(x1,y1,x2,y2);
g.drawString(new String("起始點坐標:"+x1+","+y1),x1,y1);
v.add(new MyLine(x1,x2,y1,y2)); // here pay attention
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
((Window)e.getSource()).dispose();
System.exit(0);
}}
);
}
}
/*計算器*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JPanel panel;
JTextField tfShow;/*定義顯示文本框*/
JButton b1[]=new JButton[10]; /*數(shù)字按鈕*/
JButton b2[]=new JButton[6]; /*操作按鈕*/
boolean isNumber;/*判斷是否輸入多位數(shù)字的變量*/
double number;/*存儲輸入數(shù)值、顯示結(jié)果的變量*/
double result;/*存儲中間運算結(jié)果的變量*/
char operator;/*存儲當前操作符的成員變量*/
public Calculator(){
frame=new JFrame("計算器");
frame.setSize(300,300);/*指定框架窗口的大小*/
frame.setResizable(false);/*使框架窗口不可改變大小*/
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(20,20,20,20));/*繪制框架的指定大小的空透明邊框*/
tfShow=new JTextField("0",25);/*指定屬性的文本域*/
tfShow.setHorizontalAlignment(JTextField.RIGHT);/*設(shè)置文本域中文本的對齊方式*/
isNumber=true;/*初始值設(shè)置*/
number=0;/*初始值設(shè)置*/
result=0;/*初始值設(shè)置*/
operator=' ';/*初始值設(shè)置*/
for(int i=0;ib1.length;i++){
b1[i]=new JButton(Integer.toString(i));/*創(chuàng)建數(shù)字按鈕*/
b1[i].setActionCommand(Integer.toString(i));
b1[i].addActionListener(this);
b1[i].setForeground(Color.blue);
}
String bs[]={"/","*","-","C","+","="};
for(int i=0;ib2.length;i++){
b2[i]=new JButton(bs[i]);/*創(chuàng)建操作按鈕*/
b2[i].setActionCommand(bs[i]);
b2[i].addActionListener(this);
b2[i].setForeground(Color.red);
}
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
panel.add(b1[1]);
panel.add(b1[2]);
panel.add(b1[3]);
panel.add(b2[0]);
panel.add(b1[4]);
panel.add(b1[5]);
panel.add(b1[6]);
panel.add(b2[1]);
panel.add(b1[7]);
panel.add(b1[8]);
panel.add(b1[9]);
panel.add(b2[2]);
panel.add(b1[0]);
panel.add(b2[3]);
panel.add(b2[4]);
panel.add(b2[5]);
frame.add(tfShow,BorderLayout.NORTH);/*將文本框放置在框架上方*/
frame.add(panel,BorderLayout.CENTER);/*將裝有按鈕組的panel放在框架的中心*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*設(shè)置框架窗口的默認窗口關(guān)閉操作*/
frame.setVisible(true);/*設(shè)置框架可見*/
}
public double getDisplay(){/*返回要顯示的結(jié)果*/
return number;
}
public void reDisplay(){/*刷新文本域的內(nèi)容*/
tfShow.setText(""+getDisplay());
}
/*對輸入數(shù)字的處理*/
public void numberProcess(int num){
if(isNumbernum!=0){
String s1=Integer.toString(num);
String s2=Integer.toString((int)(this.number));
this.number=Double.parseDouble(s2+s1);/*對多位數(shù)字的處理*/
}else{
this.number=num;
}
isNumber=true;/*輸入連續(xù)數(shù)字(即多位數(shù)字)時為真*/
}?
public void operationProcess(char operator){/*根據(jù)輸入的操作符改變當前操作符*/
switch(operator){
case '-':
this.operator='-';
break;
case '+':
this.operator='+';
break;
case '*':
this.operator='*';
break;
case '/':
this.operator='/';
break;
}
result=number;
isNumber=false;/*輸入操作符時表示輸入連續(xù)數(shù)字的標記變量為假*/
}?
public void clear(){
number=0;
result=0;
}??
public void equal(){/*計算運算結(jié)果*/
switch(operator){
case '-':
result=result-number;
break;
case '+':
result=result+number;
break;
case '*':
result=result*number;
break;
case '/':
result=result/number;
break;
case ' ':
result=number;
break;
}
number=result; /*把運算結(jié)果賦值給顯示變量*/
isNumber=false;
operator=' ';?
}?
public static void main(String args[]){
Calculator cal=new Calculator();/*創(chuàng)建計算器*/
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();/*獲取按鈕激發(fā)的操作事件的命令名稱*/
char c=command.charAt(0);/*將按鈕命令名稱的第一個字符賦值給一個字符c*/
switch(c){
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
int number=Integer.parseInt(command);
numberProcess(number);/*輸入數(shù)字的處理*/
break;
case '+':
case '-':
case '*':
case '/':
operationProcess(c);/*算數(shù)運算符的處理*/
break;
case '=':equal();break;/*計算運算結(jié)果*/
case 'C':clear();break;/*清零*/
}
reDisplay(); /*在文本域中顯示信息*/
}
}
運行截圖:
上代碼:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Paint;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.*;
public class YuanYiDong extends JFrame{
private static int BANJIN=0;
private static int X=0;
private static int Y=0;
JTextField rTxt=new JTextField(5);
JTextField xField=new JTextField(5);
JTextField yField=new JTextField(5);
JButton paintBt=new JButton("畫");
JLabel huaban=new huaban();
JPanel jPanel=new JPanel();
JLabel banjingLabel,xLabel,yLabel;
public YuanYiDong(){
banjingLabel=new JLabel("半徑");
xLabel=new JLabel("X坐標");
yLabel=new JLabel("Y坐標");
this.setTitle("圓的移動");
this.setLocation(300,100);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(rTxt);
jPanel.setLayout(new FlowLayout());
add(huaban,BorderLayout.CENTER);
jPanel.add(banjingLabel);
jPanel.add(rTxt);
jPanel.add(xLabel);
jPanel.add(xField);
jPanel.add(yLabel);
jPanel.add(yField);
jPanel.add(paintBt);
add(jPanel,BorderLayout.NORTH);
paintBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
BANJIN=Integer.parseInt(rTxt.getText());
X=Integer.parseInt(xField.getText());
Y=Integer.parseInt(yField.getText());
huaban.repaint();
}
});
}
private void drawCirlce(Graphics g) {
g.setColor(Color.blue);
g.fillOval(X, Y, BANJIN,BANJIN);
}
public static void main(String[] args) {
YuanYiDong frame = new YuanYiDong();
}
public class huaban extends JLabel{
public huaban(){}
public void paint(Graphics g) {
Image image = createImage(getWidth(), getHeight());
drawCirlce(image.getGraphics());
g.drawImage(image, 0, 0, null);
}
}
}
給分吧!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tester extends Applet implements ActionListener
{
Button btn1,btn2,btn3,btn4;
public void init(){
setLayout(null);
btn1=new Button("繪制直線");
btn2=new Button("繪制矩形");
btn3=new Button("繪制圓");
add(btn1);
add(btn2);
add(btn3);
btn1.setBounds(10, 60, 60, 20);
btn2.setBounds(10, 90, 60, 20);
btn3.setBounds(10, 120, 60, 20);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
validate();
setVisible(true);
}
public void start(){
}
public void actionPerformed(ActionEvent e)
{
Graphics g=this.getGraphics();
if(e.getSource()==btn1)
{
g.drawLine(200, 100, 300, 200);
}
else if(e.getSource()==btn2)
{
g.drawRect(200, 250, 200, 200);
}
else
{
g.drawArc(200, 500, 200, 200, 0, 360);
}
}
}