好的,首先我知道如何绘制一个矩形和圆形等与g.drawRect或g.drawOval但是没有g.drawtriangle所以你们可以告诉我是否有办法画一个三角形我不得不把它画出三角形的每一边。
答案 0 :(得分:9)
您可以使用Graphics.drawPolygon(int[], int[], int)
,其中第一个int []是x值的集合,第二个int []是y值的集合,int是数组的长度。 (在三角形的情况下,int将是3)
示例:
graphics.drawPolygon(new int[] {10, 20, 30}, new int[] {100, 20, 100}, 3);
输出:
答案 1 :(得分:4)
我会使用Path2D对象,并使用其moveTo(...)
方法放置我的第一个点,然后使用其lineTo(...)
方法添加其他点。然后我可以通过Graphics2D#draw(...)
和Graphics2D#fill(...)
绘制或填充它。同时在其上调用closePath()
将确保您的三角形适当关闭。
例如,以下代码生成:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.*;
public class Path2DExample extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final Color COLOR_1 = Color.blue;
private static final Color COLOR_2 = Color.red;
private static final Paint GRADIENT_PAINT = new GradientPaint(0, 0, COLOR_1, 20, 20, COLOR_2, true);
private Path2D myPath = new Path2D.Double();
public Path2DExample() {
double firstX = (PREF_W / 2.0) * (1 - 1 / Math.sqrt(3));
double firstY = 3.0 * PREF_H / 4.0;
myPath.moveTo(firstX, firstY);
myPath.lineTo(PREF_W - firstX, firstY);
myPath.lineTo(PREF_W / 2.0, PREF_H / 4.0);
myPath.closePath();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// to smooth out the jaggies
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(GRADIENT_PAINT); // just for fun!
g2.fill(myPath); // fill my triangle
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
Path2DExample mainPanel = new Path2DExample();
JFrame frame = new JFrame("Path2DExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
使用Path2D对象的另一个好处是,如果要拖动Shape,使用MouseListener和MouseMotionListener并不难,比如说:
private class MyMouseAdapter extends MouseAdapter {
private Point pPressed = null;
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() != MouseEvent.BUTTON1) {
return;
}
if (myPath.contains(e.getPoint())) {
pPressed = e.getPoint();
}
}
public void mouseDragged(MouseEvent e) {
drag(e);
}
@Override
public void mouseReleased(MouseEvent e) {
drag(e);
pPressed = null;
}
private void drag(MouseEvent e) {
if (pPressed == null) {
return;
}
Point p = e.getPoint();
int tx = p.x - pPressed.x;
int ty = p.y - pPressed.y;
AffineTransform at = AffineTransform.getTranslateInstance(tx, ty);
myPath.transform(at);
pPressed = p;
repaint();
};
}
整个事情看起来像:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import javax.swing.*;
@SuppressWarnings("serial")
public class Path2DExample extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final Color COLOR_1 = Color.blue;
private static final Color COLOR_2 = Color.red;
private static final Paint GRADIENT_PAINT = new GradientPaint(0, 0, COLOR_1,
20, 20, COLOR_2, true);
private Path2D myPath = new Path2D.Double();
public Path2DExample() {
double firstX = (PREF_W / 2.0) * (1 - 1 / Math.sqrt(3));
double firstY = 3.0 * PREF_H / 4.0;
myPath.moveTo(firstX, firstY);
myPath.lineTo(PREF_W - firstX, firstY);
myPath.lineTo(PREF_W / 2.0, PREF_H / 4.0);
myPath.closePath();
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(GRADIENT_PAINT);
g2.fill(myPath);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class MyMouseAdapter extends MouseAdapter {
private Point pPressed = null;
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() != MouseEvent.BUTTON1) {
return;
}
if (myPath.contains(e.getPoint())) {
pPressed = e.getPoint();
}
}
public void mouseDragged(MouseEvent e) {
drag(e);
}
@Override
public void mouseReleased(MouseEvent e) {
drag(e);
pPressed = null;
}
private void drag(MouseEvent e) {
if (pPressed == null) {
return;
}
Point p = e.getPoint();
int tx = p.x - pPressed.x;
int ty = p.y - pPressed.y;
AffineTransform at = AffineTransform.getTranslateInstance(tx, ty);
myPath.transform(at);
pPressed = p;
repaint();
};
}
private static void createAndShowGui() {
Path2DExample mainPanel = new Path2DExample();
JFrame frame = new JFrame("Path2DExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}