用java写应用小程序,在网页上输出一条直线
5个回答
2014-09-24
展开全部
package com.jack;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MoveLine extends JFrame{
public MoveLine(){
MyLinePane p = new MyLinePane(10,10,100,100);
p.setSize(400,400);
setContentPane(p);
setSize(500,500);
}
public static void main(String[] args) {
MoveLine ml = new MoveLine();
ml.setVisible(true);
ml.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyLinePane extends JPanel implements MouseListener,MouseMotionListener{
int x1,x2,y1,y2;//当前值
int oldx1,oldy1,oldx2,oldy2;//原始值
boolean selected=false;
boolean move=false;
public MyLinePane(int x1,int y1,int x2,int y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.oldx1=x1;
this.oldx2=x2;
this.oldy1=y1;
this.oldy2=y2;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
if(selected){
g.fillRect(x1,y1,x1+1,y1+1);
g.fillRect(x2,y2,x2+1,y2+1);
}
//此处根据状态,selected,move 编写方法
//每次擦掉原来的线和点,绘制新的线和点
}
//以下几个方法中判断当前的操作,并纪录坐标
public void mouseDragged(MouseEvent e){
}
public void mouseMoved(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
if(e.getButton()==MouseEvent.BUTTON1){
selected=true;
}
this.updateUI();
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MoveLine extends JFrame{
public MoveLine(){
MyLinePane p = new MyLinePane(10,10,100,100);
p.setSize(400,400);
setContentPane(p);
setSize(500,500);
}
public static void main(String[] args) {
MoveLine ml = new MoveLine();
ml.setVisible(true);
ml.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyLinePane extends JPanel implements MouseListener,MouseMotionListener{
int x1,x2,y1,y2;//当前值
int oldx1,oldy1,oldx2,oldy2;//原始值
boolean selected=false;
boolean move=false;
public MyLinePane(int x1,int y1,int x2,int y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.oldx1=x1;
this.oldx2=x2;
this.oldy1=y1;
this.oldy2=y2;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
if(selected){
g.fillRect(x1,y1,x1+1,y1+1);
g.fillRect(x2,y2,x2+1,y2+1);
}
//此处根据状态,selected,move 编写方法
//每次擦掉原来的线和点,绘制新的线和点
}
//以下几个方法中判断当前的操作,并纪录坐标
public void mouseDragged(MouseEvent e){
}
public void mouseMoved(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
if(e.getButton()==MouseEvent.BUTTON1){
selected=true;
}
this.updateUI();
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Test extends Applet
{
/**
*
*/
private static final long serialVersionUID = -6059738190801891376L;
class Point
{
public int x, y;
}
Point p1, p2;
public void start()
{
p1 = new Point();
p1.x = p1.y = 0;
p2 = new Point();
p2.x = p2.y = 100;
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
import java.awt.Color;
import java.awt.Graphics;
public class Test extends Applet
{
/**
*
*/
private static final long serialVersionUID = -6059738190801891376L;
class Point
{
public int x, y;
}
Point p1, p2;
public void start()
{
p1 = new Point();
p1.x = p1.y = 0;
p2 = new Point();
p2.x = p2.y = 100;
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用java写一个applet,然后applet中在print方法中调用Graphics对象的drawLine方法可以画线的。
最后把applet嵌到html中就可以了。
最后把applet嵌到html中就可以了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在前端的html写个直线不就行了,为啥要在后台写
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
out.print("<br/>")
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询