How to custom Shape a JPanel when painting it? - java

I want to create a custom shape for a JPanel, here's an example of how to shape a JPanel:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FunkyShapedComponent {
public static void main(String[] args) {
new FunkyShapedComponent();
}
public FunkyShapedComponent() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JPanel testPane = new JPanel();
testPane.setBackground(Color.RED);
testPane.setLayout(new GridBagLayout());
testPane.add(new FunkyPane());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(testPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FunkyPane extends JPanel {
public FunkyPane() {
setLayout(new GridBagLayout());
add(new JLabel("This is a simple test"));
setOpaque(false);
}
#Override
public Insets getInsets() {
return new Insets(10, 10, 10, 10);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth() - 1;
int height = getHeight() - 1;
int radius = Math.min(width, height) / 10;
Path2D p = new Path2D.Float();
p.moveTo(0, radius / 2);
p.curveTo(0, 0, 0, 0, radius / 2, 0);
p.curveTo(width / 4, radius, width / 4, radius, (width / 2) - radius, radius / 2);
p.curveTo(width / 2, 0, width / 2, 0, (width / 2) + radius, radius / 2);
p.curveTo(width - (width / 4), radius, width - (width / 4), radius, width - (radius / 2), 0);
p.curveTo(width, 0, width, 0, width, radius / 2);
p.curveTo(width - radius, height / 4, width - radius, height / 4, width - (radius / 2), (height / 2) - radius);
p.curveTo(width, height / 2, width, height / 2, width - (radius / 2), (height / 2) + radius);
p.curveTo(width - radius, height - (height / 4), width - radius, height - (height / 4), width, height - (radius / 2));
p.curveTo(width, height, width, height, width - (radius / 2), height);
p.curveTo(width - (width / 4), height - radius, width - (width / 4), height - radius, (width / 2) + radius, height - (radius / 2));
p.curveTo(width / 2, height, width / 2, height, (width / 2) - radius, height - (radius / 2));
p.curveTo((width / 4), height - radius, (width / 4), height - radius, (radius / 2), height);
p.curveTo(0, height, 0, height, 0, height - (radius / 2));
p.curveTo(radius, height - (height / 4), radius, height - (height / 4), (radius / 2), (height / 2) + radius);
p.curveTo(0, height / 2, 0, height / 2, (radius / 2), (height / 2) - radius);
p.curveTo(radius, (height / 4), radius, (height / 4), 0, (radius / 2));
p.closePath();
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setColor(getBackground());
g2d.fill(p);
g2d.dispose();
}
}
}
This is what it does:
And this is the shape i want:
What i want is to add components to a panel with that shape, but i don't know how to manage path2D, if there's a better way to do it, please tell me

I personally wouldn't attempt to misshape the JPanel, it is essentially what holds everything else and isn't something you should change. Instead I would create a custom shape according to what you would like. I am assuming you want the shape to hide something else; because you originally used the JPanel, so I would paint the shape last; this would give the desired effect.
Here's What You Can Do:
Draw an Arbitrary Shape; Java doesn't support custom shapes directly.
Here is a link which will explain everything: http://docs.oracle.com/javase/tutorial/2d/geometry/arbitrary.html
I Hope This Helped :)
*Just so you know the sample code only leads to the download for the image.

See How to Create Translucent and Shaped Windows
To create that shape you can mask out the top and bottom of a rectangle with two ellipses. Examples here.
I would be inclined to do it with JavaFX such as this.
Your code link points to the image link.

Related

Creating a 2D Cylinder with Java Graphics

I'm trying to make a cylinder using Java Graphics and the paintComponent() method.
The user of the application can select which shape they want (in this case it's a cylinder) and input the dimensions they want for the shape.
After they input the dimensions and click the submit button, another window will open with the image drawn on it.
My current issue is getting the shape made correctly. I'm currently trying to make two ovals and connect them using two lines. The base will be a red oval and everything else will have no color.
When you submit the dimensions for the cylinder, the sides are never the correct length or in the correct position on the Y-Axis. An example can be view here:
The dimensions for this cylinder: 200 height, 50 radius.
What the cylinder should look like:
300 height, 300 radius
I'll be working on adding the minimal version of the program for testing. However, for right now I'll be providing what the code is for the cylinder itself and the paintComponent() method.
Cylinder:
import java.awt.Color;
public class Cylinder extends Circle {
private int length;
public Cylinder(int radius, int length, Color color) {
super(radius, length, color);
this.length = length;
this.radius = radius;
}
public Cylinder(int newX, int newY, int newRadius, int newLength) {
super(newX, newY, newRadius);
length = newLength;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int calcArea() {
return (int) Math.ceil( 2 * 3.14 * radius * radius + 2 * 3.14 * radius * length);
}
public int calcVolume() {
return (int) Math.ceil(3.14 * radius * radius * length);
}
public DrawFigure drawFigure() {
DrawFigure cylinder1 = new DrawFigure(4, getRadius(), length);
return cylinder1;
}
public String toString() {
return "Length = " + length + " " + super.toString();
}
}
DrawFigure (paintComponent is the last method):
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawFigure extends JPanel {
int type;
int length, width, height, radius;
public DrawFigure() {
super();
type = 5;
}
public DrawFigure(int myType, int myWidth, int myLength, int myHeight) { // Box and Rectangle
super();
type = myType;
length = myLength;
width = myWidth;
height = myHeight;
}
public DrawFigure(int x, int y, int myType, int myWidth, int myLength, int myHeight) {
super();
type = myType;
length = myLength;
width = myWidth;
height = myHeight;
}
public DrawFigure(int myType, int myRadius, int myHeight) {
super();
type = myType;
radius = myRadius;
height = myHeight;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (type == 1) { // Draw Rectangle
} else if (type == 2) { // Draw Box
} else if(type == 3) { // Draw Circle
} else if(type == 4) { // Draw Cylinder
g.setColor(Color.BLACK);
g.drawOval(135, 65, radius, radius - radius / 2);
// Base
g.setColor(Color.RED);
g.fillOval(135, 65 + height, radius, radius / 2);
g.setColor(Color.BLACK);
g.drawLine(135, 65 + height + (height /4), 135, 135);
g.setColor(Color.BLACK);
g.drawLine(135 + radius, 65 + height + (height /4), 135 + radius, 135);
return;
}
}
}
Full code for the program:
Point.java https://pastebin.com/iVgN47e3
Lab6GUI.java https://pastebin.com/bKM790iQ
Rectangle.java https://pastebin.com/MdCrJYeA
Box.java https://pastebin.com/iZCZpUi7
Circle.java https://pastebin.com/aui1NgJi
Cylinder.java https://pastebin.com/fHDNmBXT
DrawFigure.java https://pastebin.com/z8t31put
LessThanOrEqualToZeroException.java https://pastebin.com/4ELEmsNX
LessThanOrGreaterThanException.java https://pastebin.com/1avRUudN
Okay, so drawing an oval extends from the x/y position, with a positive width/height, that would make the oval draw right/down from the x/y position, for example...
So, assuming we start at 0x0, this would mean that the lines would need to start at a y position of radius / 4, given that you're using radius for the width and radius / 2 for the height (I'm not going to mention how that is confusing). This will allow the lines to "appear" that they join the outer edge of the oval (and draw down)
The lines would then be height long. This means the bottom oval would then need to start at height - (radius / 4) ... okay, I had to go and double check this, but remember, the line ends at (radius / 4) + height, this also means that the cylinder is the height + (radius / 2) high in total.
🤪🤯
height=200, radius=50
height=300, radius=300
This prevents scenario where radius / 4 is greater than height, because that would just be a mess
Runnable example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new DrawPane(300, 300));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawPane extends JPanel {
int height, radius;
public DrawPane(int myRadius, int myHeight) {
super();
radius = myRadius;
height = myHeight;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - radius) / 2;
int y = (getHeight() - (height + (radius / 4))) / 2;
g2d.translate(x, y);
g2d.setColor(Color.LIGHT_GRAY);
g2d.drawRect(0, 0, radius, height + (radius / 4));
// Base
g2d.setColor(Color.RED);
g2d.fillOval(0, height - (radius / 4), radius, radius / 2);
g2d.setColor(Color.BLACK);
g2d.drawOval(0, 0, radius, radius / 2);
g2d.setColor(Color.BLACK);
g2d.drawLine(0, radius / 4, 0, height);
g2d.drawLine(radius, radius / 4, radius, height);
g2d.dispose();
}
}
}

How to redraw a canvas in Custom View class upon clicking a Restart button

I've got a minesweeper app going and everything basically works fine, until I want to reset the board so I can play a new game.
I have a separate method call drawBoard() which I use to draw my board
My issue is that I want to redraw the board in its initial state with all the squares back to their original colors but I don't know a good way of implementing that upon clicking my Restart button. The canvas is in a Custom view class
Trying to call drawBoard() in my onClickListener in my Main Activity won't work, I can understand why, but trying to use OnClickListener anywhere in the Custom view class throws exceptions
I've looked around, it seems the alternative is to draw an invisible button over my existing button in my LinearLayout, is this the only way that works or is there a way to check the button is clicked then invoke drawboard()?
Update: I'm including my drawboard() as well as my clear() onDraw code
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
int sqWidth= getWidth()/ columns;
int sqlength = getHeight()/rows;
rect = new Rect( 0, 0, sqWidth, sqlength);
//from the list of saved points we can utilize this to paint our squared the was we want it
for (Point p: pointsList){
if (Model.getInstance().getFieldContent(p.x, p.y) == Model.getInstance().mine){
canvas.save();
canvas.translate(p.x * sqWidth, p.y * sqlength);
canvas.drawRect(rect, mine);
canvas.drawBitmap(bomb, p.x, p.y, bombpaint);
canvas.restore();
}
else {
canvas.save();
canvas.translate(p.x * sqWidth, p.y * sqlength);
canvas.drawRect(rect, square);
canvas.restore();
}
}
}
public void drawBoard(Canvas canvas) {
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
canvas.drawLine(0, getHeight() / rows, getWidth(), getHeight() / rows, Lines);
canvas.drawLine(0, 2 * getHeight() / rows, getWidth(), 2 * getHeight() / rows, Lines);
canvas.drawLine(0, 3 * getHeight() / rows, getWidth(), 3 * getHeight() / rows, Lines);
canvas.drawLine(0, 4 * getHeight() / rows, getWidth(), 4 * getHeight() / rows, Lines);
canvas.drawLine(0, 5 * getHeight() / rows, getWidth(), 5 * getHeight() / rows, Lines);
canvas.drawLine(0, 6 * getHeight() / rows, getWidth(), 6 * getHeight() / rows, Lines);
canvas.drawLine(0, 7 * getHeight() / rows, getWidth(), 7 * getHeight() / rows, Lines);
canvas.drawLine(0, 8 * getHeight() / rows, getWidth(), 8 * getHeight() / rows, Lines);
canvas.drawLine(0, 9 * getHeight() / rows, getWidth(), 9 * getHeight() / rows, Lines);
canvas.drawLine(0, 10 * getHeight() / rows, getWidth(), 10 * getHeight() / rows, Lines);
// 9 vertical lines
canvas.drawLine(getWidth() / columns, 0, getWidth() / columns, getHeight(), Lines);
canvas.drawLine(2 * getWidth() / columns, 0, 2 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(3 * getWidth() / columns, 0, 3 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(4 * getWidth() / columns, 0, 4 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(5 * getWidth() / columns, 0, 5 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(6 * getWidth() / columns, 0, 6 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(7 * getWidth() / columns, 0, 7 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(8 * getWidth() / columns, 0, 8 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(9 * getWidth() / columns, 0, 9 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(10 * getWidth() / columns, 0, 10 * getWidth() / columns, getHeight(), Lines); //With this the squares should be drawn
}
public void clear ()
{
path = new Path();
postInvalidate();
}

Java Swing Line Graph

I'm attempting to draw a line graph based on data held in array. I have managed to get the X and Y axis drawn. However when I draw the line with the data onto the graph it does not line up with the X and Y axis values. I am unsure of how this can be fixed. I have shown the code below:
public void drawLineG(Graphics g, int yCoords[]) {
String maxY = Integer.toString(getMax(yCoords));
String minY = Integer.toString(getMin(yCoords));
String maxX = Double.toString((xInterval * yCoords.length) - xInterval);
String minX = Double.toString(xStart);
g.setColor(Color.BLUE);
int height = (getHeight() / 2);
int x = 200; // start x point
// previous coord positions.
int prevX = x;
int prevY = yCoords[0];
g.setColor(Color.BLACK);
g.drawLine(prevX, getHeight() / 2, 500, getHeight() / 2);
g.drawLine(prevX, 200, 200, getHeight() / 2);
g.setColor(Color.BLUE);
g.drawString(minY, 190, (getHeight() / 2));
g.drawString(maxY, 180, (getHeight() / 2) - 255);
g.drawString(yLabel, 140, (getHeight() / 2) - 100);
g.drawString(minX, 192, (getHeight() / 2) + 20);
g.drawString(maxX, 500, (getHeight() / 2) + 20);
g.drawString(xLabel, 350, (getHeight() / 2) + 50);
for (int y : yCoords) {
g.setColor(Color.RED);
g.drawLine(prevX, height - prevY, x, height - y);
prevX = x;
prevY = y;
// add an increment to the X pos.
x = x + 50;
}
}
The array of data I have been testing contains the values: 0, 3, 4, 7, 5, 10, 3
However when this is plotted on the graph it doesn't line up with the values on the x and y axis.
Current: https://i.stack.imgur.com/Ju8BQ.jpg
What I am trying to achieve: https://i.stack.imgur.com/n9Aio.jpg
Any help?
Thanks
Your problem is your scale, you're trying to draw your yCoords as they're given, (3, 10 and so on) but they should be like: 30, 100 to fit the window's scale (Window = your program window).
For example in my code below, each "square" or each "unit" are of 30 x 30 pixels each, so I must convert yCoord = 3 to it's equivalent which would be 3 * 30 = 90 but as the axis is below starting at 400 I must substract it so I get the coord from the axis or from the bottom of the window to the real yCoord which would be 400 - 90 = 310, now this is my real point that I must paint.
However the painting should be done in the paintComponent method.
For my example I didn't draw the Strings for 0, 10, 0.0, 90.0 but you can do it later if you wish
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GraphSample {
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new GraphSample()::createAndShowGui);
}
private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
GraphDrawer drawer = new GraphDrawer(new int[] {0, 3, 4, 7, 5, 10, 3});
frame.add(drawer);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#SuppressWarnings("serial")
class GraphDrawer extends JPanel {
private int[] yCoords;
private int startX = 100;
private int startY = 100;
private int endX = 400;
private int endY = 400;
private int unitX = (endX - startX) / 10;
private int unitY = (endY - startY) / 10;
private int prevX = startX;
private int prevY = endY;
public GraphDrawer(int[] yCoords) {
this.yCoords = yCoords;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//We draw in the following 2 loops the grid so it's visible what I explained before about each "unit"
g2d.setColor(Color.BLUE);
for (int i = startX; i <= endX; i += unitX) {
g2d.drawLine(i, startY, i, endY);
}
for (int i = startY; i <= endY; i += unitY) {
g2d.drawLine(startX, i, endX, i);
}
//We draw the axis here instead of before because otherwise they would become blue colored.
g2d.setColor(Color.BLACK);
g2d.drawLine(startX, startY, startX, endY);
g2d.drawLine(startX, endY, endX, endY);
//We draw each of our coords in red color
g2d.setColor(Color.RED);
for (int y : yCoords) {
g2d.drawLine(prevX, prevY, prevX += unitX, prevY = endY - (y * unitY));
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(endX + 100, endY + 100);
}
}
}
I hope it's clear what the error was.
This is a sample of what the output looks like with a 10 x 10 grid

I am trying to draw the line in the middle. I tried different coordinates

Here is my paintComponent which contains the coordinates
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawLine(-100,0,500,0);
g2.drawLine(141,-500,141,500);
g2.translate(getWidth()/2.0, getHeight()/2.0);
g2.scale(1,-1);
g2.rotate(45*Math.PI/180);
Rectangle2D r = new Rectangle2D.Double(0,0,100,100);
g2.fill(r);
It's a bit of a cheat, but...
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g2.drawLine(0, getHeight() / 2, getWidth(), getHeight()/ 2);
g2.translate(getWidth() / 2.0, (getHeight() / 2.0));
g2.scale(1, -1);
g2.rotate(45 * Math.PI / 180);
Rectangle2D r = new Rectangle2D.Double(-50, -50, 100, 100);
g2.fill(r);
g2.dispose();
}
Basically, the origin point is now the center of the screen, so in order to draw the rectangle "centered" around the origin point, you need to adjust the x/y accordingly
Now, you also adjust the origin point by 50x50 instead, but then you'd need to change the anchor point around which the Graphics context is rotated to the center of the box
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g2.drawLine(0, getHeight() / 2, getWidth(), getHeight()/ 2);
g2.translate((getWidth() / 2.0) - 50, (getHeight() / 2.0) - 50);
g2.scale(1, -1);
g2.rotate(45 * Math.PI / 180, 50, 50);
Rectangle2D r = new Rectangle2D.Double(0, 0, 100, 100);
g2.fill(r);
g2.dispose();
}

Placing Fans in frames

import java.awt.*;
import javax.swing.JPanel;
public class FigurePanel extends JPanel {
// Define constants
public static final int LINE = 1;
public static final int RECTANGLE = 2;
public static final int ROUND_RECTANGLE = 3;
public static final int OVAL = 4;
public static final int ARC = 5;
public static final int POLYGON = 6;
private int type = 1;
private boolean filled;
/** Construct a default FigurePanel */
public FigurePanel() {
}
/** Construct a FigurePanel with the specified type */
public FigurePanel(int type) {
this.type = type;
}
/** Construct a FigurePanel with the specified type and filled */
public FigurePanel(int type, boolean filled) {
this.type = type;
this.filled = filled;
}
/** Draw a figure on the panel */
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Get the appropriate size for the figure
int width = getSize().width;
int height = getSize().height;
switch (type) {
case LINE: // Display two cross lines
g.setColor(Color.BLACK);
g.drawLine(10, 10, width - 10, height - 10);
g.drawLine(width - 10, 10, 10, height - 10);
break;
case RECTANGLE: // Display a rectangle
g.setColor(Color.BLUE);
if (filled)
g.fillRect((int)(0.1 * width), (int)(0.1 * height),
(int)(0.8 * width), (int)(0.8 * height));
else
g.drawRect((int)(0.1 * width), (int)(0.1 * height),
(int)(0.8 * width), (int)(0.8 * height));
break;
case ROUND_RECTANGLE: // Display a round-cornered rectangle
g.setColor(Color.RED);
if (filled)
g.fillRoundRect((int)(0.1 * width), (int)(0.1 * height),
(int)(0.8 * width), (int)(0.8 * height), 20, 20);
else
g.drawRoundRect((int)(0.1 * width), (int)(0.1 * height),
(int)(0.8 * width), (int)(0.8 * height), 20, 20);
break;
case OVAL: // Display an oval
g.setColor(Color.BLACK);
if (filled)
g.fillOval((int)(0.1 * width), (int)(0.1 * height),
(int)(0.8 * width), (int)(0.8 * height));
else
g.drawOval((int)(0.1 * width), (int)(0.1 * height),
(int)(0.8 * width), (int)(0.8 * height));
break;
case ARC: // Display an arc
g.setColor(Color.BLACK);
if (filled) {
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius =
(int)(Math.min(getWidth(), getHeight()) * 0.4);
int x = xCenter - radius;
int y = yCenter - radius;
g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
}
else {
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius =
(int)(Math.min(getWidth(), getHeight()) * 0.4);
int x = xCenter - radius;
int y = yCenter - radius;
g.drawArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.drawArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.drawArc(x, y, 2 * radius, 2 * radius, 180, 30);
g.drawArc(x, y, 2 * radius, 2 * radius, 270, 30);
};
break;
case POLYGON: // Display a polygon
g.setColor(Color.BLACK);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius =
(int)(Math.min(getWidth(), getHeight()) * 0.4);
// Create a Polygon object
Polygon polygon = new Polygon();
// Add points to the polygon
polygon.addPoint(xCenter + radius, yCenter);
polygon.addPoint((int)(xCenter + radius *
Math.cos(2 * Math.PI / 6)), (int)(yCenter - radius *
Math.sin(2 * Math.PI / 6)));
polygon.addPoint((int)(xCenter + radius *
Math.cos(2 * 2 * Math.PI / 6)), (int)(yCenter - radius *
Math.sin(2 * 2 * Math.PI / 6)));
polygon.addPoint((int)(xCenter + radius *
Math.cos(3 * 2 * Math.PI / 6)), (int)(yCenter - radius *
Math.sin(3 * 2 * Math.PI / 6)));
polygon.addPoint((int)(xCenter + radius *
Math.cos(4 * 2 * Math.PI / 6)), (int)(yCenter - radius *
Math.sin(4 * 2 * Math.PI / 6)));
polygon.addPoint((int)(xCenter + radius *
Math.cos(5 * 2 * Math.PI / 6)), (int)(yCenter - radius *
Math.sin(5 * 2 * Math.PI / 6)));
// Draw the polygon
if (filled)
g.fillPolygon(polygon);
else
g.drawPolygon(polygon);
}
}
/** Set a new figure type */
public void setType(int type) {
this.type = type;
// repaint();
}
/** Return figure type */
public int getType() {
return type;
}
/** Set a new filled property */
public void setFilled(boolean filled) {
this.filled = filled;
repaint();
}
/** Check if the figure is filled */
public boolean isFilled() {
return filled;
}
/** Specify preferred size */
public Dimension getPreferredSize() {
return new Dimension(80, 80);
}
}
^ That is Figure Panel class
import java.awt.*;
import javax.swing.*;
public class fan extends JFrame {
public static void main(String[] args) {
JFrame frame = new fan();
frame.setSize(300, 300);
frame.setTitle("Exercise13_09");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
public fan() {
setLayout(new GridLayout(2, 2));
add(new FigurePanel(FigurePanel.ARC, true));
add(new FigurePanel(FigurePanel.OVAL));
add(new FigurePanel(FigurePanel.ARC, true));
add(new FigurePanel(FigurePanel.OVAL, true));
}
}
This is the fan class
I'm currently trying to make a picture of a fan to show up on the screen and was wondering if there was a way to make it draw both the arc and oval in the same spot? I have been messing around with the Figure panel class but I'm not quite sure how I could merge ARC true with OVAL. Help would be appreciated thank you
Add drawOval() at the end of the fill branch of case ARC:
g.drawOval(x, y, 2 * radius, 2 * radius);
Also consider RenderingHints:
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

Categories

Resources