Java画布绘图 [英] Java Canvas Drawing

查看:87
本文介绍了Java画布绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的新格式

背景:我有一个扩展JFrame的Window类,在JFrame中有一个画布. 我向画布添加自定义对象.该对象的唯一目的(出于争论的目的)是在画布上打印一个带有文本的圆圈.您可以将多个这些自定义组件添加到画布.添加新的自定义组件时,该组件与添加到画布上的上一个组件相距20个像素(目前).

BACKGROUND: I have a class, Window, that extends JFrame, In the JFrame I have a canvas. To the canvas I add custom objects. The only purpose of this object(for argumenst sake) is to print a circle with text to the canvas. You can add multiple of these custom components to the canvas. When you add a new custom component it is placed 20 pixels(for now) away from the previous component added to the canvas.

问题:画布将第二个圆放置在一个称为A的位置,A大约位于表单的中心,添加的第三个圆放置在了位置B,B一直位于该位置的右侧画布上,当添加更多的圆圈时,位置A和B都向左移动.不管我在paint方法中指定为drawOval函数的x和y坐标是什么,

PROBLEM: The canvas places the second circle at a position lets call it A, A is approximately in the centre of the form, The third circle added is placed at a position B, B is all the way to the right of the canvas, when more circles are added both positions A and B shift left. Irrespective of what I specify as the x and y coordinates of the drawOval function in the paint method this happens.

问题:是否存在与您要绘制的对象相关的设置/功能/选项,例如Canvas和JFrame,它将设置在其上的对象放置在预设位置而不是指定要绘制的位置

QUESTION: Is there a setting/function/option related to objects that you draw to eg Canvas and JFrame that will put the objects drawn to it at preset positions instead of where it is specified to draw

在edit4之前发布的所有内容:

Everything posted before edit4:

Java中的画布是否具有某种我可以禁用的设置,因此无法将其放置在所需的位置.

Does a canvas in java have some sort of setting that I can disable which makes it place items where it wants.

public void paint(Graphics g){
  System.out.println("paint   X: "+x+"  Y: "+y);
  g.drawOval(x, y, RAD , RAD);
  System.out.println("paint 2nd   X: "+x+"  Y: "+y);

}

当对x和y进行硬编码时,以下代码不会将两个圆放置在同一位置.out证明x,y不变,所以我很茫然.(当两个不同的对象各自调用其draw函数时第一个放置在预定义的位置之后,而不是我指定的位置)

The following code does not place two circles at the same position when x and y are hard coded the .out's prove that x,y do not change so I am at a loss.( when two different objects call their draw function every one after the first one is placed at predefined positions but not where I specify it)

我已经制作了一个jar文件,您可以检查它的意思,如果添加一个新节点,则它应该与前一个节点只有60像素的距离,并且位置固定.可以在此处

I have made a jar file that you can check to see what I mean, if you add a new node it should only be 60pixels away from the previous, and at a constant position. It can be found here

Edit2:我真的不知道您可能需要什么代码,我确实有很多代码.就像在我拥有一个JFrame之前所解释的那样,在Jframe上有一个画布,为了澄清更多,我有一些对象扩展了组件,每个对象都使用上述代码片段作为paint(据我所知,当在canvas上调用repaint时会调用该对象),如果您查看该示例,则可以单击添加节点按钮,第一个对象(节点)的位置为0,0(正确),则添加的第二个节点应位于添加的前一个节点的右侧60像素处,但是您添加的第二个项目始终放置在画布中心附近,无论该第二个项目的坐标如何,即使我对其进行了硬编码也是如此.如果右键单击,您还可以添加一个新节点,并且该节点应放置在您右键单击的位置,但也应添加到画布上的随机位置,我已经检查并仔细检查了函数接收到的坐标,因此一点都没有错我的坐标,这就是为什么我的问题的开始行询问如何在我绘制它们的地方而不是在画布想要放置它们的地方如何放置这些东西.

I don't really know what code you could need, I do have a lot of code. Like explained before I have a JFrame, On the Jframe there is a canvas, to clarify more, I have objects which extend component each of which use the above code snippet as paint(which to my understanding gets called when repaint is called on the canvas) if you look at the example you can click the add node button, the first object (node) gets put at 0,0 which is correct, the second node you add should be placed 60 pixels to the right of the previous node added, but the second one you add always gets placed at around the centre of the canvas, no matter what the coordinates of that second item even if I hard code them. If you right click you can also add a new node and that node should be placed where you right clicked but also gets added to a random position on the canvas I have checked and double check the coordinates the function recieve so nothing at all is wrong with my coordinates, thats why the starting line of my question asks how to get the Items to be plaed where I am drawing them and not where the canvas wanst to place them.

Edit3:我的问题仍然不清楚,如果您可以查看jar,并且已经安装了Java,则可以使用以下命令运行jar,前提是jar位于命令提示符C:\ Program Files \中的当前目录中. Java \ jre1.6.0_04 \ bin \ java.exe -jar Projek.jar,对于Linux,仅Java -jar Projek.jar.好,以获取更多代码段.

Aperantly my question is still unclear If you can look a the jar, and you have java installed you can run the jar with the folowing command assuming the jar is in your current directory in command prompt C:\Program Files\Java\jre1.6.0_04\bin\java.exe -jar Projek.jar, and for linux just java -jar Projek.jar . Ok for more code snippets.

在Window类中

public class Window extends JFrame implements   ActionListener{
...
Canvas panel;
...
private int globalX;
private int globalY;
...
public Window(){
...
graph=new Graph();

    panel = new Canvas();
panel.setLayout(new GridLayout(1, 2));
...
container.add(panel);
    container.add(btn_newNode,BorderLayout.SOUTH);
     addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
      });

    setSize(1024,758);
    this.setVisible(true);
}
//Add Node gets called when you click on the button
public void addNode(String name){

    int ix;
    int iy;
    String itext=name;

    if(name.compareTo(".print") != 0){
    if(graph.VertexCount() > 0){
    Vertex temp=graph.getVertex(graph.VertexCount());

    if((temp.x + 20) < panel.getWidth() ){
        ix=temp.x + 20;
        iy=temp.y;
    }
    else{
        ix=temp.x;
        iy=temp.y + 20;
    }
    } else {
        ix=0;
        iy=0;
    }
        Vertex a=new Vertex(itext,ix,iy);
        graph.addVertex(a);
        panel.add(a);
        panel.revalidate();
      } else System.out.println(graph.toString());
    }


  public class Vertex<T> extends Component {
  private T data;
  public int x;
  public int y;
public final int RAD=50;
public Vertex( T data,int _x,int _y) {
incomingEdges = new ArrayList<Edge<T>>();
outgoingEdges = new ArrayList<Edge<T>>();
visited = false;
this.data = data;
x=_x;
y=_y;
System.out.println("X: "+x+"  Y: "+y);

}
public void paint(Graphics g){
  System.out.println("paint   X: "+x+"  Y: "+y);
  g.drawString(data.toString(), x+4, y+27);
  g.drawOval(x, y, RAD , RAD);

  //g.drawOval(20, 0, RAD , RAD);
  System.out.println("paint 2nd   X: "+x+"  Y: "+y);
}

这就是我认为可以理解的一切,但是在^

That is everything that I can think is relavant, But I have been wrong before ^

推荐答案

正在将您的Vertex组件添加到具有GridLayout的容器(画布)中. GridLayout试图定位/绑定Vertex组件.顶点的绘制方法中的坐标系相对于组件的位置/边界.您正在混合两个坐标系和对象层次结构.除非您确实需要顶点作为组件,否则我可以重新组织您的对象结构.如果确实需要Vertex作为组件,则需要使用NO布局管理器(或自定义管理器),并使用Vertex对象的Component坐标进行渲染.解决方案因您的要求而异.

Your Vertex components are being added to a container (the Canvas) with a GridLayout. GridLayout is trying to position/bound the Vertex components. The coordinate system in the paint method of the Vertex is relative to the position/bounds of the component. You're mixing two coordinate systems and object hierarchies. Unless you REALLY need the Vertex to be a component, I could reorganize your object structure. If you DO need Vertex to be a component then you'll want to use NO layout manger (or a custom manager) and use the Component coordinates for the Vertex objects for rendering. The solution is very different depending on your requirements.

这篇关于Java画布绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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