我如何使用外部类的类的Graphics对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何使用外部类的类的Graphics对象相关的知识,希望对你有一定的参考价值。

我正在学习java awt和swing libs.In这个程序我试图模仿MSPaint中的铅笔工具。当我在单个类中进行编程时工作正常但是,当我使用外部类来监听时不起作用我的鼠标运动。我的猜测是我没有得到应用程序的Graphics对象,请告诉我我哪里出错了。谢谢! 这是代码

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Paint extends Canvas {

    Paint() {
        Outer obj=new Outer(this);
        JFrame frame=new JFrame("Paint");
        frame.setSize(400,400);
        frame.add(this);
        frame.addMouseMotionListener(obj);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Paint();
    }
}

class Outer implements MouseMotionListener {

    static int x,y,x1,y1;

    Paint ob;

    Outer(Paint ob) {
        this.ob=ob;
    }

    public void mouseDragged(MouseEvent me) {
        Graphics g=ob.getGraphics();
        x1=me.getX();
        y1=me.getY();
        _paint_(g,x,y,x1,y1);
        x=x1;
        y=y1;
    }

    public void _paint_(Graphics g,int x,int y,int x1,int y1) {
        g.drawLine(x,y,x1,y1);
    }

    public void mouseMoved(MouseEvent me) {
        y=me.getY();
        x=me.getX();
    }
}

由于不起作用,我的意思是框架出现,但“铅笔工具”不绘制线条

答案

如果你想用JavaFX做这个,你可以阅读this文档的教程,其中包含你正在寻找的一个确切的例子。只需下载CanvasDoodleTest.zip并运行它。

如果你想用Java Swing做这个,你可以这样做,一般来说,就像这样:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingCanvas extends JPanel implements MouseListener, MouseMotionListener {
    private final Path2D pencilPath; //This will be the Shape we are going to paint in 'myPaint(...)' method.

    public SwingCanvas() {
        pencilPath = new Path2D.Double(); //Create a new path object, i.e. a set of points and lines between them.

        super.addMouseListener(this); //Register this object as its MouseListener (for the mousePressed event).
        super.addMouseMotionListener(this); //Register this object as its MouseMotionListener (for the mouseDragged event).
        super.setPreferredSize(new Dimension(400, 200)); //Set the preferred size of the component (i.e. the size we want the panel to start with).
        super.setBackground(Color.WHITE); //[Optional] Setting the background color of the panel to white.
        super.setOpaque(true); //Opaque components are responsible to paint their full contents. This is the default behaviour of JPanel, so this is an optional call here.
    }

    /**
     * Overriding paintComponent(...) tells Swing how to paint the panel.
     * @param graphics 
     */
    @Override
    public void paintComponent(final Graphics graphics) {
        super.paintComponent(graphics); //Call this in order to clear previous painting state and start over.
        final Graphics2D g2d = (Graphics2D) graphics.create(); //Create a Graphics2D object to paint.
        try {
            myPaint(g2d); //Call our custom paint method.
        }
        catch (final RuntimeException re) { //Catch any exception in our myPaint (eg a NullPointerException) (if any).
            System.err.println(re); //Handle the exception in some way.
        }
        finally {
            g2d.dispose(); //ALWAYS dispose the CREATED Graphics2D object.
        }
    }

    /**
     * Do whatever painting you need here.
     * In our case, we draw the full path the pencil has followed.
     * @param g2d The {@link Graphics2D} object to paint on.
     */
    protected void myPaint(final Graphics2D g2d) {
        g2d.draw(pencilPath);
    }

    @Override
    public void mousePressed(final MouseEvent e) {
        pencilPath.moveTo(e.getX(), e.getY()); //Move the drawing pencil (without drawing anything new) to the new position.
    }

    @Override
    public void mouseClicked(final MouseEvent e) {
        //Do nothing.
    }

    @Override
    public void mouseReleased(final MouseEvent e) {
        //Do nothing.
    }

    @Override
    public void mouseEntered(final MouseEvent e) {
        //Do nothing.
    }

    @Override
    public void mouseExited(final MouseEvent e) {
        //Do nothing.
    }

    @Override
    public void mouseMoved(final MouseEvent e) {
        //Do nothing.
    }

    @Override
    public void mouseDragged(final MouseEvent e) {
        if (pencilPath.getCurrentPoint() == null) //If the initial point of the path is not set (via a call to "moveTo(...)") then:
            pencilPath.moveTo(e.getX(), e.getY()); //Register the new point from which the new lines will be drawn.
        else
            pencilPath.lineTo(e.getX(), e.getY()); //Register a new line in the path to be drawn.
        repaint(); //Notify Swing to repaint the component (which will call paintComponent(...) which will call myPaint(...) which will paint the pencilPath...
    }

    public static void main(final String[] args)  {
        final JFrame frame = new JFrame("SwingPaint pencil"); //Creates a new application's window.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the new window to close the application if the user closes the window.
        frame.getContentPane().add(new SwingCanvas()); //Add our custom painting panel to the window's content pane.
        frame.pack(); //Automatic sizing of the window, based on preferred sizes of contained components.
        frame.setLocationRelativeTo(null); //Put the window in the center of the screen.
        frame.setVisible(true); //Pop the window.
    }
}

如果需要,您还可以组合这两种方法,因为CanvasComponent(因此可以将其添加到Swing示例中的主框架内容窗格中)。

如果你想从另一个类访问MouseEventSwingCanvass,那么你可以扩展一个MouseAdapter并将其添加到面板中。

以上是关于我如何使用外部类的类的Graphics对象的主要内容,如果未能解决你的问题,请参考以下文章

访问另一个类的 main 中定义的类的实例

如何访问禁用“=”运算符的类的对象?

c++ 使用引用另一个类的类的非成员函数

五. 面向对象高级特性2. 内部类的分类

Delphi的类和对象- 初识类的事件

使用包含在 HTML 中的外部 CSS 类的 PdfSharp 从 HTML 片段创建 PDF