import javaawt;
import javaawtevent;
import javaawtgeomPoint2D;
import javaawtimageBufferedImage;
import javaxswing;
import javaxswingborderBorder;
public class DrawShapes extends JFrame{
private static final long serialVersionUID = 3685715844170953607L;
//作图面板
private CVS cvs;
//作图方式选择按钮
private JToggleButton cycle,line,rect;
private JLabel red,blue,green;
private ButtonGroup bgroup;
//按钮布局面板
private JPanel menuPanel;
//复位按钮
private JButton clean;
public DrawShapes(){
super("DrawShape!");
// thissetResizable(false);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetSize(640,480);
thissetLocationRelativeTo(null);
thisgetContentPane()add(cvs=new CVS());
thisinitComponents();
}
//初始化组件
private void initComponents() {
thismenuPanel = new JPanel(null);
thismenuPanelsetPreferredSize(new Dimension(400,28));
thismenuPanelsetBorder(BorderFactorycreateLineBorder(Colorgray));
thisgetContentPane()add(thismenuPanel,"South");
thisbgroup = new ButtonGroup();
thiscycle = new JToggleButton("圆形");
thiscyclesetBounds(20,4,68,20);
thisline = new JToggleButton("直线");
thislinesetBounds(100,4,68,20);
thisrect=new JToggleButton("矩形");
thisrectsetBounds(180,4,68,20);
red = new JLabel();
redsetOpaque(true);
redsetBackground(Colorred);
redsetBounds(258,4,20,20);
blue=new JLabel();
bluesetOpaque(true);
bluesetBackground(Colorblue);
bluesetBounds(282,4,20,20);
green=new JLabel();
greensetBackground(Colorgreen);
greensetOpaque(true);
greensetBounds(306,4,20,20);
thismenuPaneladd(red);
thismenuPaneladd(blue);
thismenuPaneladd(green);
thismenuPaneladd(cycle);
thismenuPaneladd(line);
thismenuPaneladd(rect);
thisbgroupadd(cycle);
thisbgroupadd(line);
thisbgroupadd(rect);
thisclean = new JButton("清除");
thiscleansetBounds(338,4,68,20);
thismenuPaneladd(clean);
//按钮动作侦听器实例
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e) {
Object src = egetSource();
if(srcequals(cycle))
cvssetSX(SCYCLE);//画圆
else if(srcequals(line))
cvssetSX(SLINE);//画直线
else if(srcequals(rect))
cvssetSX(SRECTANGLE);//画矩形
else if(srcequals(clean))
cvsreset();//复位
}
};
//给按钮注册侦听器
thiscycleaddActionListener(al);
thislineaddActionListener(al);
thisrectaddActionListener(al);
thiscleanaddActionListener(al);
MouseListener ml = new MouseAdapter(){
public void mousePressed(MouseEvent e){
Object s = egetSource();
Border b = BorderFactorycreateLoweredBevelBorder();
if(sequals(red)){
cvssetFC(Colorred);
redsetBorder(b);
bluesetBorder(null);
greensetBorder(null);
}
else if(sequals(blue)){
cvssetFC(Colorblue);
bluesetBorder(b);
redsetBorder(null);
greensetBorder(null);
}
else if(sequals(green)){
cvssetFC(Colorgreen);
greensetBorder(b);
bluesetBorder(null);
redsetBorder(null);
}
}
};
blueaddMouseListener(ml);
redaddMouseListener(ml);
greenaddMouseListener(ml);
}
//程序入口
public static void main(String[] aregs) throws Exception{
//调用系统视觉设置
UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());
SwingUtilitiesinvokeLater(new Runnable(){
public void run(){
new DrawShapes()setVisible(true);
}
});
}
}
//主作图面板类,可接收一般鼠标事件和鼠标拖动事件
class CVS extends Canvas implements MouseListener,MouseMotionListener{
private static final long serialVersionUID = 5585026114112579483L;
//指定图形的第一点,比如矩形的角点,圆形的圆心,直线的第一点
private int x,y;
//作图大小
private int w,h;
//作图类型控制
private S sx = SNULL;
//双缓冲,背景缓冲区和临时缓冲区
private BufferedImage background,buff;
//图像更新标记
private boolean hasChanged;
//背景色,临时作图色,前景色
private Color bgc = Colorwhite;
private Color tmpc = Colorgray;
private Color fgc = Colorblue;
public CVS(){
thisaddMouseListener(this);
thisaddMouseMotionListener(this);
}
//作图控制方式设置
public void setSX(S s){
sx = s;
}
//控制作图颜色:
public void setFC(Color c){
fgc=c;
}
//绘制临时缓冲区
private void drawing(int X,int Y) {
Graphics g = buffgetGraphics();
gsetColor(bgc);
gclearRect(0,0,w,h);
copyBuffer();
gsetColor(tmpc);
switch(sx){
case CYCLE:
Point p = new Point(x,y);
Point2D p2 = new Point2DDouble(X,Y);
int dist = (int)pdistance(p2);
gdrawOval(x,y,0,0);
gdrawOval(x-dist, y-dist,dist2,dist2);
break;
case LINE:
gdrawLine(x,y,X,Y);
break;
case RECTANGLE:
int dx = x-X;
dx = Mathabs(dx);
int dy = y-Y;
dy = Mathabs(dy);
gdrawRect(x<Xx:X,y<Yy:Y,dx,dy);
break;
}
gdispose();
drawBufferToCVS();
}
//绘制背景缓冲区
private void drawBuffer(int X,int Y){
Graphics2D gd = backgroundcreateGraphics();
gdsetColor(fgc);
gdsetRenderingHint(RenderingHintsKEY_ANTIALIASING,RenderingHintsVALUE_ANTIALIAS_ON);
switch(sx){
case CYCLE:
Point p = new Point(x,y);
Point2D p2 = new Point2DDouble(X,Y);
int dist = (int)pdistance(p2);
gddrawOval(x,y,0,0);
gddrawOval(x-dist, y-dist,dist2,dist2);
break;
case LINE:
gddrawLine(x,y,X,Y);
break;
case RECTANGLE:
int dx = x-X;
dx = Mathabs(dx);
int dy = y-Y;
dy = Mathabs(dy);
gddrawRect(x<Xx:X,y<Yy:Y,dx,dy);
break;
default:
popupMsg();
}
gddispose();
Graphics g = thisgetGraphics();
gclearRect(0,0,w,h);
gdrawImage(background,0,0,null);
gdispose();
}
private void popupMsg(){
JOptionPaneshowMessageDialog(this,"请选择一种作图方式开始作图");
}
//复制背景缓冲区内容到临时缓冲区
private void copyBuffer(){
Graphics g = buffgetGraphics();
gdrawImage(background,0,0,null);
gdispose();
}
//印出临时缓冲区内容到窗口
private void drawBufferToCVS() {
Graphics g = thisgetGraphics();
gdrawImage(buff,0,0,null);
gdispose();
}
//清除双缓冲区并清除窗口内容
public void reset(){
clearContents(buff);
clearContents(background);
Graphics g = thisgetGraphics();
gsetColor(bgc);
gfillRect(0,0,w,h);
gdispose();
}
//清除指定缓冲区内容
private void clearContents(BufferedImage i){
Graphics g = igetGraphics();
gsetColor(bgc);
gfillRect(0,0,w,h);
gdispose();
}
//初始化缓冲区
private void initImages() {
background=new BufferedImage(w,h,BufferedImageTYPE_INT_RGB);
buff=new BufferedImage(w,h,BufferedImageTYPE_INT_RGB);
clearContents(background);
clearContents(buff);
}
//重写此方法,以便在窗口重新显示时能正确显示最后的内容
public void paint(Graphics g){
// w = thisgetWidth();
// h = thisgetHeight();
if(background==null){
w = thisgetToolkit()getScreenSize()width;
h = thisgetToolkit()getScreenSize()height;
initImages();
}
gdrawImage(background,0,0,null);
gdispose();
}
//双击作图区也能做清除动作,呵呵
public void mouseClicked(MouseEvent e) {
if(egetClickCount()==2)
reset();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
//每当按下鼠标时就重新设置第一点的坐标
public void mousePressed(MouseEvent e) {
thisx=egetX();
thisy=egetY();
}
//每当释放鼠标时就决定是否需要将背景缓冲区印出到窗口
public void mouseReleased(MouseEvent e) {
if(hasChanged){
hasChanged=false;
drawBuffer(egetX(),egetY());
}
}
//每当鼠标拖动时开始作图
public void mouseDragged(MouseEvent e) {
hasChanged=true;
drawing(egetX(),egetY());
}
public void mouseMoved(MouseEvent e) {}
}
//作图类型的枚举
enum S{
CYCLE,RECTANGLE,LINE,NULL
}
///:~ end
看看这个满意不
如果你的轨迹回放报出这个错误,那么你可能和我一样,是在轨迹回放还未结束的时候或者说是还没有到一个点停止的时候就更新了新的位置信息,想让地图重新的渲染并执行。
这问题的根源就是小程序地图并没有销毁地图的方法,所以我们只能通过去掉地图上的点或者线来实现销毁地图的效果,但是这是有一个前提的,因为我们不能直接通过销毁地图来更新数据,所以我们只能在轨迹回放到一个点结束或者是全部回放结束了的时候才能开始更换新的数据,如果在中途就突然的更新掉数据那么就会报上图的错。
所以解决方法就是做一个判断,在轨迹未到下一个点的时候就让他继续走,直到触发到了下一个点,就可以进行数据的更新了。
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:
我想用VC编一个小程序 作用是将一条已知端点的线段均分 求出其分点坐标(包括端点)(端点坐标 份数 需自己输入)C++程序已经写好了(可以运行) 但要将其可视化 有对话框的那种 不只是在命令提示符里那种 我不大会 请指教 怎么编写 大体步骤也可以 谢谢
解析:
vc的代码是:
void CLineDlg::OnPaint() //画一条直线
{
CDC pDC=GetDC();
CPen newpen(PS_DASHDOTDOT,2,(COLORREF) 0);
CPen oldpen=pDC->SelectObject(&newpen);
pDC->MoveTo(x1,y1);//这就是已知直线的起始端点坐标20,20
pDC->LineTo(x2,y2);//终点的坐标100,20
pDC->SelectObject(oldpen);
}
void CLineDlg::OnButton1()
{
CString str;
strFormat("中点的坐标是: %d %d",(x1+x2)/2,(y1+y2)/2);
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}
这其中的x1,x2,y1,y2都是一个整数值,是一个全局变量
在构造函数当中要初始化,也就是要赋值。
原因很简单,你并没有重写jpanel的绘制方法,adda方法根本没有调用过
public class PixelWindow extends JFrame {
public PixelWindow() {
super("PixelColor");
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setBounds(new Rectangle(100, 100, 300, 300));
SetPixel set = new SetPixel();
getContentPane()add(set);
// add(set);
}
public static void main(String[] args) {
PixelWindow setPixel = new PixelWindow();
setPixelsetVisible(true);
}
}
class SetPixel extends JPanel {
Color color;
int x1, x2, y1, y2;
public SetPixel() {
color = Colorblue;
x1 = 10;
x2 = 50;
y1 = 150;
y2 = 150;
repaint();
}
public void paint(Graphics arg0) {
superpaint(arg0);
adda(arg0, x1, x2, y1, y2);
}
void adda(Graphics g, int x1, int x2, int y1, int y2) {
int k;
float x, y, dx, dy;
k = Mathabs(x2 - x1);
if (Mathabs(y2 - y1) > k)
k = Mathabs(y2 - y1);
dx = (float) (x2 - x1) / k;
dy = (float) (y2 - y1) / k;
x = (float) x1;
y = (float) y1;
for (i
以上就是关于谁能用JBuilder2006编一个绘画的小程序全部的内容,包括:谁能用JBuilder2006编一个绘画的小程序、微信小程序map轨迹为什么是直线、用VC编小程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)