使用paint或repaint方法在小程序JAVA上绘制线条 [英] Using the paint or repaint method to paint lines over an applet JAVA

查看:30
本文介绍了使用paint或repaint方法在小程序JAVA上绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在小程序上绘制线条.我正在从外部源加载小程序,但我想在屏幕上的光标所在位置绘制线条.

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.

谁能告诉我该怎么做?

这是一个例子.

  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.

说明这一点的小例子

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.

这篇关于使用paint或repaint方法在小程序JAVA上绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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