如何将图像添加到JFrame?

时间:2014-01-01 01:57:07

标签: java image swing

我希望在运行程序时显示PNG。图像是单位圆。我用单位圆上的坐标排列方框。我尝试了各种方法,但它们不起作用。我不知道你是否可以在JFrame中使用paint方法,因为这可能会导致问题。我以前使用过PNG并且已经使用了PNG,但这只是在Applet中。我希望这可以在不改变代码的情况下工作。这是显示JFrame的类的代码:

public class UnitCircleReal {
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    UnitCircle uc = new UnitCircle();
    uc.setVisible(true);
  }
}

这是JFrame的代码,我想在这个类中添加图像:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MenuBar;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class UnitCircle extends JFrame implements MouseListener,      MouseMotionListener{
  private static final long serialVersionUID = 1L;
  URL url;
  Image UnitCircle;
  int x, y;
  String s,a,b,c,d,e,f,g2,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x2,y2,z;
  boolean mouseInA = false;
  boolean mouseInB = false;
  boolean mouseInC = false;
  boolean mouseInD = false;
  boolean mouseInE = false;
  boolean mouseInF = false;
  boolean mouseInG = false;
  boolean mouseInH = false;
  boolean mouseInI = false;
  boolean mouseInJ = false;
  boolean mouseInK = false;
  boolean mouseInL = false;
  boolean mouseInM = false;
  boolean mouseInN = false;
  boolean mouseInO = false;
  boolean mouseInP = false;
  boolean mouseClickA = false;
  boolean mouseClickB = false;
  boolean mouseClickC = false;
  boolean mouseClickD = false;
  boolean mouseClickE = false;
  boolean mouseClickF = false;
  boolean mouseClickG = false;
  boolean mouseClickH = false;
  boolean mouseClickI = false;
  boolean mouseClickJ = false;
  boolean mouseClickK = false;
  boolean mouseClickL = false;
  boolean mouseClickM = false;
  boolean mouseClickN = false;
  boolean mouseClickO = false;
  boolean mouseClickP = false;


  public UnitCircle() {
    setTitle("The Unit Circle!");
    setSize(1350, 900);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    UnitCirclePic ucp = new UnitCirclePic();
    addMouseMotionListener(this);
    addMouseListener(this);
    setVisible(true);
    getContentPane().add(ucp);
  }


  public void paint(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());
    s = "sine =";
    a = "cosine =";
    b = "tangent =";
    c = "cosecant =";
    d = "secant =";
    e = "cotangent =";
    f = "0";
    g2 = "1";
    h = "√3/2";
    i = "-√3/2";
    j = "und.";
    k = "√2/2";
    l = "-√2/2";
    m = "1/2";
    n = "-1/2";
    o = "√2";
    p = "-√2";
    q = "2";
    r = "-2";
    t = "√3/3";
    u = "-√3/3";
    v = "-1";
    w = "√3";
    x2 = "-√3";
    y2 = "2√3/3";
    z = "-2√3/3";
    Font font = new Font("Serif", Font.PLAIN, 32);
    g.setFont(font);
    g.setColor(Color.BLACK);
    g.drawString(s, 900, 120);
    g.drawString(a, 900, 250);
    g.drawString(b, 900, 380);
    g.drawString(c, 900, 510);
    g.drawString(d, 900, 640);
    g.drawString(e, 900, 770);
    g.setColor(Color.RED);
    g.drawRect(653, 400, 30, 30);//a
    g.drawRect(625, 260, 30, 60);//b
    g.drawRect(580, 210, 30, 60);//c
    g.drawRect(520, 170, 30, 60);//d
    g.drawRect(400, 140, 30, 65);//e
    g.drawRect(273, 175, 35, 60);//f
    g.drawRect(222, 214, 35, 60);//g
    g.drawRect(180, 266, 35, 60);//h
    g.drawRect(150, 400, 30, 30);//i
    g.drawRect(175, 515, 35, 60);//j
    g.drawRect(220, 570, 35, 60);//k
    g.drawRect(273, 600, 35, 60);//l
    g.drawRect(395, 610, 40, 80);//m
    g.drawRect(520, 605, 35, 60);//n
    g.drawRect(580, 570, 33, 60);//o
    g.drawRect(616, 515, 38, 60);//p
    if (mouseClickA){
      g.setColor(Color.BLACK);
      g.setFont(font);
      g.drawString(f, 1000, 120);
      g.drawString(g2, 1025, 250);
      g.drawString(f, 1030, 380);
      g.drawString(f, 1045, 510);
      g.drawString(g2, 1025, 640);
      g.drawString(j, 1060, 770);
    }else if(mouseClickB){
      g.setColor(Color.BLACK);
      g.drawString(m, 1000, 120);
      g.drawString(h, 1025, 250);
      g.drawString(t, 1030, 380);
      g.drawString(q, 1045, 510);
      g.drawString(y2, 1025, 640);
      g.drawString(w, 1060, 770);
    }else if(mouseClickC){
      g.setColor(Color.BLACK);
      g.drawString(k, 1000, 120);
      g.drawString(k, 1025, 250);
      g.drawString(g2, 1030, 380);
      g.drawString(o, 1045, 510);
      g.drawString(o, 1025, 640);
      g.drawString(g2, 1060, 770);
    }else if(mouseClickD){
      g.setColor(Color.BLACK);
      g.drawString(h, 1000, 120);
      g.drawString(m, 1025, 250);
      g.drawString(w, 1030, 380);
      g.drawString(y2, 1045, 510);
      g.drawString(q, 1025, 640);
      g.drawString(t, 1060, 770);
    }else if(mouseClickE){
      g.setColor(Color.BLACK);
      g.drawString(g2, 1000, 120);
      g.drawString(f, 1025, 250);
      g.drawString(j, 1030, 380);
      g.drawString(g2, 1045, 510);
      g.drawString(f, 1025, 640);
      g.drawString(f, 1060, 770);
    }else if(mouseClickF){
      g.setColor(Color.BLACK);
      g.drawString(h, 1000, 120);
      g.drawString(n, 1025, 250);
      g.drawString(x2, 1030, 380);
      g.drawString(y2, 1045, 510);
      g.drawString(r, 1025, 640);
      g.drawString(u, 1060, 770);
    }else if(mouseClickG){
      g.setColor(Color.BLACK);
      g.drawString(k, 1000, 120);
      g.drawString(l, 1025, 250);
      g.drawString(v, 1030, 380);
      g.drawString(o, 1045, 510);
      g.drawString(p, 1025, 640);
      g.drawString(v, 1060, 770);
    }else if(mouseClickH){
      g.setColor(Color.BLACK);
      g.drawString(m, 1000, 120);
      g.drawString(i, 1025, 250);
      g.drawString(u, 1030, 380);
      g.drawString(q, 1045, 510);
      g.drawString(z, 1025, 640);
      g.drawString(x2, 1060, 770);
    }else if(mouseClickI){
      g.setColor(Color.BLACK);
      g.drawString(f, 1000, 120);
      g.drawString(v, 1025, 250);
      g.drawString(f, 1030, 380);
      g.drawString(f, 1045, 510);
      g.drawString(v, 1025, 640);
      g.drawString(j, 1060, 770);
    }else if(mouseClickJ){
      g.setColor(Color.BLACK);
      g.drawString(n, 1000, 120);
      g.drawString(i, 1025, 250);
      g.drawString(t, 1030, 380);
      g.drawString(r, 1045, 510);
      g.drawString(z, 1025, 640);
      g.drawString(w, 1060, 770);
    }else if(mouseClickK){
      g.setColor(Color.BLACK);
      g.drawString(l, 1000, 120);
      g.drawString(l, 1025, 250);
      g.drawString(g2, 1030, 380);
      g.drawString(p, 1045, 510);
      g.drawString(p, 1025, 640);
      g.drawString(g2, 1060, 770);
    }else if(mouseClickL){
      g.setColor(Color.BLACK);
      g.drawString(i, 1000, 120);
      g.drawString(n, 1025, 250);
      g.drawString(w, 1030, 380);
      g.drawString(z, 1045, 510);
      g.drawString(r, 1025, 640);
      g.drawString(t, 1060, 770);
    }else if(mouseClickM){
      g.setColor(Color.BLACK);
      g.drawString(v, 1000, 120);
      g.drawString(f, 1025, 250);
      g.drawString(j, 1030, 380);
      g.drawString(v, 1045, 510);
      g.drawString(f, 1025, 640);
      g.drawString(f, 1060, 770);
    }else if(mouseClickN){
      g.setColor(Color.BLACK);
      g.drawString(i, 1000, 120);
      g.drawString(m, 1025, 250);
      g.drawString(x2, 1030, 380);
      g.drawString(z, 1045, 510);
      g.drawString(q, 1025, 640);
      g.drawString(u, 1060, 770);
    }else if(mouseClickO){
      g.setColor(Color.BLACK);
      g.drawString(l, 1000, 120);
      g.drawString(k, 1025, 250);
      g.drawString(v, 1030, 380);
      g.drawString(p, 1045, 510);
      g.drawString(o, 1025, 640);
      g.drawString(v, 1060, 770);
    }else if(mouseClickP){
      g.setColor(Color.BLACK);
      g.drawString(n, 1000, 120);
      g.drawString(h, 1025, 250);
      g.drawString(u, 1030, 380);
      g.drawString(z, 1045, 510);
      g.drawString(y2, 1025, 640);
      g.drawString(x2, 1060, 770);
    }
  }
  @Override
  public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub
  }
  @Override
  public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    x = e.getX();
    y = e.getY();
    if(e.getX() > 653 && e.getX() < 683){
      if(e.getY() > 400 && e.getY() < 430){
        mouseInA = true;
        mouseClickA = true;
        repaint();
      }
    }
    if(e.getX() < 653 || e.getX() > 683){
      if(e.getY() < 400 || e.getY() > 430){
      mouseInA = false;
      mouseClickA = false;
      }
    }
    if(e.getX() > 625 && e.getX() < 655){
      if(e.getY() > 260 && e.getY() < 320){
        mouseInB = true;
        mouseClickB = true;
        repaint();
      }
    }
    if(e.getX() < 625 || e.getX() > 655){
      if(e.getY() < 260 || e.getY() > 320){
      mouseInB = false;
      mouseClickB = false;
      }
    }
    if(e.getX() > 580 && e.getX() < 610){
      if(e.getY() > 210 && e.getY() < 270){
        mouseInC = true;
        mouseClickC = true;
        repaint();
      }
    }
    if(e.getX() < 580 || e.getX() > 610){
      if(e.getY() < 210 || e.getY() > 270){
      mouseInC = false;
      mouseClickC = false;
      }
    }
    if(e.getX() > 520 && e.getX() < 550){
      if(e.getY() > 170 && e.getY() < 230){
        mouseInD = true;
        mouseClickD = true;
        repaint();
      }
    }
    if(e.getX() < 520 || e.getX() > 550){
      if(e.getY() < 170 || e.getY() > 230){
      mouseInD = false;
      mouseClickD = false;
      }
    }
    if(e.getX() > 400 && e.getX() < 430){
      if(e.getY() > 140 && e.getY() < 205){
        mouseInE = true;
        mouseClickE = true;
        repaint();
      }
    }
    if(e.getX() < 400 || e.getX() > 430){
      if(e.getY() < 140 || e.getY() > 205){
      mouseInE = false;
      mouseClickE = false;
      }
    }
    if(e.getX() > 273 && e.getX() < 308){
      if(e.getY() > 175 && e.getY() < 235){
        mouseInF = true;
        mouseClickF = true;
        repaint();
      }
    }
    if(e.getX() < 273 || e.getX() > 308){
      if(e.getY() < 175 || e.getY() > 235){
      mouseInF = false;
      mouseClickF = false;
      }
    }
    if(e.getX() > 222 && e.getX() < 257){
      if(e.getY() > 214 && e.getY() < 274){
        mouseInG = true;
        mouseClickG = true;
        repaint();
      }
    }
    if(e.getX() < 222 || e.getX() > 257){
      if(e.getY() < 214 || e.getY() > 274){
      mouseInG = false;
      mouseClickG = false;
      }
    }
    if(e.getX() > 180 && e.getX() < 215){
      if(e.getY() > 266 && e.getY() < 326){
        mouseInH = true;
        mouseClickH = true;
        repaint();
      }
    }
    if(e.getX() < 180 || e.getX() > 215){
      if(e.getY() < 266 || e.getY() > 326){
      mouseInH = false;
      mouseClickH = false;
      }
    }
    if(e.getX() > 150 && e.getX() < 180){
      if(e.getY() > 400 && e.getY() < 430){
        mouseInI = true;
        mouseClickI = true;
        repaint();
      }
    }
    if(e.getX() < 150 || e.getX() > 180){
      if(e.getY() < 400 || e.getY() > 430){
      mouseInI = false;
      mouseClickI = false;
      }
    }
    if(e.getX() > 175 && e.getX() < 210){
      if(e.getY() > 515 && e.getY() < 575){
        mouseInJ = true;
        mouseClickJ = true;
        repaint();
      }
    }
    if(e.getX() < 175 || e.getX() > 210){
      if(e.getY() < 515 || e.getY() > 575){
      mouseInJ = false;
      mouseClickJ = false;
      }
    }
    if(e.getX() > 220 && e.getX() < 255){
      if(e.getY() > 570 && e.getY() < 630){
        mouseInK = true;
        mouseClickK = true;
        repaint();
      }
    }
    if(e.getX() < 220 || e.getX() > 255){
      if(e.getY() < 570 || e.getY() > 630){
      mouseInK = false;
      mouseClickK = false;
      }
    }
    if(e.getX() > 273 && e.getX() < 308){
      if(e.getY() > 600 && e.getY() < 660){
        mouseInL = true;
        mouseClickL = true;
        repaint();
      }
    }
    if(e.getX() < 273 || e.getX() > 308){
      if(e.getY() < 600 || e.getY() > 660){
      mouseInL = false;
      mouseClickL = false;
      }
    }
    if(e.getX() > 395 && e.getX() < 435){
      if(e.getY() > 610 && e.getY() < 690){
        mouseInM = true;
        mouseClickM = true;
        repaint();
      }
    }
    if(e.getX() < 395 || e.getX() > 435){
      if(e.getY() < 610 || e.getY() > 690){
      mouseInM = false;
      mouseClickM = false;
      }
    }
    if(e.getX() > 520 && e.getX() < 555){
      if(e.getY() > 605 && e.getY() < 665){
        mouseInN = true;
        mouseClickN = true;
        repaint();
      }
    }
    if(e.getX() < 520 || e.getX() > 555){
      if(e.getY() < 605 || e.getY() > 665){
      mouseInN = false;
      mouseClickN = false;
      }
    }
    if(e.getX() > 580 && e.getX() < 613){
      if(e.getY() > 570 && e.getY() < 630){
        mouseInO = true;
        mouseClickO = true;
        repaint();
      }
    }
    if(e.getX() < 580 || e.getX() > 613){
      if(e.getY() < 570 || e.getY() > 630){
      mouseInO = false;
      mouseClickO = false;
      }
    }
    if(e.getX() > 616 && e.getX() < 654){
      if(e.getY() > 515 && e.getY() < 575){
        mouseInP = true;
        mouseClickP = true;
        repaint();
      }
    }
    if(e.getX() < 616 || e.getX() > 654){
      if(e.getY() < 515 || e.getY() > 575){
      mouseInP = false;
      mouseClickP = false;
      }
    }

  }
  @Override
  public void mouseDragged(MouseEvent e) {
    // TODO Auto-generated method stub  
  }
  @Override
  public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
  }
  @Override
  public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

  }
  @Override
  public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
  }
  @Override
  public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub  
  }
}

1 个答案:

答案 0 :(得分:3)

最简单的方法是使用JLabel作为图片持有人

JLabel imgLabel = new JLabel(new ImageIcon("path_to_image.png"));
add(imgLabel);