Swing Paint项目-使用Java绘制一条更粗的线

Swing Paint项目-使用Java绘制一条更粗的线,java,Java,这里的代码解释了使用netbeans在java语言上运行的绘图工具。我希望“标记”比默认值更厚。这样画很薄。至少对我来说是这样。无论如何,我对如何使它变厚感到迷茫。(这不再是一个好答案,因为op改变了他的帖子!) 你需要改变你的字体 因此,您需要调用swing组件 package com.ssaurel.swingpaint; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.a

这里的代码解释了使用netbeans在java语言上运行的绘图工具。我希望“标记”比默认值更厚。这样画很薄。至少对我来说是这样。无论如何,我对如何使它变厚感到迷茫。

(这不再是一个好答案,因为op改变了他的帖子!)

你需要改变你的字体

因此,您需要调用swing组件

package com.ssaurel.swingpaint;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JComponent;

/**
* Component for drawing !
*
* @author sylsau
*
*/
public class DrawArea extends JComponent {

  // Image in which we're going to draw
  private Image image;
  // Graphics2D object ==> used to draw on
  private Graphics2D g2;
  // Mouse coordinates
  private int currentX, currentY, oldX, oldY;

  public DrawArea() {
    setDoubleBuffered(false);
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        // save coord x,y when mouse is pressed
        oldX = e.getX();
        oldY = e.getY();
      }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        // coord x,y when drag mouse
        currentX = e.getX();
        currentY = e.getY();

        if (g2 != null) {
          // draw line if g2 context not null
          g2.drawLine(oldX, oldY, currentX, currentY);
          // refresh draw area to repaint
          repaint();
          // store current coords x,y as olds x,y
          oldX = currentX;
          oldY = currentY;
        }
      }
    });
  }

  protected void paintComponent(Graphics g) {
    if (image == null) {
      // image to draw null ==> we create
      image = createImage(getSize().width, getSize().height);
      g2 = (Graphics2D) image.getGraphics();
      // enable antialiasing
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      // clear draw area
      clear();
    }

    g.drawImage(image, 0, 0, null);
  }

  // now we create exposed methods
  public void clear() {
    g2.setPaint(Color.white);
    // draw white on entire draw area to clear
    g2.fillRect(0, 0, getSize().width, getSize().height);
    g2.setPaint(Color.black);
    repaint();
  }

  public void red() {
    // apply red color on g2 context
    g2.setPaint(Color.red);
  }

  public void black() {
    g2.setPaint(Color.black);
  }

  public void magenta() {
    g2.setPaint(Color.magenta);
  }

  public void green() {
    g2.setPaint(Color.green);
  }

  public void blue() {
    g2.setPaint(Color.blue);
  }

}

package com.ssaurel.swingpaint;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingPaint {

  JButton clearBtn, markerBtn, blackBtn, blueBtn, greenBtn, redBtn, magentaBtn;
  DrawArea drawArea;
  ActionListener actionListener = new ActionListener() {

  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == clearBtn) {
        drawArea.clear();
      } else if (e.getSource() == blackBtn) {
        drawArea.black();
      } else if (e.getSource() == blueBtn) {
        drawArea.blue();
      } else if (e.getSource() == greenBtn) {
        drawArea.green();
      } else if (e.getSource() == markerBtn) {
        drawArea.black(); 
      } else if (e.getSource() == redBtn) {
        drawArea.red();
      } else if (e.getSource() == magentaBtn) {
        drawArea.magenta();
      }
    }
  };

  public static void main(String[] args) {
    new SwingPaint().show();
  }

  public void show() {



    // create main frame
    JFrame frame = new JFrame("Swing Paint");
    Container content = frame.getContentPane();
    // set layout on content pane
    content.setLayout(new BorderLayout());
    // create draw area
    drawArea = new DrawArea();

    // add to content pane
    content.add(drawArea, BorderLayout.CENTER);

    // create controls to apply colors and call clear feature
    JPanel controls = new JPanel();

    clearBtn = new JButton("Clear");
    clearBtn.addActionListener(actionListener);
    blackBtn = new JButton("Black");
    blackBtn.addActionListener(actionListener);
    blueBtn = new JButton("Blue");
    blueBtn.addActionListener(actionListener);
    greenBtn = new JButton("Green");
    greenBtn.addActionListener(actionListener);
    redBtn = new JButton("Red");
    redBtn.addActionListener(actionListener);
    magentaBtn = new JButton("Magenta");
    magentaBtn.addActionListener(actionListener);
    markerBtn = new JButton("Marker");
    markerBtn.addActionListener(actionListener);

    // add to panel
    controls.add(markerBtn);
    controls.add(greenBtn);
    controls.add(blueBtn);
    controls.add(blackBtn);
    controls.add(redBtn);
    controls.add(magentaBtn);
    controls.add(clearBtn);

    // add to content pane
    content.add(controls, BorderLayout.NORTH);

    frame.setSize(900, 720);
    // can close frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // show the swing paint result
    frame.setVisible(true);

  }

}
或者跟

.setFont().deriveFont(Font.BOLD));

要设置新字体

现在您已经编辑了文章,解释您希望线条画得更粗(这与“粗体”不同-粗体仅适用于字体),您需要查找Java“笔划样式”


这是官员。还有很多其他的。

它不是一种字体,它是一支画笔,而且它只设置为默认的小厚度。。。我想知道您是否可以加粗默认厚度。我必须导入什么?我无法回答,因为您提供的代码和有关您真正尝试实现的内容的信息较少。是的,您可以通过将fontsize乘以定义的工厂来加粗厚度。您需要在DrawArea类中的g2对象上调用setFont()。那就行了。
.setFont(new Font("serif", Font.BOLD,  16))