How To Draw A Triangle From A Circle
I would use a Path2D object, and would place my commencement bespeak with its moveTo(...)
method, and then add additional points with its lineTo(...)
method. Then I could draw information technology or fill information technology 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 coffee.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 { individual static final int PREF_W = 600; individual static final int PREF_H = PREF_W; private static final Colour COLOR_1 = Color.blue; private static terminal Color COLOR_2 = Color.reddish; private static final Paint GRADIENT_PAINT = new GradientPaint(0, 0, COLOR_1, 20, twenty, COLOR_2, true); private Path2D myPath = new Path2D.Double(); public Path2DExample() { double firstX = (PREF_W / 2.0) * (1 - one / Math.sqrt(3)); double firstY = 3.0 * PREF_H / four.0; myPath.moveTo(firstX, firstY); myPath.lineTo(PREF_W - firstX, firstY); myPath.lineTo(PREF_W / 2.0, PREF_H / four.0); myPath.closePath(); } @Override protected void paintComponent(Graphics thou) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) yard; // to smoothen out the jaggies g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(GRADIENT_PAINT); // but for fun! g2.fill up(myPath); // fill my triangle } @Override public Dimension getPreferredSize() { if (isPreferredSizeSet()) { render 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(Cord[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
An boosted benefit to use of a Path2D object is that if you want to elevate the Shape, it'south not difficult to do with a MouseListener and MouseMotionListener, say something like:
private grade MyMouseAdapter extends MouseAdapter { private Point pPressed = nil; @Override public void mousePressed(MouseEvent e) { if (due east.getButton() != MouseEvent.BUTTON1) { return; } if (myPath.contains(eastward.getPoint())) { pPressed = east.getPoint(); } } public void mouseDragged(MouseEvent e) { drag(east); } @Override public void mouseReleased(MouseEvent e) { elevate(e); pPressed = cipher; } private void drag(MouseEvent due east) { if (pPressed == null) { return; } Signal p = due east.getPoint(); int tx = p.10 - pPressed.x; int ty = p.y - pPressed.y; AffineTransform at = AffineTransform.getTranslateInstance(tx, ty); myPath.transform(at); pPressed = p; repaint(); }; }
The whole affair could look like:
import java.awt.Color; import java.awt.Dimension; import coffee.awt.GradientPaint; import coffee.awt.Graphics; import java.awt.Graphics2D; import coffee.awt.Paint; import java.awt.Signal; 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("series") public class Path2DExample extends JPanel { private static last int PREF_W = 600; private static final int PREF_H = PREF_W; private static final Colour COLOR_1 = Color.blue; private static final Color COLOR_2 = Colour.scarlet; individual static final Paint GRADIENT_PAINT = new GradientPaint(0, 0, COLOR_1, 20, twenty, COLOR_2, true); private Path2D myPath = new Path2D.Double(); public Path2DExample() { double firstX = (PREF_W / 2.0) * (i - 1 / Math.sqrt(three)); double firstY = three.0 * PREF_H / iv.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.make full(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 = goose egg; @Override public void mousePressed(MouseEvent e) { if (eastward.getButton() != MouseEvent.BUTTON1) { return; } if (myPath.contains(eastward.getPoint())) { pPressed = due east.getPoint(); } } public void mouseDragged(MouseEvent due east) { drag(e); } @Override public void mouseReleased(MouseEvent e) { elevate(e); pPressed = null; } individual void drag(MouseEvent e) { if (pPressed == null) { return; } Point p = e.getPoint(); int tx = p.x - pPressed.ten; 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(); } }); } }
Source: https://stackoverflow.com/questions/29447994/how-do-i-draw-a-triangle
Posted by: burnsyoudly.blogspot.com
0 Response to "How To Draw A Triangle From A Circle"
Post a Comment