简体   繁体   English

Java图形-在Jbutton上绘制形状

[英]Java Graphics - draw shape on Jbutton

I am making a minimally small paint application for a class in Java. 我正在为Java类制作一个最小的绘画应用程序。 I need to make several buttons for the user to select different shapes, and on those buttons I am supposed to put an image of the shape they are using. 我需要制作几个按钮供用户选择不同的形状,然后在这些按钮上放置他们正在使用的形状的图像。 For example, a button that lets a user draw a line should have have an image of a line on it. 例如,一个让用户画一条线的按钮上应该有一条线的图像。 A button that draws a rectangle, a rectangle should be on it. 绘制矩形的按钮,应该在矩形上。 I need to be able to do this from within the program without using an external image source. 我需要能够在程序中执行此操作,而无需使用外部图像源。

Here is my current button code example. 这是我当前的按钮代码示例。

lineB = new JButton();
lineB.setBounds(0, 25, 20, 20);
lineB.setBackground(Color.WHITE);
shapePanel.add(lineB);
lineB.addActionListener(this);
  1. Create a BufferedImage of the desired size -- BufferedImage img = new BufferedImage(biWidth, biHeight, BufferedImage.TYPE_INT_ARGB); 创建所需大小的BufferedImage img = new BufferedImage(biWidth, biHeight, BufferedImage.TYPE_INT_ARGB); - BufferedImage img = new BufferedImage(biWidth, biHeight, BufferedImage.TYPE_INT_ARGB);
  2. Get its Graphics context by calling getGraphics() or createGraphics() (better because it gives you a Graphics2D object) on it. 通过在其上调用getGraphics()createGraphics() (最好是因为它为您提供了Graphics2D对象)来获取其Graphics上下文。
  3. I'd set the RenderingHints of this Graphics2D object to all for antialiasing using the setRenderingHints(...) method. 我将使用setRenderingHints(...)方法将此Graphics2D对象的RenderingHints设置为全部以进行抗锯齿。 This can smooth out jaggies. 这样可以消除锯齿。
  4. Draw your shape with this object. 使用此对象绘制形状。
  5. Dispose of the Graphics object. 处置Graphics对象。
  6. Create an ImageIcon from the above Image using new ImageIcon(Image image) constructor. 使用new ImageIcon(Image image)构造函数从上述Image创建一个ImageIcon。
  7. Set your button's icon with the above icon using setIcon(...) . 使用setIcon(...)将按钮的图标设置为上面的图标。
  8. Do not call setBounds(...) on the button or on anything. 不要在按钮或任何东西上调用setBounds(...)

eg, 例如,

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.*;

@SuppressWarnings("serial")
public class ImageButton extends JPanel {
   private static final int IMG_WIDTH = 50;
   private static final Color SHAPE_COLOR = Color.RED;
   private static final int GAP = 4;
   private JButton circleButton = new JButton();
   private JButton squareButton = new JButton();

   public ImageButton() {
      BufferedImage circleImg = new BufferedImage(IMG_WIDTH, IMG_WIDTH, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = circleImg.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setColor(SHAPE_COLOR);
      int x = GAP;
      int y = x;
      int width = IMG_WIDTH - 2 * x;
      int height = IMG_WIDTH - 2 * y;
      g2.fillOval(x, y, width, height);
      g2.dispose();
      circleButton.setIcon(new ImageIcon(circleImg));

      BufferedImage squareImg = new BufferedImage(IMG_WIDTH, IMG_WIDTH, BufferedImage.TYPE_INT_ARGB);
      g2 = squareImg.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setColor(SHAPE_COLOR);
      g2.fillRect(x, y, width, height);
      g2.dispose();
      squareButton.setIcon(new ImageIcon(squareImg));

      add(circleButton);
      add(squareButton);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("ImageButton");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ImageButton());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

One Easy way is to add an image to JButton and make it like an image button. 一种简单的方法是将图像添加到JButton并使它像图像按钮。

try {
 Image icon = ImageIO.read(getClass().getResource("icons/line.jpg"));
 lineButton.setIcon(new ImageIcon(icon));
} catch (IOException ex) {
}

edited : 编辑:

since you are urged to create shapes in your code u can do the code below to create a image in memory draw on graphic of the image and then assign it to the button. 由于敦促您在代码中创建形状,因此可以执行以下代码以在内存中创建图像并在图像的图形上绘制图像,然后将其分配给按钮。

    final int BI_WIDTH = 50;
    final int BI_HEIGHT = 50;
    BufferedImage lineImage = new BufferedImage(BI_WIDTH, BI_HEIGHT,
            BufferedImage.TYPE_INT_RGB);
    lineImage.createGraphics().drawLine(2, 25, 48, 25);
    JButton lineButton = new JButton();
    lineButton.setIcon(new ImageIcon(lineImage));

graphic object has many different functions and you could draw whatever you like on it. 图形对象具有许多不同的功能,您可以在其上绘制任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM