Java - Color Rectangle - java

import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
int height = getHeight();
int width = getWidth();
g.drawRect(350, 510, 110, 170);
g.drawRect(470, 510, 110, 170);
g.drawRect(590, 510, 110, 170);
g.drawRect(710, 510, 110, 170);
g.drawRect(830, 510, 110, 170);
g.drawRect(350, 30, 110, 170);
g.drawRect(470, 30, 110, 170);
g.drawRect(590, 30, 110, 170);
g.drawRect(710, 30, 110, 170);
g.drawRect(830, 30, 110, 170);
g.setColor(Color.RED);
g.drawRect(110, 450, 110, 170);
g.drawRect(110, 60, 110, 170);
}
}
I need to color red every Rectangle ( i mean inside the Rectangle ), but with this g.setColor ( Color.RED ) ; i can only color the exterior part of Rectanlge

drawRect() from the JavaDocs
Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color.
That's why you need to use fillRect:
Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.
From your last comment: And what about if i want to have the half rectangle blue and the rest red? What should i do then ?
Draw 2 rectangles, one ends where the other one starts, something like:
g.setColor(Color.BLUE);
g.fillRect(50, 50, 50, 50);
g.setColor(Color.RED);
g.fillRect(100, 50, 50, 50);
I haven't tested the above code, but you get the idea :)

g.drawRect() only draws rectangle's border. You probably should use g.fillRect() which fills your rectangle with solid color. JavaDoc

Use fillRect() to fill a rectangular area rather than just drawing a rectangle.

Try this:
g.fillRect(x, y, width, height)
Description here!

Related

Drawing Rectangles in Java

I am trying to draw rectangles in Java like this picture:
I visualize the coordinates as I do at math but I come up with the rectangles turned upside down which is like this:
I know Im missing just a few things.What should I do?
(Colors will be edited)
public class BlockTower
{
public static void main(String[] args)
{
Rectangle rect1 = new Rectangle(20, 70, 40, 30);
rect1.draw();
rect1.setColor(Color.BLUE);
rect1.fill();
Rectangle rect2 = new Rectangle(60, 70, 40, 30);
rect2.draw();
rect2.setColor(Color.MAGENTA);
rect2.fill();
Rectangle rect3 = new Rectangle(100, 70, 40, 30);
rect3.draw();
rect3.setColor(Color.CYAN);
rect3.fill();
Rectangle rect4 = new Rectangle(40, 100, 40, 30);
rect4.draw();
rect4.setColor(Color.RED);
rect4.fill();
Rectangle rect5 = new Rectangle(80, 100, 40, 30);
rect5.draw();
rect5.setColor(Color.PINK);
rect5.fill();
Rectangle rect6 = new Rectangle(60, 130, 40, 30);
rect6.draw();
rect6.setColor(Color.BLUE);
rect6.fill();
//TODO finish the draft to display the six blocks
}
}
Coordinates in Swing start from Top Left. That means you have to recalculate your y-coordinates. So the bottom of your panel is actually at the current height.
If you've calculated something to be at coordinates (x,y) it now has to be at coordinates (x, height - y) instead.

Rectangle thickness java

I need to outline in blue 1 rectangle at a time when selected. The code works but I cant see the blue outline very well. what is the easiest way to make the outline thicker. Wither that be drawing another shape over it or something else.
private void displayBlock(Graphics g)
{
for(int i = 0; i < 10; i++)
{
if (occupant[i].equals("Empty"))
{
//Sets the rectangle red to show the room is occupied
g.setColor(emptyColor);
g.fillRect(150+(i*50), 120, aptWidth, aptHeight);
}
else
{
//Sets the rectangle green to show the room is free
g.setColor(Color.red);
g.fillRect(150+(i*50), 120, aptWidth, aptHeight);
}
if (i == selectedApartment)
{
g.setColor(Color.blue);
g.drawRect(150+(i*50), 120, aptWidth, aptHeight);
}
else
{
g.setColor(Color.black);
g.drawRect(150+(i*50), 120, aptWidth, aptHeight);
}
// Draws the numbers in the Corresponding boxes
g.setColor(Color.black);
g.drawString(Integer.toString(i+1), 150+(i*50)+15, 120+25);
}
//Drawing the key allowing the user to see what the colours refer to
g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
g.drawString("Key", 20,120);
g.drawString("Occupied",60,150);
g.drawString("Empty", 60, 170);
// Drawing the boxes to show what the colours mean in the key
g.setColor(Color.red);
g.fillRect(20, 130, 25, 25);
g.setColor(Color.black);
g.drawRect(20, 130, 25, 25);
g.setColor(Color.green);
g.fillRect(20,150,25,25);
g.setColor(Color.black);
g.drawRect(20,150,25,25);
} // End of displayBlock
You can set the thickness of the rectangle by creating a Graphics2D object and setting its stroke.
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g.create();
g2.setStroke(new java.awt.BasicStroke(3)); // thickness of 3.0f
g2.setColor(Color.blue);
g2.drawRect(10,10,50,100); // for example
Maybe you could draw a second rectangle immediately inside the first one. Or multiple of them.
public static void drawRectangle(Graphics g, int x, int y, int width, int height, int thickness) {
g.drawRect(x, y, width, height);
if (thickness > 1) {
drawRectangle(g, x + 1, y + 1, width - 2. height - 2, thickness - 1);
}
}

Java 2D Graphics rotating objects keeping original coords system?

I've already searched this topic everywhere on the Internet, but nothing.
I'm programming with JAVA and I'm painting in a JPanel some shapes like yellow stars with a sort of spaceship in the center of the screen. My purpose is to make the entire scene, apart from the spaceship which remains stationary at the center, rotate around the center (and of course around the spaceship) when I press a certain button.
Now, when I rotate the stars I make them rotate around the center point wich changes as the spaceship moves, but when I do a rotation, the coordinates system changes and the ship moves diagonally, while I want to rotate the scene keeping possible to move the spaceship vertically and horizontally.
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
// RIEMPIMENTO SFONDO
g2.setColor(new Color(10, 10, 10));
g2.fillRect(0, 0, 1366, 768);
// DEBUGGER
g2.setColor(Color.WHITE);
g2.drawString("|" + posx + "|" + posy + "| " + scale, 1250, 758);
// TRASFORMAZIONE NAVICELLA
AffineTransform at = new AffineTransform();
at.translate(683, 384);
at.scale(scale, scale);
g2.setTransform(at);
AffineTransform backup = g2.getTransform();
// DISEGNO NAVICELLA
g2.setColor(Color.LIGHT_GRAY);
int[] xp = {0, -25, -25, 0, 25, 25};
int[] yp = {-25, 0, 25, 0, 25, 0};
g2.drawPolygon(xp, yp, 6);
// TRASFORMAZIONE SPAZIO
at.translate(posx, posy);
at.rotate(Math.toRadians(rotation), -posx, -posy);
g2.setTransform(at);
// DISEGNO SPAZIO
STAR(g2, 300 + posx, 100 + posy, 200, 200, Color.YELLOW);
STAR(g2, -100 + posx, -200 + posy, 200, 200, Color.YELLOW);
g2.dispose();
}

Build Java applet to draw series of houses using a loop

I'm very new to Java, and find myself having a bit of trouble looping. I am to first design a simple applet to build a house, for which I have the code below:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Polygon;
public class Houseref extends Applet
{
public void paint (Graphics page)
{
Polygon poly = new Polygon(); // Roof Polygon
poly.addPoint (50,90);
poly.addPoint (150, 50);
poly.addPoint (250, 90);
page.setColor (new Color(218,165,32)); // Custom brown color
page.fillPolygon (poly);
page.setColor (Color.black);
page.drawLine (50, 90, 150, 50); // Roof outline
page.drawLine (150, 50, 250, 90);
page.setColor (Color.yellow);
page.fillRect (50, 90, 200, 100); // House base with houseColor
page.setColor (Color.black);
page.drawRect (50, 90, 200, 100); // House outline
page.setColor (Color.black);
page.fillRect (75, 110, 30, 25); // Window 1
page.fillRect (190, 110, 30, 25); // Window 2
page.setColor (Color.blue);
page.drawLine (75, 123, 105, 123); // Window Frame 1
page.drawLine (89, 110, 89, 134);
page.fillRect (70, 110, 5, 25); // Shutter 1
page.fillRect (105, 110, 5, 25); // Shutter 2
page.drawLine (75+115, 123, 105+115, 123); // Window Frame 2
page.drawLine (89+115, 110, 89+115, 134);
page.fillRect (70+115, 110, 5, 25); // Shutter 3
page.fillRect (105+115, 110, 5, 25); // Shutter 4
page.setColor (Color.blue);
page.fillRect (130, 150, 35, 40); // Door
page.setColor (Color.red);
page.fillOval (155, 170, 4, 4); // Door knob
}
}
Now I need to create a loop that iterates 5 times, each time the new house must be in a different color and in a different location. I'm having trouble with understanding how to get an applet to loop. Any help is appreciated!
You don't loop an applet. You loop within an applet, as arg0's answer illustrates.
You've used magic numbers all through your paint method. You need to change the magic numbers to fields so you can change the variables.
The first thing you need to do is refactor your paint method so that you have lots of little methods. You should have a drawWall method, a drawRoof method, a drawDoor method, and a drawWindow method you call twice.
I'm assuming by different color houses you mean the wall should be different colors. You pass the color to the wall method you create as a parameter.
Here's a refactored drawWall method, so you can see what I'm talking about. You'll need to break up the rest of your paint method this way.
private void drawWall(Graphics page, Color color, int x, int y, int width,
int height) {
page.setColor(color);
page.fillRect(x, y, width, height); // House base with houseColor
page.setColor(Color.black);
page.drawRect(x, y, width, height); // House outline
}
The Rectangle class would be a good way to pass x, y, width, and height values to the method.
Here's a loop that iterates 5 times.
for(int i = 0; i < 5; i++){
/* Your_code_here */
}
I hope this helps, please tell me if it doesn't.

setBackground statement not working?

import javax.swing.JApplet;
import java.awt.*;
public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
public void paint (Graphics page)
{
final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
page.setColor (Color.black);
page.fillOval(MID-10, TOP+10, 5, 5);
page.fillOval(MID+5, TOP+10, 5, 5);
page.drawArc(MID-10, TOP+20, 20, 10, 190, 160); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
}
}
This is all the code. The setBackground is stated after I declare the two final variables, thanks in advance, I got this code from a book, "Java Software Solutions", I looked over it over and over, and no luck :/ thanks in advance :)
//<applet code='Snowman' width=300 height=200></applet>
import javax.swing.*;
import java.awt.*;
public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
public void init() {
add(new SnowmanPanel());
validate();
}
}
class SnowmanPanel extends JPanel {
final int MID = 150;
final int TOP = 50;
SnowmanPanel() {
setBackground (Color.cyan);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
page.setColor (Color.black);
page.fillOval(MID-10, TOP+10, 5, 5);
page.fillOval(MID+5, TOP+10, 5, 5);
page.drawArc(MID-10, TOP+20, 20, 10, 190, 160); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
}
}
General advice.
Don't paint in a top-level Swing component. Instead move the custom painting into a JPanel or JComponent and paint there. For custom painting in the latter, override paintComponent(Graphics)
When doing custom painting, remember to call super.paintComponent(Graphics)
Set the color in the constructor (or init()) rather than in the paint method.
Other advice
For a static image, you can also draw it to a BufferedImage and put the image in an ImageIcon in a JLabel. Which is simpler.
If this book has rushed into creating applets, toss it away. applets are much more difficult than standard apps., and should not be attempted by newbies.
Try this code
import java.awt.*;
import javax.swing.JApplet;
public class SnowMan extends JApplet
{
public SnowMan()
{
setBackground(Color.cyan);
}
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
#Override
public void paint(Graphics page)
{
final int MID = 150;
final int TOP = 50;
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor(Color.yellow);
page.fillOval(-40, -40, 80, 80); // sun
page.setColor(Color.white);
page.fillOval(MID - 20, TOP, 40, 40); // head
page.fillOval(MID - 35, TOP + 35, 70, 50); // upper torso
page.fillOval(MID - 50, TOP + 80, 100, 60); // lower torso
page.setColor(Color.black);
page.fillOval(MID - 10, TOP + 10, 5, 5);
page.fillOval(MID + 5, TOP + 10, 5, 5);
page.drawArc(MID - 10, TOP + 20, 20, 10, 190, 160); // smile
page.drawLine(MID - 25, TOP + 60, MID - 50, TOP + 40); // left arm
page.drawLine(MID + 25, TOP + 60, MID + 55, TOP + 60); // right arm
page.drawLine(MID - 20, TOP + 5, MID + 20, TOP + 5); // brim of hat
page.fillRect(MID - 15, TOP - 20, 30, 25); // top of hat
}
}
setBackground (Color.cyan);
It is working properly in my IDE. I also changed the color of background.It works nice and properly. No need to change the code.Make sure when you are creating the class.
The paint(Graphics) method is used only to paint the parameter (in your case page).
The application applet background color has already been processed by this stage.
That is why you can fix the problem by setting it in the constructor:
public Snowman()
{
this.setBackground(Color.cyan);
}
I think you need to use, getContentPane().setBackground()

Categories