简体   繁体   English

如何增加线宽(Java Applet)

[英]How to increase line width (Java Applet)

I've been trying to increase the width of line using the setStroke method in Java but the only output I seem to be getting is an error message saying "Cannot find method setStroke". 我一直在尝试使用Java中的setStroke方法来增加行的宽度,但我似乎得到的唯一输出是一条错误消息,内容为“无法找到方法setStroke”。

public void paint(Graphics g){
    g.setColor(Color.orange);
    g.setStroke(new BasicStroke(4));
    g.drawLine(5, 5, 480, 5);
 }

I've imported 我已经导入

import java.applet.Applet;
import java.awt.*;          
import java.awt.event.*;    
import javax.swing.*;
import java.awt.Graphics2D;

Try with Graphics2D#setStroke() 尝试使用Graphics2D#setStroke()

Simply downcast it to Graphics2D and use it. 只需将其转换为Graphics2D使用。

For example 例如

Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(4));

NOTE: Never forget to call super.paint(g) in overridden method. 注意:永远不要忘记在重写方法中调用super.paint(g)

You need to change what type you're inputting into your method. 您需要更改要在方法中输入的类型。

public void paint(Graphics2D g){
    g.setColor(Color.orange);
    g.setStroke(new BasicStroke(4));
    g.drawLine(5, 5, 480, 5);
 }

as Graphics2D instead of Graphics . 作为Graphics2D而不是Graphics

EDIT: 编辑:

This will also work (casting it): 这也将起作用(广播):

public void paint(Graphics2D g){
    Graphics2D twoD = (Graphics2D) g;
    twoD.setColor(Color.orange);
    twoD.setStroke(new BasicStroke(4));
    twoD.drawLine(5, 5, 480, 5);
 }

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

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