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);
Related
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();
}
}
}
I am currently making an analog clock in java using AWT Package and Swing. But the digital clock overlay is currently flickering every once in a while and I'd like to fix that.
I've read about implementing double buffered in conjunction with repaint() but I am stuck on how to implement it.
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class Clock2d extends Applet {
GregorianCalendar cal;
Timer clockTimer = new Timer();
TimeZone clockTimeZone = TimeZone.getDefault();
public Clock2d() {
clockTimer.schedule(new TickTimerTask(), 0, 1000);
}
#Override
public void init() {
}
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillOval(40, 40, 220, 220);
g.setColor(Color.WHITE);
g.fillOval(50, 50, 200, 200);
double second = cal.get(Calendar.SECOND);
double minute = cal.get(Calendar.MINUTE);
double hours = cal.get(Calendar.HOUR);
for (int i = 0; i < 60; i++) {
int length = 90;
double rad = (i * 6) * (Math.PI) / 180;
if (i % 5 == 0) {
length = 82;
g.setColor(Color.BLUE);
} else {
g.setColor(Color.GRAY);
}
int x = 150 + (int) (95 * Math.cos(rad - (Math.PI / 2)));
int y = 150 + (int) (95 * Math.sin(rad - (Math.PI / 2)));
int x1 = 150 + (int) (length * Math.cos(rad - (Math.PI / 2)));
int y1 = 150 + (int) (length * Math.sin(rad - (Math.PI / 2)));
g.drawLine(x, y, x1, y1);
}
drawHands(g, second, minute, hours);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
g.setColor(Color.BLUE);
g.setFont(new Font("Tahoma", Font.BOLD, 16));
g.drawString(sdf.format(cal.getTime()), 120, 20);
g.setFont(new Font("Arial", Font.BOLD, 10));
}
public void drawHands(Graphics g, double second, double minute, double hours) {
double rSecond = (second * 6) * (Math.PI) / 180;
double rMinute = ((minute + (second / 60)) * 6) * (Math.PI) / 180;
double rHours = ((hours + (minute / 60)) * 30) * (Math.PI) / 180;
g.setColor(Color.RED);
g.drawLine(150, 150, 150 + (int) (100 * Math.cos(rSecond - (Math.PI / 2))), 150 + (int) (100 * Math.sin(rSecond - (Math.PI / 2))));
g.setColor(Color.BLACK);
g.drawLine(150, 150, 150 + (int) (70 * Math.cos(rMinute - (Math.PI / 2))), 150 + (int) (70 * Math.sin((rMinute - (Math.PI / 2)))));
g.drawLine(150, 150, 150 + (int) (50 * Math.cos(rHours - (Math.PI / 2))), 150 + (int) (50 * Math.sin(rHours - (Math.PI / 2))));
}
class TickTimerTask extends TimerTask {
#Override
public void run() {
cal = (GregorianCalendar) GregorianCalendar.getInstance(clockTimeZone);
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Clock 2D");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(330, 330));
Clock2d clock2d = new Clock2d();
clock2d.setPreferredSize(new Dimension(320, 320));
clock2d.init();
frame.setLayout(new BorderLayout());
frame.getContentPane().add(clock2d, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
The code work exactly as intended aside from the occasional flickering of the Numbered Digital clock.
public class Clock2d extends Applet A java.awt.Applet is not only deprecated, but also not double buffered by default. Change that to extend a JPanel (which is).
Then change public void paint(Graphics g) { to public void paintComponent(Graphics g) { super.paintComponent(g); to respect the paint chain.
class TickTimerTask extends TimerTask use a javax.swing.Timer instead. It runs on the Event Dispatch Thread, and any calls to the GUI should be made on the EDT.
Here are those three points, expressed in the code. Do you see the 'flickering' artifacts in this version?
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.swing.*;
public class Clock2d extends JPanel {
TimeZone clockTimeZone = TimeZone.getDefault();
GregorianCalendar cal =
(GregorianCalendar) GregorianCalendar.getInstance(clockTimeZone);
ActionListener repaintListener = (ActionEvent e) -> {
cal = (GregorianCalendar) GregorianCalendar.getInstance(clockTimeZone);
repaint();
};
Timer clockTimer = new Timer(100, repaintListener);
public Clock2d() {
clockTimer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(40, 40, 220, 220);
g.setColor(Color.WHITE);
g.fillOval(50, 50, 200, 200);
double second = cal.get(Calendar.SECOND);
double minute = cal.get(Calendar.MINUTE);
double hours = cal.get(Calendar.HOUR);
for (int i = 0; i < 60; i++) {
int length = 90;
double rad = (i * 6) * (Math.PI) / 180;
if (i % 5 == 0) {
length = 82;
g.setColor(Color.BLUE);
} else {
g.setColor(Color.GRAY);
}
int x = 150 + (int) (95 * Math.cos(rad - (Math.PI / 2)));
int y = 150 + (int) (95 * Math.sin(rad - (Math.PI / 2)));
int x1 = 150 + (int) (length * Math.cos(rad - (Math.PI / 2)));
int y1 = 150 + (int) (length * Math.sin(rad - (Math.PI / 2)));
g.drawLine(x, y, x1, y1);
}
drawHands(g, second, minute, hours);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
g.setColor(Color.BLUE);
g.setFont(new Font("Tahoma", Font.BOLD, 16));
g.drawString(sdf.format(cal.getTime()), 120, 20);
g.setFont(new Font("Arial", Font.BOLD, 10));
}
public void drawHands(Graphics g, double second, double minute, double hours) {
double rSecond = (second * 6) * (Math.PI) / 180;
double rMinute = ((minute + (second / 60)) * 6) * (Math.PI) / 180;
double rHours = ((hours + (minute / 60)) * 30) * (Math.PI) / 180;
g.setColor(Color.RED);
g.drawLine(150, 150, 150 + (int) (100 * Math.cos(rSecond - (Math.PI / 2))), 150 + (int) (100 * Math.sin(rSecond - (Math.PI / 2))));
g.setColor(Color.BLACK);
g.drawLine(150, 150, 150 + (int) (70 * Math.cos(rMinute - (Math.PI / 2))), 150 + (int) (70 * Math.sin((rMinute - (Math.PI / 2)))));
g.drawLine(150, 150, 150 + (int) (50 * Math.cos(rHours - (Math.PI / 2))), 150 + (int) (50 * Math.sin(rHours - (Math.PI / 2))));
}
public static void main(String[] args) {
// Swing / AWT GUIs should be created & changed on the EDT ..
Runnable r = () -> {
JFrame frame = new JFrame("Clock 2D");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(330, 330));
Clock2d clock2d = new Clock2d();
clock2d.setPreferredSize(new Dimension(320, 320));
frame.setLayout(new BorderLayout());
frame.getContentPane().add(clock2d, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
};
// .. this is how we ensure that Runnable is on the EDT.
SwingUtilities.invokeLater(r);
}
}
Further tip re fonts:
g.setFont(new Font("Arial", Font.BOLD, 10));
Given the paint method is called repeatedly, it would be best to establish the fonts (both of them) when the class is constructed and store them a attributes of the class (which can be referenced in the methods).
But better to use logical fonts instead of something like 'Arial', like this:
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
Not only does that provide compile time checking, but it adapts across OS platforms. For example, the SANS_SERIF font would result in Arial on Windows and probably most *nix boxes, but on OS X the default SANS_SERIF (undecorated) font is Helvetica.
The other font, Tahoma, is more problematic. Again it's likely to be present on many Windows boxes, but it might be best to either test for it and have a list of backups, or supply the font with the clock app. (assuming you have distribution rights).
I'm having a problem randomly selecting a shape from pre-designed shapes in Java. Previously, I was able to create a random shape but I'm getting no-where with this.
My code is below. I need "DIE07" to randomly be DIE01-DIE06. All help is very much appreciated.
import java.awt.*;
import javax.swing.JPanel;
import java.util.Random;
public class FigurePanel extends JPanel {
public static final int DIE01 = 1;
public static final int DIE02 = 2;
public static final int DIE03 = 3;
public static final int DIE04 = 4;
public static final int DIE05 = 5;
public static final int DIE06 = 6;
public static final int DIE07 = 7;
Random rand = new Random();
private int type = 1;
private boolean filled = false;
public FigurePanel() {
}
public FigurePanel(int type) {
this.type = type;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
switch (type) {
case DIE01:
g.setColor(Color.BLACK);
g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
(int)(0.8 * width), (int)(0.5 * height), 20, 20);
g.fillOval((int)(0.44 * width), (int)(0.46 * height),
(int)(0.1 * width), (int)(0.075 * height));
break;
case DIE02:
g.setColor(Color.RED);
g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
(int)(0.8 * width), (int)(0.5 * height), 20, 20);
g.fillOval((int)(0.27 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
break;
case DIE03:
g.setColor(Color.GREEN);
g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
(int)(0.8 * width), (int)(0.5 * height), 20, 20);
g.fillOval((int)(0.27 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.45 * width), (int)(0.46 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
break;
case DIE04:
g.setColor(Color.GREEN);
g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
(int)(0.8 * width), (int)(0.5 * height), 20, 20);
g.fillOval((int)(0.27 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.27 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
break;
case DIE05:
g.setColor(Color.RED);
g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
(int)(0.8 * width), (int)(0.5 * height), 20, 20);
g.fillOval((int)(0.27 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.27 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.45 * width), (int)(0.46 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
break;
case DIE06:
g.setColor(Color.BLACK);
g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
(int)(0.8 * width), (int)(0.5 * height), 20, 20);
g.fillOval((int)(0.27 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.27 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.27 * width), (int)(0.46 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.46 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.58 * height),
(int)(0.1 * width), (int)(0.075 * height));
g.fillOval((int)(0.63 * width), (int)(0.34 * height),
(int)(0.1 * width), (int)(0.075 * height));
break;
case DIE07:
break;
}
}
public void setType(int type) {
this.type = type;
repaint();
}
public int getType() {
return type;
}
public void setFilled(boolean filled) {
this.filled = filled;
repaint();
}
public boolean isFilled() {
return filled;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(80, 80);
}
}
and this...
import java.awt.*;
import javax.swing.*;
public class TestFigurePanel extends JFrame {
public TestFigurePanel() {
setLayout(new GridLayout(1, 7, 1, 1));
add(new FigurePanel(FigurePanel.DIE01));
add(new FigurePanel(FigurePanel.DIE02));
add(new FigurePanel(FigurePanel.DIE03));
add(new FigurePanel(FigurePanel.DIE04));
add(new FigurePanel(FigurePanel.DIE05));
add(new FigurePanel(FigurePanel.DIE06));
add(new FigurePanel(FigurePanel.DIE07));
}
public static void main(String[] args) {
TestFigurePanel frame = new TestFigurePanel();
frame.setSize(1100, 300);
frame.setTitle("TestFigurePanel");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You can use your Random member instance to instantiate your type randomly if the passed argument is DIE07. If not, you assign it to your type directly.
public FigurePanel(int type) {
if (type == DIE07) {
this.type = DIE01 + rand.nextInt(DIE06);
} else {
this.type = type;
}
}
Random#nextInt(int n) returns a random int value between 0 (inclusive) and the specified value n (exclusive). So, DIE01 + rand.nextInt(DIE06) will return an int value between DIE01 to DIE06 (both inclusive).
I have a rectangle that rotates around it's middle and I have another rectangle that I want to connect to the upper right corner of the rotating rectangle. The problem is that I have no idea how to get the corner so that the second rectangle always will be stuck to that corner.
This is my sample code. Right now the second rectangle will be at the same place all the time which is not the result that I'm after.
package Test;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
class Test{
public static void main(String[] args){
new Test();
}
public Test(){
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Graphic());
frame.setSize(1000,700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
class Graphic extends JPanel{
private int x, y, windowW, windowH;
private double angle;
private Rectangle rect1, rect2;
private Path2D path;
private Timer timer;
private AffineTransform rotation;
public Graphic(){
windowW = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
windowH = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
path = new Path2D.Double();
rotation = new AffineTransform();
angle = 0;
x = windowW / 2;
y = windowH / 2;
timer = new Timer(100, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
angle += .1;
if(angle > 360) angle -= 360;
repaint();
}
});
timer.start();
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
rotation.setToTranslation(500, 200);
rotation.rotate(angle, 32, 32);
rect1 = new Rectangle(0, 0, 64, 64);
path = new Path2D.Double(rect1, rotation);
rect2 = new Rectangle(path.getBounds().x, path.getBounds().y, 10, 50);
g2d.fill(path);
g2d.fill(rect2);
}
}
Mathematical solution :)
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
rotation.setToTranslation(500, 200);
rotation.rotate(angle, 32, 32);
rect1 = new Rectangle(0, 0, 64, 64);
path = new Path2D.Double(rect1, rotation);
double r = 32.0 * Math.sqrt(2);
// (532, 232) - coordinates of rectangle center |
// you can change position of second rectangle by this V substraction (all you need to know is that the full circle corresponds to 2Pi)
int x2 = (int) Math.round(532 + r * Math.cos(angle - Math.PI / 4));
int y2 = (int) Math.round(232 + r * Math.sin(angle - Math.PI / 4));
rect2 = new Rectangle(x2, y2, 10, 50);
g2d.fill(path);
g2d.fill(rect2);
}
Of course, some constants should be class fields, not method variables.
I can't test this code to be sure but I believe it is the proper working code that you want
int hw = -width / 2;
int hh = -height / 2;
int cos = Math.cos( theta );
int sin = Math.sin( theta );
int x = hw * cos - hh * sin;
int y = hw * sin + hh * cos;
This will get you the top left corner based on the theta, rotation, of the square. To get the other corners you just use change the hw and hh values:
//top right corner
hw = width / 2
hh = -height / 2
//bottom right corner
hw = width / 2
hh = height / 2
//bottom left corer
hw = -width / 2
hh = height / 2
I hope this helps
The following code is an animation of a clock.
What changes would have to be made in the program to make it not flicker?
Why does the field initiation have to take place in the addNotify() function for the hands to be displayed?
You can copy all of it into one .java file and it should work (flicker ;) ).
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ac extends Frame implements Runnable {
// for double buffering
Graphics og;
Image oi;
Thread t = null;
int width, height;
int timeH, timeM, timeS, radius = 50, lenH, lenM, lenS,
lenIn, cx, cy, x1, y1, x2, y2;
private boolean timeToQuit;
ac() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
stopRunning();
dispose();
}
});
setMinimumSize(new Dimension(300, 300));
EventQueue.invokeLater(new Runnable() {
public void run() {
setVisible(true);
}
});
}
public void addNotify() {
super.addNotify();
width = getBounds().width;
height = getBounds().height;
setSize(width, height);
lenH = 5 * radius / 10;
lenM = 6 * radius / 10;
lenS = 7 * radius / 10;
lenIn = 8 * radius / 10;
cx = width / 2;
cy = height / 2;
// for double buffering
oi = createImage(width, height);
og = oi.getGraphics();
}
public static void main(String [] args) {
ac clock = new ac();
clock.start();
}
public void start() {
if (t == null) {
t = new Thread(this);
t.start();
}
}
public void stopRunning() {
timeToQuit = true;
}
public void run() {
Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1);
do {
repaint();
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
}
}
while (!timeToQuit);
System.out.println("Quitting");
}
public void paint(Graphics g) {
og.setColor(new Color(170, 170, 170));
og.fillRect(0, 0, width, height);
Calendar cal = new GregorianCalendar();
timeH = cal.get(Calendar.HOUR);
timeM = cal.get(Calendar.MINUTE);
timeS = cal.get(Calendar.SECOND);
if (timeH >= 12)
timeH -= 12;
for (int i = 1 ; i < 13 ; i++) {
og.setColor(new Color(20, 20, 20));
x2 = (int) (cx + radius * Math.sin(i * 2 * 3.14159f / 12));
y2 = (int) (cy - radius * Math.cos(i * 2 * 3.14159f / 12));
if (i % 3 != 0) {
x1 = (int) (cx + 0.9f * radius * Math.sin(i * 2 * 3.14159f / 12));
y1 = (int) (cy - 0.9f * radius * Math.cos(i * 2 * 3.14159f / 12));
}
else {
x1 = (int) (cx + 0.8f * radius * Math.sin(i * 2 * 3.14159f / 12));
y1 = (int) (cy - 0.8f * radius * Math.cos(i * 2 * 3.14159f / 12));
}
og.drawLine(x1, y1, x2, y2);
og.setColor(new Color(230, 230, 230));
og.drawLine(x1 - 1, y1 - 1, x2 - 1, y2 - 1);
}
og.setColor(new Color(20, 20, 20));
x2 = (int) (cx + lenH * Math.sin((timeH + timeM / 60.0f + timeS / 3600.0f)
* 2 * 3.14159f / 12));
y2 = (int) (cy - lenH * Math.cos((timeH + timeM / 60.0f + timeS / 3600.0f)
* 2 * 3.14159f / 12));
og.drawLine(cx, cy, x2, y2);
og.setColor(Color.red);
og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);
og.setColor(new Color(20, 20, 20));
x2 = (int) (cx + lenM * Math.sin((timeM + timeS / 60.0f) * 2 * 3.14159f
/ 60));
y2 = (int) (cy - lenM * Math.cos((timeM + timeS / 60.0f) * 2 * 3.14159f
/ 60));
og.drawLine(cx, cy, x2, y2);
og.setColor(Color.green);
og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);
og.setColor(new Color(20, 20, 20));
x2 = (int) (cx + lenS * Math.sin(timeS * 2 * 3.14159f / 60));
y2 = (int) (cy - lenS * Math.cos(timeS * 2 * 3.14159f / 60));
og.drawLine(cx, cy, x2, y2);
og.setColor(Color.blue);
og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1);
og.setColor(new Color(20, 20, 20));
og.drawOval((width - 2 * radius) / 2, (height - 2 * radius) / 2,
2 * radius, 2 * radius);
og.setColor(new Color(230, 230, 230));
og.drawOval((width - 2 * radius) / 2 - 1, (height - 2 * radius) / 2 - 1,
2 * radius, 2 * radius);
g.drawImage(oi, 0, 0, this);
}
}
This uses awt which is old and it paints on to the main Frame - not the best way to make a swing UI. You can try this :
make the timer - sleep amount to less or more, try 300 or 800
use a Panel to draw on, add the panel to the frame
use swing (use a JComponent, it double buffers automatically, and a JFrame
For 2 and 3 you can keep original class to make Frame/JFrame and a new class for the java.awt.Panel or javax.swing.JComponent which is added to the former.
Does not flicker on my mac system, so i think its an OS + times its refreshed issue. Meaning suggestion 1 might work - try that first. Talking about line of code :
Thread.sleep(800);
Believe it or not, the problem was solved by adding the function:
public void update(Graphics g)
{
paint(g);
}
But the question remains, why? And why aren't the hands displayed when you initiate the class fields outside the addNotify() function?