void lines(Graphics g){ a=(int)(x+length*Math.cos(angle)); b=(int)(y+length*Math.sin(angle)); g.setColor(Color.red); g.drawLine(x,y,a,b); }将这条线变粗的代码
时间: 2024-03-20 21:39:24 浏览: 39
要将这条线变粗,可以使用 `Graphics` 类中的 `setStroke()` 方法来设置画笔的粗细。例如,可以在 `lines()` 方法中添加以下代码来将线条变为 3 像素宽:
```java
void lines(Graphics g) {
a = (int)(x + length * Math.cos(angle));
b = (int)(y + length * Math.sin(angle));
g.setColor(Color.red);
// 创建一个 3 像素宽的画笔
Graphics2D g2d = (Graphics2D)g;
Stroke stroke = new BasicStroke(3);
g2d.setStroke(stroke);
g.drawLine(x, y, a, b);
}
```
在上述代码中,首先使用 `Graphics2D` 类型的变量 `g2d` 获取 `Graphics` 对象,并且创建一个宽度为 3 像素的画笔。然后,将画笔设置为 `g2d` 对象的画笔,最后调用 `g.drawLine()` 方法绘制线条。这样,就可以将线条变粗了。
相关问题
import java.util.*; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } }import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class HeartAnimation extends JPanel implements Runnable { private List<Point> heartPoints; private int size; private Color color; private int x; private int y; public HeartAnimation(int size, Color color, int x, int y) { this.size = size; this.color = color; this.x = x; this.y = y; this.setPreferredSize(new Dimension(size, size)); this.heartPoints = new ArrayList<>(); this.createHeart(); } private void createHeart() { for (double t = 0; t <= Math.PI * 2; t += 0.01) { int x = (int) (16 * Math.pow(Math.sin(t), 3)); int y = (int) (-13 * Math.cos(t) + 5 * Math.cos(2 * t) + 2 * Math.cos(3 * t) + Math.cos(4 * t)); Point point = new Point(x * 10, y * 10); this.heartPoints.add(point); } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(this.color); for (Point point : this.heartPoints) { int x = point.x + this.x; int y = point.y + this.y; g.drawLine(x, y, x, y); } } @Override public void run() { while (true) { try { Thread.sleep(10); this.x += 1; this.y += 1; this.repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); HeartAnimation heartAnimation = new HeartAnimation(400, Color.RED, 100, 100); frame.add(heartAnimation); frame.pack(); frame.setVisible(true); Thread thread = new Thread(heartAnimation); thread.start(); } }帮我检查一下这个代码为什么无法运行
这份代码存在两个类的定义,需要将第二个类 `HeartAnimation` 的代码复制到第一个类 `HelloWorld` 的后面并将其删除。此外,在 `main` 方法中启动线程时,应该使用 `SwingUtilities.invokeLater()` 方法以确保在事件分派线程中启动线程,代码如下:
```
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Thread thread = new Thread(heartAnimation);
thread.start();
}
});
```
修改后的代码如下:
```
import java.util.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HelloWorld extends JPanel implements Runnable {
private List<Point> heartPoints;
private int size;
private Color color;
private int x;
private int y;
public HelloWorld(int size, Color color, int x, int y) {
this.size = size;
this.color = color;
this.x = x;
this.y = y;
this.setPreferredSize(new Dimension(size, size));
this.heartPoints = new ArrayList<>();
this.createHeart();
}
private void createHeart() {
for (double t = 0; t <= Math.PI * 2; t += 0.01) {
int x = (int) (16 * Math.pow(Math.sin(t), 3));
int y = (int) (-13 * Math.cos(t) + 5 * Math.cos(2 * t) + 2 * Math.cos(3 * t) + Math.cos(4 * t));
Point point = new Point(x * 10, y * 10);
this.heartPoints.add(point);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(this.color);
for (Point point : this.heartPoints) {
int x = point.x + this.x;
int y = point.y + this.y;
g.drawLine(x, y, x, y);
}
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
this.x += 1;
this.y += 1;
this.repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HelloWorld heartAnimation = new HelloWorld(400, Color.RED, 100, 100);
frame.add(heartAnimation);
frame.pack();
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Thread thread = new Thread(heartAnimation);
thread.start();
}
});
}
}
```
现在代码就可以正常运行了。
帮我生成UML图:package com.sxt; import java.awt.*; /** * 底层地图 * 绘制游戏相关组件 */ public class MapBottom { //绘制方法 void paintSelf(Graphics g){ g.setColor(Color.red); //画竖线 for (int i = 0; i <= GameUtil.MAP_W; i++) { g.drawLine(GameUtil.OFFSET + i * GameUtil.SQUARE_LENGTH, 3*GameUtil.OFFSET, GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH, 3*GameUtil.OFFSET+GameUtil.MAP_H*GameUtil.SQUARE_LENGTH); } //画横线 for (int i = 0; i <=GameUtil.MAP_H; i++){ g.drawLine(GameUtil.OFFSET, 3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH, GameUtil.OFFSET+GameUtil.MAP_W*GameUtil.SQUARE_LENGTH, 3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH); } for (int i = 1; i <= GameUtil.MAP_W ; i++) { for (int j = 1; j <=GameUtil.MAP_H ; j++) { g.drawImage(GameUtil.lei, GameUtil.OFFSET + (i-1)* GameUtil.SQUARE_LENGTH+1, GameUtil.OFFSET*3+(j-1)*GameUtil.SQUARE_LENGTH+1, GameUtil.SQUARE_LENGTH-2, GameUtil.SQUARE_LENGTH-2, null); } } } }
抱歉,由于我是一个语言模型AI,无法在此处为您生成UML图。但是,根据您提供的代码,可以将MapBottom类表示为一个独立的包(package),其中包含一个名为MapBottom的类,该类具有一个方法paintSelf,该方法接受一个Graphics参数。可以将其表示为以下UML类图:
```
+-----------------------+
| com.sxt |
+-----------------------+
| |
| +---------+ |
| | MapBottom| |
| +---------+ |
| | | |
| | | |
| | +------>| paintSelf(g: Graphics) |
| | | |
| +-----------+ |
| |
+-----------------------+
```
其中,MapBottom类具有一个paintSelf方法,该方法接受一个Graphics参数,没有返回值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)