+ 1

How can I code simple shapes?

Abstract Window Toolkit Java

29th Sep 2019, 11:07 AM
Mubiru James Benjamin
Mubiru James Benjamin - avatar
2 odpowiedzi
+ 1
Here is some code that uses a mixture of Java Swing and Abstract Windowing Toolkit and draws an ellipse and rectangle. You could add other simple shapes to the paint method. See https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html and its descendent class, https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html (Graphics2D) for how to draw lots more. Is this what you're after? import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; public class AwtClass extends JFrame { public void paint(Graphics g){ int width=getWidth(); int height=getHeight(); g.setColor(Color.RED); // Draw ellipse. g.drawOval(20, 35, width - 50, height - 50); g.setColor(Color.BLUE); // draw rectangle. g.drawRect(20, 35, width - 50, height - 50); } public static void main(String a[]) { JFrame frame = new AwtClass(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setVisible(true); } }
30th Sep 2019, 6:19 PM
Josh Greig
Josh Greig - avatar
0
Thanks
8th Oct 2019, 8:00 PM
Mubiru James Benjamin
Mubiru James Benjamin - avatar