How to interlock shapes in Java - java

I have a project where I have to draw Olympic Rings and I am having trouble making it seem like the ring interlock. This is a picture that shows what I mean.
notice how they interlock? That's what I want right now I only have the shapes sitting on top of each other. This is what I have right now.
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color; //sets color
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.AlphaComposite;
class ColoredOlypmicRings extends JFrame {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
float strokeThickness = 27.0f;
float arcThickness = 9.0f;
BasicStroke Outline = new BasicStroke(strokeThickness);
BasicStroke arcOutline = new BasicStroke(arcThickness);
g2d.setStroke(Outline);
g2d.setColor(Color.blue);
g2d.drawOval(100, 100, 300, 300);
g2d.setColor(Color.yellow);
g2d.drawOval(265, 300, 300, 300);
g2d.setColor(Color.black);
g2d.drawOval(430, 100, 300, 300);
g2d.setColor(Color.green);
g2d.drawOval(595, 300, 300, 300);
g2d.setColor(Color.red);
g2d.drawOval(760, 100, 300, 300);
g2d.setStroke(arcOutline);
g2d.setColor(Color.white);
g2d.drawArc(253, 378, 50, 75, -230, 58); // -270 start
g2d.drawArc(290, 370, 50, 75, -230, 58);
g2d.drawArc(360, 285, 50, 75, -230, 58);
g2d.drawArc(405, 285, 50, 75, -230, 58);
}
public ColoredOlypmicRings() {
setSize(1200, 800);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
ColoredOlypmicRings guiWindow = new ColoredOlypmicRings();
guiWindow.setVisible(true);
}
}

Draw arcs of colors that when combined form your circles rather than drawing whole ovals.
The top layer of arcs need to be drawn last. This may take a little trial and error, but you can do this.
General recommendation to never draw in a top level window such as a JFrame.
Instead draw within a JPanel's paintComponent method, and then display that JPanel within your JFrame
Always call the super's painting method in your overridden method.
Smooth your drawing by using Graphics2D setRenderingHints and turn anti-aliasing on.
First iteration -- not yet fully fixed:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import javax.swing.*;
#SuppressWarnings("serial")
public class OlympicRings extends JPanel {
private static final Color BG = Color.WHITE;
private static final int OVAL_WIDTH = 300;
private static final int OVAL_HEIGHT = OVAL_WIDTH;
private static final int X_START = 100;
private static final int Y_START = X_START;
private static final int DELTA_X = 175;
private static final int DELTA_Y = 180;
private static final Color COLOR_GOLD = new Color(242, 205, 25);
private static final Stroke INNER_STROKE = new BasicStroke(30f);
private static final Stroke OUTER_STROKE = new BasicStroke(40f);
private static final int ARC_LENGTH = 30;
private static final int CIRCLE_DEGREES = 360;
public OlympicRings() {
setBackground(BG);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = X_START;
int y = Y_START;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLUE, OVAL_WIDTH);
x += DELTA_X;
y += DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, COLOR_GOLD, OVAL_WIDTH);
x += DELTA_X;
y -= DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLACK, OVAL_WIDTH);
x += DELTA_X;
y += DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.GREEN.darker(), OVAL_WIDTH);
x += DELTA_X;
y -= DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.RED, OVAL_WIDTH);
x = X_START;
y = Y_START;
int angle = CIRCLE_DEGREES - ARC_LENGTH;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLUE, OVAL_WIDTH, angle, ARC_LENGTH);
x += DELTA_X;
y += DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, COLOR_GOLD, OVAL_WIDTH, 0, ARC_LENGTH);
x += DELTA_X;
y -= DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLACK, OVAL_WIDTH, angle, ARC_LENGTH);
x += DELTA_X;
y += DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.GREEN.darker(), OVAL_WIDTH, 0, ARC_LENGTH);
g2.dispose();
}
private void myDrawArc(Graphics2D g2, Stroke innerStroke, Stroke outerStroke, int x, int y,
Color bg2, Color color, int ovalWidth, int start, int end) {
g2.setStroke(outerStroke);
g2.setColor(bg2);
g2.drawArc(x, y, ovalWidth, ovalWidth, start, end);
g2.setStroke(innerStroke);
g2.setColor(color);
g2.drawArc(x, y, ovalWidth, ovalWidth, start, end);
}
private void myDrawOval(Graphics2D g2, Stroke innerStroke, Stroke outerStroke, int x, int y,
Color bg2, Color color, int ovalWidth) {
g2.setStroke(outerStroke);
g2.setColor(bg2);
g2.drawOval(x, y, ovalWidth, ovalWidth);
g2.setStroke(innerStroke);
g2.setColor(color);
g2.drawOval(x, y, ovalWidth, ovalWidth);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
int w = 2 * X_START + DELTA_X * 4 + OVAL_WIDTH;
int h = 2 * Y_START + DELTA_Y + OVAL_HEIGHT;
return new Dimension(w, h);
}
private static void createAndShowGui() {
OlympicRings mainPanel = new OlympicRings();
JFrame frame = new JFrame("OlympicRings");
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(() -> {
createAndShowGui();
});
}
}
This is not quite there as my background stroke extends beyond the end of the arc:
still working on it.
Slightly better:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import javax.swing.*;
#SuppressWarnings("serial")
public class OlympicRings extends JPanel {
private static final Color BG = Color.WHITE;
private static final int OVAL_WIDTH = 300;
private static final int OVAL_HEIGHT = OVAL_WIDTH;
private static final int X_START = 100;
private static final int Y_START = X_START;
private static final int DELTA_X = 175;
private static final int DELTA_Y = 180;
private static final Color COLOR_GOLD = new Color(242, 205, 25);
private static final Stroke INNER_STROKE = new BasicStroke(30f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND);
private static final Stroke OUTER_STROKE = new BasicStroke(40f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
private static final int ARC_LENGTH = 30;
private static final int CIRCLE_DEGREES = 360;
public OlympicRings() {
setBackground(BG);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = X_START;
int y = Y_START;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLUE, OVAL_WIDTH);
x += DELTA_X;
y += DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, COLOR_GOLD, OVAL_WIDTH);
x += DELTA_X;
y -= DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLACK, OVAL_WIDTH);
x += DELTA_X;
y += DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.GREEN.darker(), OVAL_WIDTH);
x += DELTA_X;
y -= DELTA_Y;
myDrawOval(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.RED, OVAL_WIDTH);
x = X_START;
y = Y_START;
int angle = CIRCLE_DEGREES - ARC_LENGTH;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLUE, OVAL_WIDTH, angle, ARC_LENGTH);
x += DELTA_X;
y += DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, COLOR_GOLD, OVAL_WIDTH, 0, ARC_LENGTH);
x += DELTA_X;
y -= DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLACK, OVAL_WIDTH, angle, ARC_LENGTH);
x += DELTA_X;
y += DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.GREEN.darker(), OVAL_WIDTH, 0, ARC_LENGTH);
g2.dispose();
}
private void myDrawArc(Graphics2D g2, Stroke innerStroke, Stroke outerStroke, int x, int y,
Color bg2, Color color, int ovalWidth, int start, int end) {
g2.setStroke(outerStroke);
g2.setColor(bg2);
g2.drawArc(x, y, ovalWidth, ovalWidth, start, end);
g2.setStroke(innerStroke);
g2.setColor(color);
g2.drawArc(x, y, ovalWidth, ovalWidth, start, end);
}
private void myDrawOval(Graphics2D g2, Stroke innerStroke, Stroke outerStroke, int x, int y,
Color bg2, Color color, int ovalWidth) {
g2.setStroke(outerStroke);
g2.setColor(bg2);
g2.drawOval(x, y, ovalWidth, ovalWidth);
g2.setStroke(innerStroke);
g2.setColor(color);
g2.drawOval(x, y, ovalWidth, ovalWidth);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
int w = 2 * X_START + DELTA_X * 4 + OVAL_WIDTH;
int h = 2 * Y_START + DELTA_Y + OVAL_HEIGHT;
return new Dimension(w, h);
}
private static void createAndShowGui() {
OlympicRings mainPanel = new OlympicRings();
JFrame frame = new JFrame("OlympicRings");
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(() -> {
createAndShowGui();
});
}
}
Latest change:
x = X_START;
y = Y_START;
int angle = CIRCLE_DEGREES - ARC_LENGTH;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLUE, OVAL_WIDTH, angle, ARC_LENGTH);
x += DELTA_X;
y += DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, COLOR_GOLD, OVAL_WIDTH, 57, ARC_LENGTH);
x += DELTA_X;
y -= DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.BLACK, OVAL_WIDTH, angle, ARC_LENGTH);
x += DELTA_X;
y += DELTA_Y;
myDrawArc(g2, INNER_STROKE, OUTER_STROKE, x, y, BG, Color.GREEN.darker(), OVAL_WIDTH, 57, ARC_LENGTH);
and the result:

This is the output of the code that the OP posted:
White border around rings - instead of attempting to hard-code the border only when rings cross each other, you should draw a white arc before you draw any color arc, with the white arc having a broader stroke than the colored one.
Overlaps - if you draw all rings one after the other, you will have to fix at least 4 overlaps so that it looks like the logo you're after. You should repaint each of those 4 overlaps (blue-over-yellow, yellow-over-black, black-over-green, green-over-red) by redrawing only the corresponding arc portion.
Colors & distances - use a color-picker tool to find the exact RGB values of the colors you are after from those in your sample image. You should also space your rings out horizontally a bit more, and draw the top row closer to the bottom row.
Edit: following my own advice, here's my output
And the program to generate it (import statements ommitted)
public static void paintRing(Graphics2D g, BasicStroke bs, BasicStroke fs,
double x, double y,
double r, double rw, Color color, int a0, int a) {
g.setColor(Color.white);
g.setStroke(new BasicStroke((float)rw*1.5f));
g.drawArc((int)x, (int)y, (int)r, (int)r, a0+5, a-10);
g.setColor(color);
g.setStroke(new BasicStroke((float)rw));
g.drawArc((int)x, (int)y, (int)r, (int)r, a0, a);
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put( RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
double w = getWidth();
// eyeballed measurements
double m = w/12;
double dx = w/7;
double dy = w/8;
double x = m;
double y = .7 * m;
double r = w/4;
// for partial lines
int fwdStart = -30;
int topStart = 90-30;
// background & foreground strokes
float rw = (float)w/40;
BasicStroke bs = new BasicStroke(rw*1.5f);
BasicStroke fs = new BasicStroke(rw);
// colors
Color blue = new Color(0, 133, 199);
Color gold = new Color(244, 195, 0);
Color black = Color.black;
Color green = new Color(0, 159, 61);
Color red = new Color(223, 0, 36);
paintRing(g2d, fs, bs, x, y, r, rw, blue, 0, 360);
paintRing(g2d, fs, bs, x+dx, y+dy, r, rw, gold, 0, 360);
paintRing(g2d, fs, bs, x+2*dx, y, r, rw, black, 0, 360);
// mesh blue-gold-black
paintRing(g2d, fs, bs, x+dx, y+dy, r, rw, gold, topStart, 60);
paintRing(g2d, fs, bs, x, y, r, rw, blue, fwdStart, 60);
paintRing(g2d, fs, bs, x+3*dx, y+dy, r, rw, green, 0, 360);
paintRing(g2d, fs, bs, x+4*dx, y, r, rw, red, 0, 360);
// mesh red-green-black
paintRing(g2d, fs, bs, x+3*dx, y+dy, r, rw, green, topStart, 60);
paintRing(g2d, fs, bs, x+2*dx, y, r, rw, black, fwdStart, 60);
}
public static void main(String[] args) {
JFrame jf = new JFrame("Test");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new Olympic());
jf.setSize(800, 400);
jf.setLocationByPlatform(true);
jf.setVisible(true);
}

Related

Java Graphics - Remove part of rounded rectangle under text?

I'm trying to render text on top of a rounded rectangle, but I want the part of the rounded rectangle under the text to be cut out. Here's what I want it to look like:
The problem is that I can't find any easy way of doing this. I tried using clearRect, but that just creates a black rectangle, and I want to have an image underneath (for now it's just white).
I then had the idea that maybe I could just fill the area I want to remove of the rectangle with white, then filter out all the white pixels. This didn't work as well as I hoped, as there are still white pixels left over:
Here's the code I have currently:
public static void createRoundedRectImg(int width, int height)
{
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
int padding = 50;
g.setColor(Color.BLUE);
g.drawRoundRect(padding, padding, width - (padding * 2), height - (padding * 2), 50, 50);
float textSize = 84f;
Font font = g.getFont().deriveFont(textSize).deriveFont(Font.BOLD);
g.setFont(font);
String text = "TEXT";
Rectangle2D stringBounds = g.getFontMetrics(font).getStringBounds(text, g);
int textWidth = (int) stringBounds.getWidth();
int textHeight = (int) (stringBounds.getHeight() + g.getFontMetrics(font).getDescent());
int textX = (width / 2) - (textWidth / 2);
int textY = g.getFontMetrics(font).getDescent() * 2 + padding;
//g.clearRect(textX, textY - textHeight, textWidth, textHeight);
g.setColor(Color.WHITE);
g.fillRect(textX, textY - textHeight, textWidth, textHeight);
g.setColor(Color.GREEN);
g.drawString(text, textX, textY);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Color c = new Color(img.getRGB(x, y));
if (c.getRGB() == Color.WHITE.getRGB())
img.setRGB(x, y, new Color(0, 0, 0, 255).getRGB());
}
}
g.dispose();
}
Is there a simpler way of just clearing the part of the rounded rectangle under the text? After that is done I want to overlay the whole thing on top of an image, so I need the background to be transparent.
You could use the subtract method of the Area class to remove a rectangular section from a stroked RoundRectangle2D.
float strokeWidth = 1.5f;
RoundRectangle2D roundedRect = new RoundRectangle2D.Double(padding, padding, width - (padding * 2), height - (padding * 2), 50, 50);
Rectangle2D rectMask = new Rectangle2D.Double(textX, padding-strokeWidth, textWidth, 2*strokeWidth);
Stroke stroke = new BasicStroke(strokeWidth);
Area roundedRectArea = new Area(stroke.createStrokedShape(roundedRect));
roundedRectArea.subtract(new Area(rectMask));
g.setColor(Color.BLACK);
g.fill(roundedRectArea);
g.drawString(text, textX, textY);
Which produces:
Full code:
public static void createRoundedRectImg(int width, int height)
{
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
float textSize = 84f;
Font font = g.getFont().deriveFont(textSize).deriveFont(Font.BOLD);
g.setFont(font);
int padding = 50;
String text = "TEXT";
Rectangle2D stringBounds = g.getFontMetrics(font).getStringBounds(text, g);
int textWidth = (int) stringBounds.getWidth();
int textX = (width / 2) - (textWidth / 2);
int textY = g.getFontMetrics(font).getDescent() * 2 + padding;
float strokeWidth = 1.5f;
RoundRectangle2D roundedRect = new RoundRectangle2D.Double(padding, padding, width - (padding * 2),
height - (padding * 2), 50, 50);
Rectangle2D rectMask = new Rectangle2D.Double(textX, padding - strokeWidth, textWidth, 2 * strokeWidth);
Stroke stroke = new BasicStroke(strokeWidth);
Area roundedRectArea = new Area(stroke.createStrokedShape(roundedRect));
roundedRectArea.subtract(new Area(rectMask));
g.setColor(Color.BLACK);
g.fill(roundedRectArea);
g.drawString(text, textX, textY);
g.dispose();
try
{
ImageIO.write(img, "png", new File("round.png"));
} catch (IOException e)
{
e.printStackTrace();
}
}
Try this.
public static void createRoundedRectImg(int width, int height)
{
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D)img.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
int padding = 50;
g.setComposite(AlphaComposite.Clear);
g.fillRoundRect(padding, padding, width - (padding * 2), height - (padding * 2), 50, 50);
g.setComposite(AlphaComposite.SrcOver);
g.setColor(Color.BLUE);
g.drawRoundRect(padding, padding, width - (padding * 2), height - (padding * 2), 50, 50);
float textSize = 84f;
Font font = g.getFont().deriveFont(textSize).deriveFont(Font.BOLD);
g.setFont(font);
String text = "TEXT";
Rectangle2D stringBounds = g.getFontMetrics(font).getStringBounds(text, g);
int textWidth = (int) stringBounds.getWidth();
int textHeight = (int) (stringBounds.getHeight() + g.getFontMetrics(font).getDescent());
int textX = (width / 2) - (textWidth / 2);
int textY = g.getFontMetrics(font).getDescent() * 2 + padding;
g.setColor(Color.WHITE);
g.fillRect(textX, textY - textHeight, textWidth, textHeight);
g.setColor(Color.GREEN);
g.drawString(text, textX, textY);
g.dispose();
}
You could just use a TitledBorder. Since the curve is proportional to the line size I created a RoundedBorder class with most of the code from the paintBorder() method in the API to allow the size of the arc corners to be specified. It is now a simple pixel amount for both width and height of the arc radius.
first, create a RoundedBorder instance. Try 30 for the arc radius.
then, using the BorderFactor, create a TitledBorder instance and pass the rounded instance as the first agument.
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class TitledBorderDemo extends JPanel {
JFrame frame = new JFrame();
public static void main(String[] args) {
SwingUtilities
.invokeLater(() -> new TitledBorderDemo().start());
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public void start() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border b = new RoundedBorder(Color.black, 2, 30);
Border titled = BorderFactory.createTitledBorder(b, "Text",
TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,
new Font("Arial", Font.BOLD, 48));
setBorder(titled);
frame.add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class RoundedBorder extends LineBorder {
private int arc;
public RoundedBorder(Color color, int lineThickness, int arc) {
super(color, lineThickness);
this.arc = arc;
}
#Override
public void paintBorder(Component c, Graphics g, int x, int y,
int width, int height) {
if ((this.thickness > 0) && (g instanceof Graphics2D)) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Color oldColor = g2d.getColor();
g2d.setColor(this.lineColor);
Shape outer;
Shape inner;
int offs = this.thickness;
int size = offs + offs;
outer = new RoundRectangle2D.Float(x, y, width, height,
arc, arc);
inner = new RoundRectangle2D.Float(x + offs, y + offs,
width - size, height - size, arc, arc);
Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
path.append(outer, false);
path.append(inner, false);
g2d.fill(path);
g2d.setColor(oldColor);
}
}
}
The above, when run, produces the following image.

Java: Rotate Rectangle

I am trying to rotate a Rectangle from the vertices closest to the origin, but it is placing it way off the frame.
I have the following class:
class MyRectangle {
MyRectangle(int x, int y, int w, int h, int a){
xPos = x;
yPos = y;
width = w;
height = h;
angle = a;
}
public int xPos;
public int yPos;
public int width;
public int height;
public int angle;
public void paintComponent(Graphics g) {
g.drawRect(xPos, yPos, width, height);
Rectangle rect2 = new Rectangle(xPos, yPos, width, height);
Graphics2D gg = (Graphics2D) g.create();
AffineTransform transform = new AffineTransform();
transform.rotate(angle, rect2.getX() + rect2.width/2, rect2.getY() + rect2.height/2);
AffineTransform old = gg.getTransform();
gg.transform(transform);
gg.rotate(Math.toDegrees(-angle));
gg.draw(rect2);
}
}
I am caling the rectangle with:
MyRectangle rect = new MyRectangle(10, 200, 30, 50, 70);
It is showing up like this:
I included the original un-rotated rectangle. It should rotate from the bottom left vertex.

Drawing graphics in Java

I am trying to draw a circle in side a square and having multiple square circles in java. I am almost done but my output isn't coming out as I wanted to. The picture is what I am trying to do but it's not working out.
Here is my code:
a.awt.*;
public class SquaredCircles {
public static final int WIDTH=400;
public static final int HEIGHT=400;
public static void main (String[] args) {
DrawingPanel panel = new DrawingPanel(WIDTH,HEIGHT);
Graphics g = panel.getGraphics ();
panel.setBackground(new Color(0, 255, 255 ) );
int x = 0;
int y = 0;
int size = 100;
int rows = 5;
int numSquares = 1;
drawManySquares ( g, numSquares, x, y, size, rows );
x = 10;
y = 120;
size = 24;
rows = 4;
numSquares = 4;
drawManySquares( g, numSquares, x, y, size, rows );
x = 150;
y = 20;
size = 40;
rows = 6;
numSquares = 5;
drawManySquares( g, numSquares, x, y, size, rows );
x = 130;
y = 275;
size = 36;
rows = 3;
numSquares = 3;
drawManySquares( g, numSquares, x, y, size, rows );
}
public static void drawManySquares( Graphics g, int numSquares, int x, int y, int size, int rows ) {
for ( int i = 0; i < numSquares; i++ ) {
for ( int j = 0; j < numSquares; j++ ) {
drawOneSquare( g, x + i size, y + j size, size, rows );
}
}
}
public static void drawOneSquare( Graphics g, int x, int y, int size, int rows ) {
g.setColor ( Color.GREEN);
g.fillRect(x , y, size, size);
g.setColor ( Color.YELLOW);
g.fillOval ( x, y, size, size);
g.setColor ( Color.BLACK);
g.drawLine(size / 2, x, size / 2, size);
g.setColor ( Color.BLACK);
g.drawLine(x, size / 2, size, size / 2);
for (int i = 0; i <= rows; i = i + 1) {
g.setColor ( Color.BLACK);
g.drawOval(x + (i* (size/rows)), y+ (i*(size/rows)), size - (i*(size/rows +10 )) , size - (i*(size/rows +10)));
}
}
}
Start by having a look at Painting in AWT and Swing and Performing Custom Painting to see how painting should be done in Swing
Break down your problem into manageable chunks. The first thing you need to be able to do is paint a circle of a given size at a specific location, something like
public void paintCircleAt(Graphics2D g2d, int radius, int centerX, int centerY, Color stroke, Color fill) {
Ellipse2D.Double circle = new Ellipse2D.Double(centerX - radius, centerY - radius, radius * 2, radius * 2);
g2d.setColor(fill);
g2d.fill(circle);
g2d.setColor(stroke);
g2d.draw(circle);
}
So, this allows you to paint a circle of a given radius around the center points of x/y filled and outlined with the specified color, pretty simple.
Now, you need someway to paint a series of circles around the same center point, something like...
public void paintCirclesIn(Graphics2D g2d, int count, int radius, int centerX, int centerY, Color stroke, Color fill) {
System.out.println(radius + "; " + centerX + "; " + centerY);
int delta = radius / count;
int innerRadius = radius;
for (int index = 0; index < count; index++, innerRadius -= delta) {
paintCircleAt(g2d, innerRadius, centerX, centerY, stroke, fill);
}
}
Okay, this basically calculates the difference (delta) between each circle and the paints that many circles with that much difference in their radius from the previous one. Because of the way the painting is done, we start with the outer circle and paint in.
And finally, we need someway to paint a square and circles, something like...
public void paintCirclesInSquare(Graphics2D g2d, int count, int x, int y, int width, int height, Color squareStroke, Color squareFill, Color circleStroke, Color circleFill) {
int centerX = x + (width / 2);
int centerY = y + (height / 2);
int radius = Math.min(centerX, centerY);
Rectangle2D box = new Rectangle2D.Double(x, y, width, height);
g2d.setColor(squareFill);
g2d.fill(box);
g2d.setColor(squareStroke);
g2d.draw(box);
paintCirclesIn(g2d, count, radius, centerX, centerY, circleStroke, circleFill);
g2d.drawLine(centerX, y, centerX, y + height);
g2d.drawLine(x, centerY, x + width, centerY);
}
This, again, simply reuses the existing code we already have and adds to it, painting the square, the circles in the square and finally the lines.
Now, from here, you could write a method which took the number of columns/rows you wanted, the x/y position to start from, the size of each of square, the number of circles you need and the colors and reuse this functionality, but I'll leave that up to you ;)
Runnable example for you to play with...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class CirclesAndSquares {
public static void main(String[] args) {
new CirclesAndSquares();
}
public CirclesAndSquares() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
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);
int x = getWidth() / 2;
int y = getHeight() / 2;
// paintCircleAt(g2d, Math.min(x, y), y, y, Color.BLACK, Color.YELLOW);
// paintCirclesIn(g2d, 5, Math.min(x, y), x, y, Color.BLACK, Color.YELLOW);
paintCirclesInSquare(g2d, 5, 0, 0, getWidth() - 1, getHeight() - 1, Color.BLACK, Color.GREEN, Color.BLACK, Color.YELLOW);
g2d.dispose();
}
public void paintCirclesInSquare(Graphics2D g2d, int count, int x, int y, int width, int height, Color squareStroke, Color squareFill, Color circleStroke, Color circleFill) {
int centerX = x + (width / 2);
int centerY = y + (height / 2);
int radius = Math.min(centerX, centerY);
Rectangle2D box = new Rectangle2D.Double(x, y, width, height);
g2d.setColor(squareFill);
g2d.fill(box);
g2d.setColor(squareStroke);
g2d.draw(box);
paintCirclesIn(g2d, count, radius, centerX, centerY, circleStroke, circleFill);
g2d.drawLine(centerX, y, centerX, y + height);
g2d.drawLine(x, centerY, x + width, centerY);
}
public void paintCirclesIn(Graphics2D g2d, int count, int radius, int centerX, int centerY, Color stroke, Color fill) {
System.out.println(radius + "; " + centerX + "; " + centerY);
int delta = radius / count;
int innerRadius = radius;
for (int index = 0; index < count; index++, innerRadius -= delta) {
paintCircleAt(g2d, innerRadius, centerX, centerY, stroke, fill);
}
}
public void paintCircleAt(Graphics2D g2d, int radius, int centerX, int centerY, Color stroke, Color fill) {
Ellipse2D.Double circle = new Ellipse2D.Double(centerX - radius, centerY - radius, radius * 2, radius * 2);
g2d.setColor(fill);
g2d.fill(circle);
g2d.setColor(stroke);
g2d.draw(circle);
}
}
}

drawing squares using a recursive method with java

I am trying to write an applet that draws a main square and then uses a recursive method that draws smaller squares on the corners of the main square. I'm really confused on how to go about this. I have drawn the square with the others squares on its corners but i need to do this process recursively and thats where i get lost. I need to set a min side length so the recursive method knows when to stop. Here is my code.
import javax.swing.JApplet;
import java.awt.*;
public class LabC extends JApplet
{
public void paint(Graphics g)
{
g.drawRect(50, 100, 100, 100);
g.drawRect(25, 75, 50, 50);
g.drawRect(125, 75, 50, 50);
g.drawRect(125, 175, 50, 50);
g.drawRect(25, 175, 50, 50);
}
}
I think this is more or less what are you looking for
public static class LabC extends JLabel {
public void paintRecursiveWraper(Graphics g, int minW, int minH, int x, int y, int w, int h) {
g.drawRect(x, y, w, h);
paintRecusive(g, minW, minH, x, y, w, h);
}
public void paintRecusive(Graphics g, int minW, int minH, int x, int y, int w, int h) {
if (h <= minH || w <= minW) {
return;
}
int newW, newH;
int newX, newY;
newW = (int) (w * scaleFactor);
newH = (int) (h * scaleFactor);
// Bot Left Square
newX = x;
newY = y;
g.drawRect(newX, newY, newW, newH);
paintRecusive(g, minW, minH, newX, newY, newW, newH);
// Bot Right Square
newX = (int) (x + w * (1 - scaleFactor));
newY = y;
g.drawRect(newX, newY, newW, newH);
paintRecusive(g, minW, minH, newX, newY, newW, newH);
// Top Left Square
newX = x;
newY = (int) (y + h * (1 - scaleFactor));
g.drawRect(newX, newY, newW, newH);
paintRecusive(g, minW, minH, newX, newY, newW, newH);
// Top Right Square
newX = (int) (x + w * (1 - scaleFactor));
newY = (int) (y + h * (1 - scaleFactor));
g.drawRect(newX, newY, newW, newH);
paintRecusive(g, minW, minH, newX, newY, newW, newH);
}
public void paint(Graphics g) {
paintRecursiveWraper(g, 10, 10, 0, 0, 1000, 1000);
}
}
scaleFactor has to be between 0 and 1
Edited*

Canvas - good rendering practices?

I've been using the Canvas a lot lately for little Java games and i've noticed a lot of strange things can happen. For example, earlier today I created a little game in which the objective is to shoot enemy ships and get points. The game draws tiny 32x32 images on the screen (sometimes slightly bigger) and for a reason I am oblivious to, the game renders oddly.
For example, my ship has a health bar above it's head:
As you can see by the image, the textures are really small. Despite this, the game lags and sometimes things render incorrectly like this for example:
If you look closely at the top of the health bar, you can see that it's been shifted upwards slightly, it puzzles me how his happens as all of my rendering is buffered.
My rendering code:
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics2D g = (Graphics2D)bs.getDrawGraphics();
toDrawG.setColor(new Color(0x222222));
toDrawG.fillRect(0, 0, WIDTH, HEIGHT);
draw((Graphics2D)toDrawG);
g.drawImage(toDraw, 0, 0, null);
g.dispose();
bs.show();
}
public void draw(Graphics2D g){
if(Settings.planets){
renderer.renderPlanets();
}
if(level != null){
for(int i = 0 ; i < level.entityList.size(); i++){
if(level.entityList.get(i) != null){
level.entityList.get(i).render(renderer);
}
}
}
renderer.overlayString("Space Game", 20, 20, 24, 0xFFFFFF);
renderer.overlayString(VERSION, 20, 50, 24, 0xFFFFFF);
renderer.overlayString("FPS: " + renderer.fps, 20, 70, 24, 0xFFFFFF);
renderer.overlayString("Ships spawned: " + level.shipsSpawned, 20, 90, 24, 0xFFFFFF);
renderer.overlayString("Time Survived: " + level.time / 100 + "s", 20, 110, 24, 0xFFFFFF);
renderer.overlayString("Physics FPS: " + fps, 20, 130, 24, 0xFFFFFF);
if(currentGui != null){
currentGui.render(renderer);
}else{
map.drawMinimap(SpaceGame.WIDTH-Minimap.WIDTH-20, SpaceGame.HEIGHT- Minimap.HEIGHT-30);
}
}
And my "Render.class" if you need to study it:
package com.maestrum;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.util.Random;
public class Render implements Runnable{
public Graphics2D g2d;
public double xScroll,yScroll;
public int frames;
public int fps;
public long lastTime;
public int[] pSequence = new int[40];
public SpaceGame game;
public Entity trackedEntity;
public Random rand;
public Render(SpaceGame game, Graphics2D g){
this.game = game;
this.g2d = g;
this.rand = new Random();
for(int i = 0 ; i < 40; i++){
pSequence[i] = rand.nextInt(15) + 1;
}
}
#Override
public void run() {
renderLoop();
}
public void renderLoop(){
while(true){
game.render();
if(System.currentTimeMillis() - lastTime >= 1000){
fps = frames;
frames = 0;
lastTime = System.currentTimeMillis();
}
frames++;
}
}
public void renderPlanets(){
overlayImage(ImageHandler.background, 0, 0, 1.5);
for(int i = 0 ; i < 20; i++){
overlayImage(ImageHandler.planets[pSequence[i]/4][pSequence[i]%4], i * 400 - xScroll/pSequence[i], i * pSequence[i]*40 - yScroll/pSequence[i]*2, pSequence[i]);
}
}
private class PlanetRenderer {
}
public void overlayString(String s, double x, double y, int fontSize, int colour){
drawString(s, x+xScroll, y+yScroll, fontSize, colour);
}
public void overlayRectangle(double x, double y, int xs, int ys, int colour){
drawRectangle(x+xScroll, y+yScroll, xs, ys, colour);
}
public void overlayBlurred(BufferedImage img, double x, double y, double scale){
drawImageBlurred(img, x+xScroll, y+yScroll, scale);
}
public void overlayImage(BufferedImage img, double x, double y, double scale){
drawImage(img, x+xScroll, y+yScroll, scale);
}
public BufferedImage execute(BufferedImage img) {
// TODO Auto-generated method stub
float weight = 1.0f/2.0f;
float [] elements = {weight, weight, weight, weight, weight, weight, weight, weight, weight};
Kernel k = new Kernel (3,3,elements);
ConvolveOp op = new ConvolveOp(k);
BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
op.filter(img, dest);
return dest;
}
public void drawImageBlurred(BufferedImage img, double x, double y, double scale){
x -= xScroll;
y -= yScroll;
BufferedImage image = new BufferedImage((int)(img.getWidth()*scale), (int)(img.getHeight()*scale), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.drawImage(img, 0, 0, (int)(img.getWidth()*scale), (int)(img.getHeight()*scale), null);
execute(image);
g2d.drawImage(image, (int)x, (int)y, null);
g.dispose();
}
public void drawString(String s, Vector2D pos, int fontSize, int colour){
drawString(s, pos.x, pos.y, fontSize, colour);
}
public void drawString(String s, double x, double y, int fontSize, int colour){
if(s == null){
return;
}
x -= xScroll;
y -= yScroll;
BufferedImage img = new BufferedImage(s.length()*fontSize+1, fontSize*2, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(new Color(colour));
g.setFont(new Font("Consolas", Font.BOLD, fontSize));
g.drawString(s, 0, img.getHeight()/2);
g2d.drawImage(img, (int)x, (int)y, null);
g.dispose();
}
public void drawImage(BufferedImage img, Vector2D pos, double scale){
drawImage(img, pos.x, pos.y, scale);
}
public void drawLine(Vector2D v1, Vector2D v2, int colour, int width){
drawLine(v1.x, v1.y, v2.x, v2.y, colour, width);
}
public void drawLine(double x1, double y1, double x2, double y2, int colour, int lWidth){
x1 -= xScroll;
y1 -= yScroll;
x2 -= xScroll;
y2 -= yScroll;
g2d.setColor(new Color(colour));
g2d.setStroke(new BasicStroke(lWidth));
g2d.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
}
public void drawImage(BufferedImage img, double x, double y, double scale){
x -= xScroll;
y -= yScroll;
BufferedImage image = new BufferedImage((int)(img.getWidth()*scale), (int)(img.getHeight()*scale), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.drawImage(img, 0, 0, (int)(img.getWidth()*scale), (int)(img.getHeight()*scale), null);
g2d.drawImage(image, (int)x, (int)y, null);
g.dispose();
}
public void drawRectangle(Vector2D pos, int xs, int ys, int colour){
drawRectangle(pos.x, pos.y, xs, ys, colour);
}
public void drawRectangle(double x, double y, int xs, int ys, int colour){
if(xs <= 0){
return;
}
x -= xScroll;
y -= yScroll;
BufferedImage image = new BufferedImage(xs, ys, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(new Color(colour));
g.fillRect(0, 0, xs, ys);
g2d.drawImage(image, (int)x, (int)y, null);
g.dispose();
}
public void drawImageRotated(BufferedImage img, Vector2D pos, double scale, double angle) {
drawImageRotated(img, pos.x, pos.y, scale, angle);
}
public void drawImageRotated(BufferedImage img, double x, double y, double scale, double angle) {
x -= xScroll;
y -= yScroll;
BufferedImage image = new BufferedImage((int)(img.getWidth() * 1.5D), (int)(img.getHeight() * 1.5D), 2);
Graphics2D g = (Graphics2D)image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.rotate(Math.toRadians(angle), image.getWidth() / 2, image.getHeight() / 2);
g.drawImage(img, image.getWidth() / 2 - img.getWidth() / 2, image.getHeight() / 2 - image.getHeight() / 2, null);
g2d.drawImage(image, (int)(x-image.getWidth()*scale/2), (int)(y-image.getHeight()*scale/2), (int)(image.getWidth()*scale), (int)(image.getHeight()*scale), null);
g.dispose();
}
}
As you can see in the rendering class, the render process is done as many times a second as possible. This is to give the game the highest possible FPS. If you missed the point of what I'm asking; What good practices should I take into account when rendering stuff using Java? And, what could possibly be causing the space ships health bar to render like so?
NOTE: take a look at this video, I ran that program and got 80FPS with 50000 particles, however with my rendering code (which is obviously of a much lower quality) I can render only a mere 100 or so particles, before things start messing up.
http://www.youtube.com/watch?v=6M3Ze4Eu87Y
This is my tick() function, it gets called every game tick (10ms)
public void tick(){
if(System.currentTimeMillis() - lastTime >= 1000){
fps = frames;
frames = 0;
lastTime = System.currentTimeMillis();
}
frames++;
if(renderer.trackedEntity != null){
renderer.xScroll = renderer.trackedEntity.pos.x-SpaceGame.WIDTH/2;
renderer.yScroll = renderer.trackedEntity.pos.y-SpaceGame.HEIGHT/2;
}
if(level != null && !paused){
level.tick();
}
if(currentGui != null && currentGui.pausesGame()){
paused = true;
}else{
paused = false;
}
}
i think the answer by #mantrid should fix your problem.
as far as performance goes ... there are some obvious "sins" in your code:
Don't draw an image into an image to draw an image
public void drawImage(BufferedImage img, double x, double y, double scale){
x -= xScroll;
y -= yScroll;
BufferedImage image = new BufferedImage((int)(img.getWidth()*scale), (int)(img.getHeight()*scale), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.drawImage(img, 0, 0, (int)(img.getWidth()*scale), (int)(img.getHeight()*scale), null);
g2d.drawImage(image, (int)x, (int)y, null);
g.dispose();
}
I don't see the point of this. Why not just do this:
public void drawImage(BufferedImage img, double x, double y, double scale){
g2d.drawImage(img, (int)(x-xScroll), (int)(y-yScroll), (int)(img.getWidth()*scale), (int)(img.getHeight()*scale), null);
}
AAAAAAAAAAAAA
The next one actually burnt my eyes
public void drawRectangle(double x, double y, int xs, int ys, int colour){
if(xs <= 0){
return;
}
x -= xScroll;
y -= yScroll;
BufferedImage image = new BufferedImage(xs, ys, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(new Color(colour));
g.fillRect(0, 0, xs, ys);
g2d.drawImage(image, (int)x, (int)y, null);
g.dispose();
}
Why? This is so much simpler and quicker:
public void drawRectangle(double x, double y, int xs, int ys, int colour){
if(xs <= 0)
return;
g2d.setColor(new Color(colour));
g2d.fillRect((int)(x-xScroll), (int)(y-yScroll), xs, ys);
}
The same goes for the drawImageRotated and drawString.
Just draw to the g2d buffer directly, what are you afraid of?
drawImageBlurred
First of all... you're doing it again!
Second: You seem to be applying a convolve operation to an image on each frame, why not just do that operation once (e.g. at the start of the app, or even better yet, in an image editing program).
I don't mean this in a bad way at all, but you are clearly not very experienced with programming in general. I think you could take a look at processing (http://processing.org). I'm sort of biased here because i'm using it myself almost everyday. It is a great learning and prototyping environment written in java that should allow you to stop thinking about implementation details (like flipping buffers) and focus on what you really care for (make a game!).
Alright, hope what i said makes sense. Good luck with your coding! :)
The health bar might be shifted because when game lags, xScroll and yScroll are updated while components/overlays are still being rendered into backbuffer.
To fix that:
1) move game objects proportionally to amout of time elapsed from latest update to keep game speed always constant. add delta parameter to level.tick() and all game objects update method
2) put level.tick() and game.render() within the same loop sequence to ensure game objects are updated first:
currentTime = System.currentTimeMillis();
delta = currentTime - lastTime;
if(level != null && !paused){
level.tick(delta); // introduce delta here
game.render();
}
lastTime = currentTime;
In general, consider these additional steps:
1) set Component.setIgnoreRepaint(true) to speed up rendering
2) optimize images for current display at the start
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
Image optimized = gc.createCompatibleImage(img.getWidth(),img.getHeight(),Transparency.BITMASK);
optimized.getGraphics().drawImage(unoptimized, 0, 0, null);

Categories