使用油漆或重新绘制方法来画在一个applet JAVA线 [英] Using the paint or repaint method to paint lines over an applet JAVA

查看:225
本文介绍了使用油漆或重新绘制方法来画在一个applet JAVA线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在一个applet画线。我加载从外部源的小程序,但我喜欢画画线,其中光标在屏幕上。

有人能告诉我怎么会做这个吗?

下面是一个例子。

  g.drawLine(mouse.getLocation()X  -  6,mouse.getLocation()Y,
            mouse.getLocation()X + 6,mouse.getLocation()Y)。
            g.drawLine(mouse.getLocation            ().X,mouse.getLocation()Y - 6,
            。mouse.getLocation()的x,mouse.getLocation()Y + 6);


解决方案

  

我想知道是否有可能在一个applet画线。


当然可以。简单地根据事件把一个面板小程序,添加鼠标移动侦听器,并绘制面板上。

小的例子说明这种

 进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Point中;
进口java.awt.event.MouseAdapter;
进口java.awt.event.MouseEvent中;
进口java.lang.reflect.InvocationTargetException;
进口的java.util.ArrayList;
进口的java.util.List;进口javax.swing.JApplet中;
进口javax.swing.JPanel中;
进口javax.swing.SwingUtilities中;公共类TestAppletDraw扩展JApplet的{    公共静态类MyDrawPanel继承JPanel {        私人列表<点和GT;点=新的ArrayList<点和GT;();        公共MyDrawPanel(){
            的setBackground(Color.WHITE);
            MouseAdapter监听器=新MouseAdapter(){                @覆盖
                公共无效的mouseClicked(的MouseEvent E){
                    points.clear();
                    重绘();
                }                @覆盖
                公共无效的mouseMoved(的MouseEvent E){
                    points.add(e.getPoint());
                    重绘();
                }
            };
            addMouseListener将(监听);
            addMouseMotionListener(监听);
        }        @覆盖
        公共尺寸的get preferredSize(){
            返回新尺寸(300,300);
        }        @覆盖
        保护无效paintComponent(java.awt.Graphics G){
            super.paintComponent方法(G);
            点P1 =无效;
            点P2 =无效;
            g.setColor(Color.BLUE);
            为(P点:点){
                P2 = P1;
                P1 = P;
                如果(P1 = NULL&放大器;!&安培;!P2 = NULL){
                    g.drawLine(p1.x,p1.y,p2.x,p2.y);
                }
            }
        }
    }    保护无效initUI(){
        添加(新MyDrawPanel());
        验证();
    }    @覆盖
    公共无效的init(){
        super.init();
        尝试{
            SwingUtilities.invokeAndWait(新的Runnable(){
                @覆盖
                公共无效的run(){
                    initUI();
                }
            });
        }赶上(的InvocationTargetException E){
            e.printStackTrace();
        }赶上(InterruptedException的E){
            e.printStackTrace();
        }
    }}

NB:使用缓冲图像,而不是存储点可能已经过去的时间(否则积分榜上可以成为巨大的)长期更具扩展性,但​​它需要采取的面板尺寸增加护理

I am wondering if it's possible to paint lines over an applet. I am loading the applet from an external source, but I'd like to paint lines where the cursor is on the screen.

Can someone tell me how I'd do this please?

Here's an example.

  g.drawLine(mouse.getLocation().x - 6, mouse.getLocation().y,
            mouse.getLocation().x + 6, mouse.getLocation().y);
            g.drawLine(mouse.getLocation

            ().x, mouse.getLocation().y - 6,


            mouse.getLocation().x, mouse.getLocation().y + 6);

解决方案

I am wondering if it's possible to paint lines over an applet.

Sure you can. Simply put a panel in the applet, add a mouse motion listener and draw on that panel according to the events.

Small example illustrating this

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestAppletDraw extends JApplet {

    public static class MyDrawPanel extends JPanel {

        private List<Point> points = new ArrayList<Point>();

        public MyDrawPanel() {
            setBackground(Color.WHITE);
            MouseAdapter listener = new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    points.clear();
                    repaint();
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                    points.add(e.getPoint());
                    repaint();
                }
            };
            addMouseListener(listener);
            addMouseMotionListener(listener);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }

        @Override
        protected void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);
            Point p1 = null;
            Point p2 = null;
            g.setColor(Color.BLUE);
            for (Point p : points) {
                p2 = p1;
                p1 = p;
                if (p1 != null && p2 != null) {
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
                }
            }
        }
    }

    protected void initUI() {
        add(new MyDrawPanel());
        validate();
    }

    @Override
    public void init() {
        super.init();
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    initUI();
                }
            });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

NB: Using a buffered image instead of storing points may be more scalable over long periods of time (otherwise the points List can become gigantic) but it requires to take care of panel size increases.

这篇关于使用油漆或重新绘制方法来画在一个applet JAVA线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆