Ok so first of all i know how to draw a rectangle and circles and ect with g.drawRect or g.drawOval but there is no g.drawtriangle so can you guys tell me if there is a way to draw a triangle with out me having to draw it out each side of the triangle.
问题:
回答1:
You may use Graphics.drawPolygon(int[], int[], int)
where the first int[] is the set of x values, the second int[] is the set of y values, and the int is the length of the array. (In a triangle's case, the int is going to be 3)
Example:
graphics.drawPolygon(new int[] {10, 20, 30}, new int[] {100, 20, 100}, 3);
Output:
回答2:
I would use a Path2D object, and would place my first point with its moveTo(...)
method, and then add additional points with its lineTo(...)
method. Then I could draw it or fill it via Graphics2D#draw(...)
and Graphics2D#fill(...)
. Also calling closePath()
on it will make sure that your triangle closes appropriately.
For example, the following code produces:
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();
}
});
}
}
An additional benefit to use of a Path2D object is that if you want to drag the Shape, it's not hard to do with a MouseListener and MouseMotionListener, say something like:
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();
};
}
The whole thing could look like:
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();
}
});
}
}