下面是我以前做的小程序,希望能给你点帮助!
package menu;
import javaawt;
import javaawtevent;
import javaxswing;
public class DaYin extends JFrame implements ActionListener{
public DaYin() {
Container c=thisgetContentPane();
csetLayout(new BorderLayout());
JMenuBar jmb=new JMenuBar();
JMenu jm=null;
jm=new JMenu("文件");
jmbadd(jm);
jm=new JMenu("编辑");
jmbadd(jm);
jm=new JMenu("打印");
JMenuItem jmi=new JMenuItem("打印预览");
jmiaddActionListener(this);
jmadd(jmi);
jmbadd(jm);
jm=new JMenu("帮助");
jmbadd(jm);
thissetJMenuBar(jmb);
thissetSize(1024,768);
thissetVisible(true);
thisaddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Systemexit(0);
}
});
}
public static void main(String[] args) {
new DaYin();
}
public void update(Graphics g){
gsetColor(Colorblack);
String title[]={"摘要","科目","子目或户名","借方金额","贷方金额"};
String danwei[]={"千","百","十","万","千","百","十","元","角","分"};
int csx=200;//打印x的初始坐标
int csy=200;//打印y的初始坐标
int x=csx;
int y=csy;
int a=x+20;//打印字体x的初始坐标
int b=y+20;//打印字体y的初始坐标
int row=7;//一共打印出几条横线
int cul=5;//一共打印出几条竖线
int hangju=30;//线与线之间的纵向间距
int lieju=100;//线与线之间的横向间距
int yy=hangjurow+y;
int c=yy+20;
gsetFont(new Font("行楷",FontTYPE1_FONT,24));
gdrawString("记 账 凭 证", x+160, y-40);//打印出记账凭证这几个字
gdrawLine(x+100, y-30, x+340, y-30);//打印出记账凭证下面的那条线
gsetFont(new Font("行楷",FontTYPE1_FONT,13));
gdrawString("年 月 日 第_______号", x+260, y-10);
//gdrawLine(x+430, y-10, x+470, y-10);
for(int i=0;i<=row;i++){
gdrawLine(x, y, cullieju+x+25, y);//此循环用于画出表格的行
if(i==0)
y+=hangju2;
else
y+=hangju;
}
y=csy;
gdrawLine(x+lieju3, y+hangju, cullieju+x, y+hangju);//打印出第一行半的横线
for(int j=0;j<=cul;j++){
gdrawLine(x, y, x, rowhangju+y);//此循环用于画出表格的列
x+=lieju;
}
x=csx;
for(int z=0;z<cul;z++){
gsetColor(Colorred);//设置颜色
gsetFont(new Font("",FontTYPE1_FONT,13));//设置表头字体
if(z<3)
gdrawString(title[z], a, b+hangju/2);
else
gdrawString(title[z], a, b);//为表格添加表头
a+=lieju;
}//76-89打印出一个表格形状
int xx=lieju3+x+10;
for(int v=0;v<=9;v++){
gdrawLine(xx, y+hangju, xx, rowhangju+y+hangju);//此循环用于画出表格的列
gsetFont(new Font("",FontTYPE1_FONT,11));
gdrawString(danwei[v], xx-10, y+hangju+20);//为表格添加表头
if(v%2==0)
gsetColor(Colorred);//设置颜色
else
gsetColor(ColorBLUE);//设置颜色
xx+=10;
}
for(int n=0;n<9;n++){
gdrawLine(xx, y+hangju, xx, rowhangju+y+hangju);//此循环用于画出表格的列
gdrawString(danwei[n],xx-10, y+hangju+20);//为表格添加表头
if(n%2==0)
gsetColor(Colorred);//设置颜色
else
gsetColor(ColorBLUE);//设置颜色
xx+=10;
}
gdrawString(danwei[9],xx-10, y+hangju+20);
gsetColor(Colorblack);//设置颜色
for(int i=0;i<=1;i++){
gdrawLine(x,yy , cullieju+x, yy);//此循环用于画出表格的行
yy+=hangju;
}
yy=hangjurow+y;
for(int j=0;j<=3;j++){
gdrawLine(x, yy, x, (row+1)hangju+y);//此循环用于画出表格的列
if(j>0){
x+=lieju;
}else{
x+=lieju3;
}
}//112-128打印出表格的最后一行单元格
x=csx;
gsetFont(new Font("",FontTYPE1_FONT,13));
gdrawString("合 计",x+60, c);
gdrawString("会计主管 记账 出纳 审核 制证", x+30, yy+50);
gdrawLine(cullieju+x+25, y, cullieju+x+25, (row+1)hangju+y);//打印出最后一列
gdrawString("记",cullieju+x+6, y+15);
gdrawString("账", cullieju+x+6, y+35);
gdrawString("√", cullieju+x+6, y+55);//134-136为最后一列写入字体
gdrawString("附",cullieju+x+35, y+75);
gdrawString("件", cullieju+x+35, y+95);
gdrawString("张", cullieju+x+35, y+175);//134-136为最后一列后写入字体
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(egetActionCommand()equals("打印预览")){
thisrepaint();
}
}
}
使用drawLine()做了一个记账凭证。
执行点击"打印"子菜单"打印预览"就可显示出我们所画的"记账凭证表"
希望这个小程序能够给你点灵感
呵呵,哥们,我们也刚学完这些,以下代码给你,要的基本符合,不过你得要在加2个资源(在insert的resource中)“鼠标的(cursor)和菜单栏的(menu)”,不加是不行的,加了后该修改的你能看的懂的,实在不行,我就直接发文件给你:
#include <windowsh>
#include "resourceh"
#include <stringh>
#include <stdioh>
#include <mathh>
HINSTANCE g_hInstance;
int g_nToolID = 0;
struct tagLine
{
POINT ptB;
POINT ptE;
bool bIsDelete;
};
tagLine g_AllLines[100];//直线数组
int g_nLineNumber = 0; //实际直线数
struct tagCircle
{
POINT ptCenter;
int nRadius;
bool bIsDelete;
};
tagCircle g_AllCircles[100];//整圆数组
int g_nCircleNumber = 0; // 实际保存的圆个数
POINT pt1 = {200,200};
POINT pt2 = {200,200};
POINT pt3 = {0,0};
int g_nState = 0;
//功能:画一线直线段
void DrawLine(HDC hDC,POINT ptB,POINT ptE)
{
::MoveToEx(hDC,ptBx,ptBy,NULL);
::LineTo(hDC,ptEx,ptEy);
}
//功能:已知两点求距离
int Distance(POINT pt1,POINT pt2)
{
int nR = 0;
nR=sqrt((pt1x - pt2x)(pt1x - pt2x) +(pt1y - pt2y)(pt1y- pt2y));
return nR;
}
void DrawCircle(HDC hDC,POINT ptC,int nR)
{
::Ellipse(hDC,ptCx - nR,ptCy - nR,ptCx + nR,ptCy + nR);
}
void OnMouseMove(POINT ptPos,HWND hWnd)
{
if(g_nToolID == ID_CIRCLE)
{
if(g_nState == 1)
{
HDC hDC = ::GetDC(hWnd);
//取得系统预定义一个“空心刷子”
HBRUSH hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
::SelectObject(hDC,hBrush);
::SetROP2(hDC,R2_NOTXORPEN);
DrawLine(hDC,pt1,pt2);
int nR = Distance(pt1,pt2);
DrawCircle(hDC,pt1,nR);
pt3 = ptPos;
nR = Distance(pt1,pt3);
DrawCircle(hDC,pt1,nR);
DrawLine(hDC,pt1,pt3);
pt2 = pt3;
::ReleaseDC(hWnd,hDC);
}
}else if(g_nToolID == ID_LINE)
{
if(g_nState == 1)
{
HDC hDC = ::GetDC(hWnd);
pt3=ptPos;
//用背景色,绘制上一次的位置==>擦除
::SetROP2(hDC,R2_NOTXORPEN);
::DrawLine(hDC,pt1,pt2);
::DrawLine(hDC,pt1,pt3);
pt2 = pt3;
::ReleaseDC(hWnd,hDC);
}
}
}
void OnLButtonUp(POINT ptPos)
{
if(g_nToolID == ID_CIRCLE)
{
if(g_nState == 1)
{
tagCircle aCir;
aCirptCenter = pt1;
aCirnRadius = Distance(pt1,ptPos);
aCirbIsDelete = false;
g_AllCircles[g_nCircleNumber] = aCir;
g_nCircleNumber = g_nCircleNumber + 1;
g_nState = 0;
}
}else if(g_nToolID == ID_LINE)
{
if(g_nState == 1)
{
g_nState = 0;
tagLine aLine;
aLineptB = pt1;
aLineptE = pt3;
aLinebIsDelete = false;
g_AllLines[g_nLineNumber] = aLine;
g_nLineNumber = g_nLineNumber + 1;
}
}
}
//功能:判断点是否在直线段上
bool PtOnCircle(POINT pt,POINT ptC,int nR)
{
int nL = Distance(pt,ptC);
if( fabs(nL - nR) < 5 )
{
return true;
}else
{
return false;
}
}
//功能:判断点是否在直线段上
bool PtOnLine(POINT pt,POINT ptB,POINT ptE)
{
int nL1 = Distance(pt,ptB);
int nL2 = Distance(pt,ptE);
int nL3 = Distance(ptB,ptE);
if( fabs(( nL1 + nL2 ) - nL3) < 5 )
{
return true;
}else
{
return false;
}
}
void OnLButtonDown(HWND hWnd,POINT ptPos)
{
if(g_nToolID == ID_CIRCLE)
{
if(g_nState == 0)
{
g_nState = 1;
pt2 = pt1 = ptPos;
}
}else if(g_nToolID == ID_LINE)
{
if(g_nState == 0)
{
g_nState = 1;
pt2 = pt1 = ptPos;
}
}else if(g_nToolID == ID_DELETE)
{
//判断当前鼠标点是否在某一个直线段上
for(int nI = 0; nI < g_nLineNumber; nI = nI + 1)
{
tagLine pLine = &g_AllLines[nI];
if(PtOnLine(ptPos,(pLine)ptB,(pLine)ptE))
{
char cTxt[100];
sprintf(cTxt,"nI = %d\n",nI);
::OutputDebugString(cTxt);
(pLine)bIsDelete = true;
break;
}
}
//判断当前鼠标点是否在某一个圆上
for(nI = 0; nI < g_nCircleNumber; nI = nI + 1)
{
tagCircle pCircle = &g_AllCircles[nI];
if(PtOnCircle(ptPos,(pCircle)ptCenter,(pCircle)nRadius))
{
(pCircle)bIsDelete = true;
break;
}
}
::InvalidateRect(hWnd,NULL,TRUE);
}
}
void OnPaint(HDC hDC)
{
// 1重绘圆
//取得系统预定义一个“空心刷子”
HBRUSH hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
::SelectObject(hDC,hBrush);
for(int nI = 0; nI < g_nCircleNumber; nI++)
{
tagCircle aCir;
aCir = g_AllCircles[nI];
if(aCirbIsDelete == false)
{
DrawCircle(hDC,aCirptCenter,aCirnRadius);
}
}
// 2重绘直线段
for(nI = 0; nI < g_nLineNumber; nI = nI + 1)
{
tagLine aLine = g_AllLines[nI];
if(aLinebIsDelete == false)
{
::DrawLine(hDC,aLineptB,aLineptE);
}
}
}
void OnCommand(HWND hWnd,int nCmdID)
{
switch(nCmdID)
{
case ID_EXIT:
::DestroyWindow(hWnd);
break;
case ID_LINE:
g_nToolID = ID_LINE;
break;
case ID_CIRCLE:
g_nToolID = ID_CIRCLE;
break;
case ID_DELETE:
g_nToolID = ID_DELETE;
break;
}
}
// 窗口回调函数
int WndProc(HWND hWnd,
int message,
int wParam,
int lParam)
{
switch (message)
{
case WM_SETCURSOR:
{
HCURSOR hCur1 = ::LoadCursor(g_hInstance,"pen_cursor");
HCURSOR hCur2 = ::LoadCursor(g_hInstance,"hand_cursor");
HCURSOR hCur3 = ::LoadCursor(g_hInstance,"point_cursor");
HCURSOR hCur4 = ::LoadCursor(g_hInstance,"arrow_cursor");
switch(g_nToolID)
{
case ID_LINE:
::SetCursor(hCur1);
break;
case ID_CIRCLE:
::SetCursor(hCur2);
break;
case ID_DELETE:
::SetCursor(hCur3);
break;
default:
::SetCursor(hCur1);
}
break;
}
case WM_COMMAND:
{
int nID = LOWORD(wParam);
OnCommand(hWnd,nID);
break;
}
case WM_LBUTTONDOWN:
{
POINT ptPos;
ptPosx = LOWORD(lParam);
ptPosy = HIWORD(lParam);
OnLButtonDown(hWnd,ptPos);
}
break;
case WM_LBUTTONUP:
{
POINT ptPos;
ptPosx = LOWORD(lParam);
ptPosy = HIWORD(lParam);
OnLButtonUp(ptPos);
}
break;
case WM_MOUSEMOVE:
{
POINT ptPos;
ptPosx = LOWORD(lParam);
ptPosy = HIWORD(lParam);
OnMouseMove(ptPos,hWnd);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = ::BeginPaint(hWnd,&ps);
OnPaint(hDC);
::EndPaint(hWnd,&ps);
break;
}
case WM_DESTROY: //窗口关闭时发送
::PostQuitMessage(0); //在消息队列的尾部添加一个0消息,以示,结束程序
break;
default:
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
char lpCmdLine,
int nCmdShow)
{
g_hInstance = hInstance;
WNDCLASSEX wcex;
wcexcbSize = sizeof(WNDCLASSEX);
wcexstyle = CS_HREDRAW | CS_VREDRAW;
wcexlpfnWndProc = (WNDPROC)WndProc;
wcexcbClsExtra = 0;
wcexcbWndExtra = 0;
wcexhInstance = hInstance;
wcexhIcon = NULL;
wcexhCursor = LoadCursor(NULL, IDC_ARROW);
wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcexlpszMenuName = "menu1";
wcexlpszClassName = "ABCDE";
wcexhIconSm = NULL;
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow("ABCDE",
"WanJie",
WS_OVERLAPPEDWINDOW,
50, 50, //(x,y)
500, 500, // (w,h)
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd,SW_SHOW);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return 1;
}
在微信上添加文字有以下八个步骤:
一、找到一个发给别人的或是别人发给你的,如下图所示。
二、点击该后进入如下界面,可以看到该的详细图案。
三、在该的中间位置长按d出以下选项,点击编辑按钮。
四、点击进入之后可以看到最下面有一排图标,点击最中间的“T”。
五、点击后进入如下界面,我输入了“添加文字”四个字,你可以按需输入你想输入的文字,截图中间位置可以选择字的颜色,这里我未做选择。
六、点击左上角的“完成”按钮可以进入如下界面。
七、如果不想把文字放在正中的位置,可以按住文字拖动到你想要放的位置。如果不想添加文字了可以将其拖到最下方的“拖动到此处删除”,将其删除。也可以双击该文字修改文字内容。
八、一切想做的修改都完成后可以点击右上角的“完成”按钮,进入如下界面。可以发送给朋友、收藏或是保存到手机相簿。
工具
画图工具cgcontextref;
实现方法
第一种:将画笔设置为背景色来实现橡皮擦功能。
这种情况适用于当前设置context的blend mode为normal的时候,此时设置背景色的代码如下:
第二种:通过clearColor来实现橡皮擦功能。
第二种方式更加简单方面且适用性更加广泛,第一种情况如果背景色可以变化的情况下,则会有问题。第二种方式因此更具有通用性。此时再设置画笔颜色为clearColor就可以清除画笔:
注意
设置blend mode为copy的情况下,如果画笔同时支持透明度的调整,则会出现一些小问题,需要注意。
以上就是关于用Java一个简单的画图程序全部的内容,包括:用Java一个简单的画图程序、帮我写个画图程序,用win32 API,实现画圆,画矩形等,用到控件 ,鼠标消息处理等。、如何在图片上插入文字等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)