在Java中通过KeyTyped事件绘制两行 [英] draw two line by KeyTyped event in java

查看:155
本文介绍了在Java中通过KeyTyped事件绘制两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我想在键盘上按h和v在面板中绘制水平和垂直轴。我不知道......这是我的代码。如何控制Keytyped事件

  import java.awt.BasicStroke; 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import java.awt.geom.Line2D;

@SuppressWarnings(serial)
公共类DrawAxis扩展JPanel实现KeyListener
{
private Graphics2D g2;
private float x1;
private float y1;
private float x2;
private float y2;

public void DrawingAxis(float X1,float Y1,float X2,float Y2)
{
x1 = X1;
y1 = Y1;
x2 = X2;
y2 = Y2;
}

public void paint(Graphics g)
{
super.paintComponent(g);
g2 =(Graphics2D)g;
g2.setColor(Color.GRAY);
g2.setStroke(new BasicStroke(1));
g2.draw(new Line2D.Float(x1,y1,x2,y2));
}

@Override
public void keyPressed(KeyEvent event){

}

@Override
public void keyReleased(KeyEvent event){

}

@Override
public void keyTyped(KeyEvent event){
char ch = event.getKeyChar( );
switch(ch)
{
case'h':
// ????????
case'v':
// ????????
}
}
}

这是主要代码:

  import javax.swing.JFrame; 

public class Example1
{
public static void main(String [] args){
JFrame frame = new JFrame(Drawing line);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,1000);
DrawAxis dshape = new DrawAxis();
dshape.DrawingAxis(500,0,500,1000);
dshape.DrawingAxis(0,500,1000,500);
dshape.setFocusable(true);
dshape.addKeyListener(dshape);
frame.add(dshape);
frame.setVisible(true);

}

}

我该怎么办?完成此代码?

解决方案

首先在 KeyListener上使用键绑定API ,它将解决与 KeyListener 相关的一些关键问题。有关详细信息,请参阅

  import java.awt.Dimension; 
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Path2D;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

公共类DrawLine {

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

public DrawLine(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}

JFrame frame = new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

公共类TestPane扩展JPanel {

私有Point currentPoint;
私有Path2D形状;

public TestPane(){
currentPoint = new Point(0,0);
shape = new Path2D.Double();
shape.moveTo(0,0);

bindKey(KeyEvent.VK_H,draw.horizo​​ntally,新的AddPointAction(4,0));
bindKey(KeyEvent.VK_V,draw.vertically,新的AddPointAction(0,4));
}

protected void bindKey(int vkKey,String name,Action action){
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();

im.put(KeyStroke.getKeyStroke(vkKey,0),name);
am.put(姓名,行动);
}

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

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g.create();
g2d.draw(形状);
g2d.dispose();
}

公共类AddPointAction扩展AbstractAction {

private int xDelta;
private int yDelta;

public AddPointAction(int xDelta,int yDelta){
this.xDelta = xDelta;
this.yDelta = yDelta;
}

@Override
public void actionPerformed(ActionEvent e){
int x = currentPoint.x + xDelta;
int y = currentPoint.y + yDelta;
shape.lineTo(x,y);
currentPoint.x = x;
currentPoint.y = y;
repaint();
}

}

}

}


hi I want to draw horizontal and vertical axis in a panel by pressing h and v on keyboard . I don't know how ... this is my code . how can I control the Keytyped event

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import java.awt.geom.Line2D;

@SuppressWarnings("serial")
public class DrawAxis extends JPanel implements KeyListener
{   
private Graphics2D g2;
private float x1;
private float y1;
private float x2;
private float y2;

public void DrawingAxis(float X1,float Y1,float X2,float Y2)
{
    x1=X1;
    y1=Y1;
    x2=X2;
    y2=Y2;
}

public void paint(Graphics g)
{
    super.paintComponent(g);
    g2=(Graphics2D) g;
    g2.setColor(Color.GRAY);
    g2.setStroke(new BasicStroke(1));
    g2.draw(new Line2D.Float(x1,y1,x2,y2));
}

@Override
public void keyPressed(KeyEvent event) {

}

@Override
public void keyReleased(KeyEvent event) {

}

@Override
public void keyTyped(KeyEvent event) {
    char ch=event.getKeyChar();
    switch(ch) 
    {
    case 'h':
        //????????
    case 'v':
        //????????
    }
}
}

and this is the main code :

import javax.swing.JFrame;

public class Example1
{
public static void main(String[] args) {
    JFrame frame=new JFrame("Drawing line");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1000,1000);
    DrawAxis dshape=new DrawAxis();
    dshape.DrawingAxis(500,0,500,1000);
    dshape.DrawingAxis(0,500,1000,500);
    dshape.setFocusable(true);
    dshape.addKeyListener(dshape);
    frame.add(dshape);
    frame.setVisible(true);

}

}

how can I complete this code?

解决方案

Start by using the key bindings API over KeyListener, it will solve some key issues associated with KeyListener. See How to Use Key Bindings for more details.

You could simply store a series of points in a array or List and use Graphics#drawLine to draw lines between them, or you could take advantage of Graphics 2D's shape API. See 2D Graphics for more details

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Path2D;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawLine {

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

    public DrawLine() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Point currentPoint;
        private Path2D shape;

        public TestPane() {
            currentPoint = new Point(0, 0);
            shape = new Path2D.Double();
            shape.moveTo(0, 0);

            bindKey(KeyEvent.VK_H, "draw.horizontally", new AddPointAction(4, 0));
            bindKey(KeyEvent.VK_V, "draw.vertically", new AddPointAction(0, 4));
        }

        protected void bindKey(int vkKey, String name, Action action) {
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(vkKey, 0), name);
            am.put(name, action);
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.draw(shape);
            g2d.dispose();
        }

        public class AddPointAction extends AbstractAction {

            private int xDelta;
            private int yDelta;

            public AddPointAction(int xDelta, int yDelta) {
                this.xDelta = xDelta;
                this.yDelta = yDelta;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                int x = currentPoint.x + xDelta;
                int y = currentPoint.y + yDelta;
                shape.lineTo(x, y);
                currentPoint.x = x;
                currentPoint.y = y;
                repaint();
            }

        }

    }

}

这篇关于在Java中通过KeyTyped事件绘制两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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