Java允许用户绘制线的挑战 [英] Java Challenge on Permitting the User to Draw A Line

查看:152
本文介绍了Java允许用户绘制线的挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题已经在 java绘制线上作为鼠标提及被移动了,但是,我并没有深入到本书中来介绍JPanels,JFrames和Points,正如前面提到的这个问题的程序员所说的那样。

回答这个问题肯定会帮助大多数初学者程序员更好地理解图形类和绘图,这是一个经常复杂的过程,特别是对于初学者。



根据我使用的文本(正如我自己学习Java),这是如何使用Java绘制线条的示例:

  / * 
* LineTest
*演示绘制线
* /
import java.awt 。*;
public class LineTest extends Canvas {
public LineTest(){
super();
setSize(300,200);
setBackground(Color.white);

public static void main(String args []){
LineTest lt = new LineTest();
GUIFrame frame = new GUIFrame(Line Test);
frame.add(lt);
frame.pack();
frame.setVisible(true);

public void paint(Graphics g){
g.drawLine(10,10,50,100);
g.setColor(Color.blue);
g.drawLine(60,110,275,50);
g.setColor(Color.red);
g.drawLine(50,50,300,200);






规格是:


创建一个应用程序,允许您通过单击最初的
点并将鼠标拖到第二个点来绘制线条。应用程序应该重新绘制
,以便您可以在
拖动鼠标时看到换行的大小和位置。当鼠标按钮被释放时,这条线是
绘制的。


正如你会意识到的,运行这个程序不会创建任何用户绘图。我相信由于第21行会遇到这个错误: g.drawLine(x,y,x2,y2); 不正确,因为这是定义行的图形的语句。



非常感谢任何帮助。提前感谢您在这个问题上的所有时间和合作。



我的代码可以回答这个问题:

  import java。 AWT *。 
import java.awt.event。*;

public class LineDrawer extends Canvas
implements MouseListener,MouseMotionListener {
int x,y,x2,y2;

public LineDrawer(){
super();
setSize(300,200);
setBackground(Color.white);
}
public void mouseClicked(MouseEvent me){
int x = me.getX();
int y = me.getY();
int x2 = me.getX();
int y2 = me.getY();

$ b $ public void paint(Graphics g){
g.drawLine(x,y,x2,y2);
g.setColor(Color.blue);
}


public void mousePressed(MouseEvent me){
repaint();
}

public void mouseDragged(MouseEvent me){
}
public void mouseExited(MouseEvent me){
}
public void mouseEntered (MouseEvent me){
}


public void mouseReleased(MouseEvent me){
}
public void mouseMoved(MouseEvent me){


public static void main(String args []){
LineDrawer ld = new LineDrawer();
GUIFrame frame =新GUIFrame(Line Drawer);
frame.add(ld);
frame.pack();
frame.setVisible(true);
}

}

PS:我一直在犹豫寻求帮助,因为我担心其他程序员会用我还没学过的方法回答。

解决方案

  int x1,y1,x2,y2; 

  public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
}

  public void mouseDragged(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
//现在画行
repaint();
}

希望它有帮助。


My question has been alluded to in java draw line as the mouse is moved, however, I have not advanced far enough into this book to have covered JPanels, JFrames and Points as stated by the prior programmer who asked this question.

Answering this question definitely would help most beginner programmers better understand the graphics class and drawing, an often intricate process, especially for beginners.

According to the text I am using (as I am learning Java on my own), this was the example of how to draw a line using Java:

/*
 * LineTest
 * Demonstrates drawing lines
 */
import java.awt.*;
public class LineTest extends Canvas {
public LineTest() {
super();
setSize(300, 200);
setBackground(Color.white);
}
public static void main(String args[]) {
LineTest lt = new LineTest();
GUIFrame frame = new GUIFrame("Line Test");
frame.add(lt);
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g) {
g.drawLine(10, 10, 50, 100);
g.setColor(Color.blue);
g.drawLine(60, 110, 275, 50);
g.setColor(Color.red);
g.drawLine(50, 50, 300, 200);
}
}

The specification is:

Create an application that allows you to draw lines by clicking the initial point and dragging the mouse to the second point. The application should be repainted so that you can see the line changing size and position as you are dragging the mouse. When the mouse button is released, the line is drawn.

As you will recognize, running this program does not create any drawing by the user. I believe this error is encountered due to line 21: g.drawLine(x, y, x2, y2); being incorrect since this is the statement defining the drawing of the line.

Any help is greatly appreciated. Thank you in advance for all your time and cooperation regarding this matter.

My code to answer the question is:

import java.awt.*;
import java.awt.event.*;

public class LineDrawer extends Canvas
                        implements MouseListener, MouseMotionListener {
int x, y, x2, y2;

public LineDrawer() {
    super();
    setSize(300, 200);
    setBackground(Color.white);
}
public void mouseClicked(MouseEvent me) {
                int x = me.getX();
                int y = me.getY();
                int x2 = me.getX();
                int y2 = me.getY();

    }
public void paint(Graphics g) {
            g.drawLine(x, y, x2, y2);
            g.setColor(Color.blue);
    }


    public void mousePressed(MouseEvent me) {
        repaint();
    }

    public void mouseDragged(MouseEvent me) {
    }
    public void mouseExited(MouseEvent me) {
    }
    public void mouseEntered(MouseEvent me) {
    }


    public void mouseReleased(MouseEvent me) {
    }
    public void mouseMoved(MouseEvent me) {
    }

    public static void main(String args[]) {
        LineDrawer ld = new LineDrawer();
        GUIFrame frame = new GUIFrame("Line Drawer");
        frame.add(ld);
        frame.pack();
        frame.setVisible(true);
    }

}

P.S.: I have been hesitant to ask for help since I am concerned that other programmers would answer with methods that I have not yet learned.

解决方案

int x1, y1, x2, y2;

public void mousePressed(MouseEvent e){
    x1 = e.getX();
    y1 = e.getY();
}

public void mouseDragged(MouseEvent e){
    x2 = e.getX();
    y2 = e.getY();
    // Now Paint the line
    repaint();
}

Hope it helps.

这篇关于Java允许用户绘制线的挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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