开发者

Java Buffered Image, ontop of JPanel Issue

开发者 https://www.devze.com 2023-04-10 04:28 出处:网络
So here is my problem, my professor is having us make a paint program in which we pick an item and draw on it. However, the issue is that he wants us to draw on a buffered image and then have that on

So here is my problem, my professor is having us make a paint program in which we pick an item and draw on it. However, the issue is that he wants us to draw on a buffered image and then have that on top of the JPanel.

As of right now almost everything works, you can pick one of the buttons I have set up, you can draw a rectangle and resize the frame. When you do this the image of the rectangle will not go away and the JPanel will extend. However the buffered image will not extend, you can see in the code that in my paintComponent method I make a buffered image if the grid == null, and if it is then it creates a image the width and height of my board. When I try and add a call to this method any other time it does not resize since the grid is no longer equal to null. I don't know how to call a resize on a buffered image.

Any ideas would be great!

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 *
 * @author Calvin Moss
 */
public class Drawing extends JPanel implements MouseListener
{

  public int x1, x2 ,y1, y2;
  public static Drawing instance;

  BufferedImage grid;
  static Graphics2D gc;

  Drawing()
  {
    setBackground(Color.RED);
    addMouseListener(this);
  }

  public static Drawing getInstance()
  {
    if(instance == null)
      instance =  new Drawing();
    return instance;      
  }

  public void paintComponent(Graphics g)
  {     
    super.paintComponent(g);  
    Graphics2D g2 = (Graphics2D)g;

    int w = Board.getInstance().getWidth();
    int h = Board.getInstance().getHeight(); 
    if(grid == null)
    {
      grid = (BufferedImage)(this.createImage开发者_JAVA百科(w,h));
      gc = grid.createGraphics();
      gc.setColor(Color.BLUE);
      BufferedImage grid;
      Graphics2D gc;
    }
    g2.drawImage(grid, null, 0, 0);
    check();
  }

  public void draw()
  {
    Graphics2D g = (Graphics2D)getGraphics();
    int w = x2 - x1;
    if (w<0)
      w = w *(-1);
    int h = y2-y1;
    if (h<0)
      h=  h*(-1);

    switch(Main.choice)
    {      
      case 1:
      {
        System.out.println("double gay");
        gc.drawLine(x1, y1, x2, y2);
        repaint();
        break;
      }
      case 2:
      {
        check();
        System.out.println("quad gay");
        gc.drawRect(x1, y1, w, h);
        repaint();
        break;
      }
    }
  }

  public void check()
  {
    if (x1 > x2)
    {
      int z = 0;
      z = x1;
      x1 = x2;
      x2 =z;
    }
    if (y1 > y2)
    {
      int z = 0;
      z = y1;
      y1 = y2;
      y2 = z;
    }
  }

  public void mouseExited(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}

  public void mouseClicked(MouseEvent e){ 
    System.out.println("Gay");
  }

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

  public void mouseReleased(MouseEvent evt)
  {
    x2 = evt.getX();
    y2 = evt.getY();
    draw();
  }

}


Make the BufferedImage the size of the desktop,then you don't have to worry about resizing.

Or, add a ComponentListener to the panel. When the component resizes, create a new BufferedImage to reflect the new panel size. Then draw old buffered image on to the new one. Of course with this approach data will be lost if the panel shrinks and then grows again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号