2014-01-09 95 views
1

我正在制作一个简单的程序来绘制一个图形和一些点。应该在改变坐标的同时使用方法创建点,但实际上它的绘画只是最后一点。Java重绘()不起作用

下面是代码:

import javax.swing.*; 
import java.awt.*; 
public class PointGraphWriter extends JPanel 
{ 
    JFrame korniza = new JFrame(); 
    private int x; 
    private int y; 
    private int length; 
    private String OX; 
    private String OY; 
    private String emri; 
    private int y_height; 
    private int x_num; 

    public PointGraphWriter() 
    { 
     int width= 500; 
     korniza.setSize(width,width); 
     korniza.setVisible(true); 
     korniza.setTitle(emri); 
     korniza.getContentPane().add(this); 

    } 

    public void paintComponent(Graphics g) 
    { 
     g.drawLine(x,y,x+length,y); 
     g.drawLine(x,y,x,y-length); 
     g.drawString(OX,x+length, y+15); 
     g.drawString(OY,x-15,y-length); 
     g.drawString("0", x -15,y); 
     g.drawString("0", x,y+15); 
     g.fillOval(x_num,y-y_height-2, 4 ,4); 
    } 

    public void setTitle(String name) 
    { 
     emri= name; 
     this.repaint(); 
    } 

    public void setAxes(int x_pos, int y_pos, int axis_length, String x_label, String y_label) 
    { 
     x= x_pos; 
     y=y_pos; 
     length= axis_length; 
     OX = x_label; 
     OY = y_label; 
    } 

    public void setPoint1(int height) 
    { 
     y_height=height; 
     x_num = x-2; 
     this.repaint(); 
    } 

    public void setPoint2(int height) 
    { 
     y_height=height; 
     x_num = x + length/5-2; 
     this.repaint(); 
    } 
} 

,这里是主要的方法:

public class TestPlot 
{ 
    public static void main(String[] a) 
    { 
     PointGraphWriter e = new PointGraphWriter(); 
     e.setTitle("Graph of y = x*x"); 
     e.setAxes(50, 110, 90, "5", "30"); 
     int scale_factor = 3; 
     e.setPoint1(0 * scale_factor); 
     e.setPoint2(1 * scale_factor); 
    } 
} 
+0

你需要将__Co-ordinates__保存在一些'Collections'中,并迭代该'Collection'来每次绘制所有点(一个新的点被添加到前一个列表中) –

+0

Ehm我其实是一个初学者,我真的知道该怎么做。我只是把这个练习作为我学校的一个项目。 http://img856.imageshack.us/img856/8932/9n1b.png 这是练习。目前我只使用setPoint1和setPoint 2,但我无法解决这个问题。 –

+0

你可以看看这个[thread](http://stackoverflow.com/q/11062249/1057230)。我害怕,我不能为你做功课。虽然一个小例子即将推出...,如果这个想法仍然不清楚,你可以:-) –

回答

2

请看看这个例子。与此相关的一些内容,您可能必须在您的示例中加入,才能使其发挥作用。只需使用Collection来存储您之前绘制的内容,并且当新的东西出现时,只需将该东西添加到列表中,然后再次重新绘制整个Collection。如下图所示:

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

public class RectangleExample { 

    private DrawingBoard customPanel; 
    private JButton button; 

    private Random random; 

    private java.util.List<Rectangle2D.Double> rectangles; 

    private ActionListener buttonActions = 
         new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      Rectangle2D.Double rectangle = new Rectangle2D.Double(
         (double) random.nextInt(100), (double) random.nextInt(100), 
         (double) random.nextInt(100), (double) random.nextInt(100)); 
      rectangles.add(rectangle); 
      customPanel.setValues(rectangles); 
     } 
    }; 

    public RectangleExample() { 
     rectangles = new ArrayList<Rectangle2D.Double>(); 
     random = new Random(); 
    } 

    private void displayGUI() { 
     JFrame frame = new JFrame("Rectangle Example"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout(5, 5)); 

     customPanel = new DrawingBoard(); 
     contentPane.add(customPanel, BorderLayout.CENTER); 

     button = new JButton("Create Rectangle"); 
     button.addActionListener(buttonActions); 
     contentPane.add(button, BorderLayout.PAGE_END); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       new RectangleExample().displayGUI(); 
      } 
     }; 
     EventQueue.invokeLater(runnable); 
    } 
} 

class DrawingBoard extends JPanel { 

    private java.util.List<Rectangle2D.Double> rectangles = 
           new ArrayList<Rectangle2D.Double>(); 

    public DrawingBoard() { 
     setOpaque(true); 
     setBackground(Color.WHITE); 
    } 

    public void setValues(java.util.List<Rectangle2D.Double> rectangles) { 
     this.rectangles = rectangles; 
     repaint(); 
    } 

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

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (Rectangle2D.Double rectangle : rectangles) { 
      g.drawRect((int)rectangle.getX(), (int)rectangle.getY(), 
        (int)rectangle.getWidth(), (int)rectangle.getHeight()); 
     } 
     System.out.println("WORKING"); 
    } 
} 
2

了两个常用的方法作画的例子见Custom Painting Approaches

  1. 保留对象的名单被涂
  2. 油漆到一个BufferedImage

您选择的方法将取决于您的确切要求。