📄 graphics4.java
字号:
//<applet code="Graphics4" width=450 height=550></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
class Mycanvas extends Canvas
{
int x1,x2,y1,y2,model,tempx,tempy;
Color mycolor;
Mycanvas()
{
setSize(420,420);
setBackground(Color.pink);
}
public void setX1(int x1)
{this.x1=x1;
}
public void setY1(int y1)
{this.y1=y1;
}
public void setX2(int x2)
{this.x2=x2;
}
public void setY2(int y2)
{this.y2=y2;
}
public void setColor(Color mycolor)
{this.mycolor=mycolor;
}
public void setModel(int model) //作图类型
{this.model=model;
}
//算法
public void paint(Graphics g)
{
Color c1=new Color(255,255,240);
setBackground(c1);
g.setColor(mycolor);
g.drawLine(x1,420-y1,x2,420-y2);
//做线宽5的直线—正方形刷子
if (model==1)
{
float dx,dy,k;
dx=x2-x1;
dy=y2-y1;
k=dy/dx;
if (Math.abs(k)<=1) //|k|<=1
{ g.setColor(mycolor);
int x;
if (x1>x2) //保证第2个大于第一个点
{
tempx=x2;
x2=x1;
x1=tempx;
tempy=y2;
y2=y1;
y1=tempy;
}
float y=y1;
for(x=x1;x<=x2;x++)
{
g.fillRect(x-5,415-(int)Math.round(y+0.5),10,10);
y=y+k;
}
}
else //|k|>1
{ g.setColor(mycolor);
int y;
if (y1>y2) //保证第2个大于第一个点
{
tempx=x2;
x2=x1;
x1=tempx;
tempy=y2;
y2=y1;
y1=tempy;
}
float x=x1;
for(y=y1;y<=y2;y++)
{
g.fillRect((int)Math.round(x+0.5)-5,415-y,10,10);
x=x+1/k;
}
}
}
//做线宽5的直线—圆形刷子
if (model==2)
{
float dx,dy,k;
dx=x2-x1;
dy=y2-y1;
k=dy/dx;
if (Math.abs(k)<=1) //|k|<=1
{ g.setColor(mycolor);
int x;
if (x1>x2) //保证第2个大于第一个点
{
tempx=x2;
x2=x1;
x1=tempx;
tempy=y2;
y2=y1;
y1=tempy;
}
float y=y1;
for(x=x1;x<=x2;x++)
{
g.fillOval(x-5,415-(int)Math.round(y+0.5),10,10);
y=y+k;
}
}
else //|k|>1
{ g.setColor(mycolor);
int y;
if (y1>y2) //保证第2个大于第一个点
{
tempx=x2;
x2=x1;
x1=tempx;
tempy=y2;
y2=y1;
y1=tempy;
}
float x=x1;
for(y=y1;y<=y2;y++)
{
g.fillOval((int)Math.round(x+0.5)-5,415-y,10,10);
x=x+1/k;
}
}
}
}
}
public class Graphics4 extends Applet implements ActionListener
{
Label lab1;
Label labx1,laby1,labx2,laby2;
TextField inputx1,inputy1,inputx2,inputy2;
Choice mychoice,colorchoice;
Button button1;
Mycanvas canvas;
public void init()
{
labx1=new Label("第1端点的X坐标:");
laby1=new Label("第1端点的y坐标:");
labx2=new Label("第2端点的X坐标:");
laby2=new Label("第2端点的y坐标:");
lab1=new Label("请选择填充的颜色:");
inputx1=new TextField("0",10);
inputy1=new TextField("0",10);
inputx2=new TextField("0",10);
inputy2=new TextField("0",10);
mychoice=new Choice();
mychoice.addItem("做直线");
mychoice.addItem("做线宽10的直线—正方形刷子");
mychoice.addItem("做线宽10的直线—圆形刷子");
colorchoice=new Choice();
colorchoice.addItem("黑色");
colorchoice.addItem("红色");
colorchoice.addItem("绿色");
colorchoice.addItem("蓝色");
button1=new Button("作图");
canvas=new Mycanvas();
add(labx1);
add(inputx1);
add(laby1);
add(inputy1);
add(labx2);
add(inputx2);
add(laby2);
add(inputy2);
add(mychoice);
add(colorchoice);
add(button1);
add(canvas);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int x1,x2,y1,y2,model;
Color mycolor=Color.black;
model=0;
try{
x1=Integer.parseInt(inputx1.getText());
y1=Integer.parseInt(inputy1.getText());
x2=Integer.parseInt(inputx2.getText());
y2=Integer.parseInt(inputy2.getText());
if (mychoice.getSelectedIndex()==0)
{model=0;
}
if (mychoice.getSelectedIndex()==1)
{model=1;
}
if (mychoice.getSelectedIndex()==2)
{model=2;
}
if (colorchoice.getSelectedIndex()==0)
{mycolor=Color.black;
}
if (colorchoice.getSelectedIndex()==1)
{mycolor=Color.red;
}
if (colorchoice.getSelectedIndex()==2)
{mycolor=Color.green;
}
if (colorchoice.getSelectedIndex()==3)
{mycolor=Color.blue;
}
canvas.setX1(x1);
canvas.setY1(y1);
canvas.setX2(x2);
canvas.setY2(y2);
canvas.setModel(model);
canvas.setColor(mycolor);
canvas.repaint();
}
catch(NumberFormatException ee)
{x1=0;y1=0;x2=0;y2=0;model=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -