How to Change Background Color on Processing? - text

I'm still extremely new to processing and I'm just playing around with it right now. I was hoping to find out how to change the background color between two colors, particularly between white and black, when I click my mouse. I found a code online that has the background color change between several different colors but I can't seem to figure out how the bg color can be changed between two colors. I would particularly like what 'col+=' and 'col%=' represent because I can't seem to find it in the processing tutorial. Please help me! Thank you!
Below is the code that I found.
void setup() {
size(600,400);
smooth();
colorMode(HSB);
}
int col = 0;
void draw() {
background(col,255,255);
}
void mousePressed(){
col+=20;
col%=255;
println(col);
}

"x += y" is shorthand for "x = x + y" and, likewise, "x %=y" is shorthand for "x = x % y" (where % is the modulo operator).
I'm going to assume that you wanted to ask is "how do I change the background from one colour to another, and then back again"; there's two basic ways to do this.
1: set up two (or more) reference colours, an extra "current" colour, and then change what 'current' points to, drawing the background off of that:
color c1 = color(255,0,0), c2 = color(0,0,255), current;
void setup() { current = c1; }
void draw() { background(current); }
void mousePressed() { if(current==c1) { current = c2; } else { current = c1; }}
Every time you click, the program checks which of the two colours "current" points to, and then instead points it to the other colour.
2: set up one colour, and apply some operation that is modulo in 1, or 2, or ... steps:
color c = color(255,0,0);
void draw() { background(c); }
void mousePressed() { c = color( red(c), (green(c)+50)%255, blue(c)); }
Every time you click, the colour "c" gets its green component increased by 50, and then modulo-corrected for 255. So it'll cycle through: 0, 50, 100, 150, 200, 250, 300%255=45, 95, 145, 195, 245, 295%255=40, 90, etc.

Related

c++builder style override options

I'm having a problem with vcl styles overwriting custom colours. Is there a way to turn off styles when using onDrawItem, or detect when a style is on, and what the target colour will be?
Specifically, in this particular case, if the style is on the dark mode, text is made white, and in my case I need the background to be white. So white text against white background is invisible.
It looks like the pen color is being ignored and replaced with the style color, while background colors are correct.
Ideally, I'd like to ignore style stuff while in this particular draw routine, and just use my own colors. (Using C++Builder 11)
TIA
The code in question:
//TCheckListBox *clbxCategory;
void __fastcall TFormATB::clbxCategoryDrawItem(TWinControl* Control,
int Index, TRect &Rect, TOwnerDrawState State)
{
if(State.Contains(odSelected)) {
clbxCategory->Canvas->Pen->Color = clBlack;
clbxCategory->Canvas->Brush->Color = clActiveCaption;
clbxCategory->Canvas->RoundRect(Rect.Left, Rect.Top , Rect.Right, Rect.Bottom, 0, 50);
}
else {
clbxCategory->Canvas->Pen->Color = Colours[Index];
clbxCategory->Canvas->Brush->Color = Colours[Index];
}
clbxCategory->Canvas->Rectangle(Rect.Left, Rect.Top - 1, Rect.Right, Rect.Bottom + 1);
clbxCategory->Canvas->TextOut(Rect.Left, Rect.Top,clbxCategory->Items->Strings[Index]);
}

pshape processing multiple

I'm trying to make a PShape SVG multipy. I want a new shape created every time a variable (that I'm importing from a CSV file) changes. I tried using a for, but it doesn`t respect the variable range I'm giving it, it just creates as many SVGs as it wants. Basically what I'm trying to do is that if the variable indicates there are 21 data between an X rage, draw 21 copies of the SVG in a fixed distance between one and other.
Table table;
PShape tipi2;
PShape tipi3;
void setup() {
size (1875, 871);
table = loadTable("WHO.csv", "header");
tipi2 = loadShape("tipi-02.svg");
}
void draw() {
background(0);
for (TableRow row : table.rows()) {
int hale = row.getInt("Healthy life expectancy (HALE) at birth (years) both sexes");
}
tipi2.disableStyle();
noStroke();
for( int i = 0 ;i<=1800;i=i+33){
pushMatrix();
translate(0,89.5);
if(hale > 40 && hale < 60){
shape(tipi2,i,0);
popMatrix();
}
}
There are a couple of things that couple things that could be improved in your current code:
the hale variable's visibility (or scope) is only within this loop: for (TableRow row : table.rows()) {
the drawing styles (noStroke()/disableStyle(),etc.) don't change much therefore could e set once in setup() rather than multiple times a second in draw()
you could move the for loop from 0 to 1800 inside the for (TableRow row : table.rows()) { loop, but that might not be very efficient:
Here's what I mean:
Table table;
PShape tipi2;
PShape tipi3;
void setup() {
size (1875, 871);
table = loadTable("WHO.csv", "header");
tipi2 = loadShape("tipi-02.svg");
//this styles could be set once in setup, rather than multiple times in draw();
tipi2.disableStyle();
noStroke();
background(0);
for (TableRow row : table.rows()) {
int hale = row.getInt("Healthy life expectancy (HALE) at birth (years) both sexes");
for ( int i = 0; i<=1800; i=i+33) {
pushMatrix();
translate(0, 89.5);
//hale is visible within this scope, but not outside the for loop
if (hale > 40 && hale < 60) {
shape(tipi2, i, 0);
}
//popMatrix(); should be called the same amount of times as pushMatrix
popMatrix();
}
}
}
void draw() {
}

Having trouble moving a reticle around the screen while following Chili's Beginning DirectX tutorial

To begin with I am using the Chili Framework for lessons 1-15 as downloadable here:
http://www.planetchili.net/
I am using DirectX 9 on an old laptop running Windows XP SP3. I have set the Direct3D rendering to software in order to run the framework. I am using Visual Studio Express C++ 2010 with the first service pack installed.
This is the code I am having trouble with:
// Start moving reticle code
DrawReticle(itemLocX, itemLocY, 255, 255, 255);
if(itemLocX == pointA && itemLocX != pointAb)
{
itemLocX += 2;
}
else if(itemLocX == pointBc && itemLocX != pointDa)
{
itemLocX -= 2;
}
if(itemLocY == pointAb && itemLocY != pointBc)
{
itemLocY += 2;
}
else if(itemLocY == pointDa && itemLocX != pointA)
{
itemLocY -= 2;
}
// End moving reticle code
Now Chili's solution is to move along the y axis while checking for x, and x while checking for y. I may post that later, don't have it readily available. You can see it at the beginning of this video:
http://youtu.be/JEmwkQsi8l0
However I wanted to do this logically, as if I was walking the border along an invisible wall inside a box. I wanted it to make sense what was going on. But the cursor won't move, and I see no reason why it doesn't. Here is my game.h:
#pragma once
#include "D3DGraphics.h"
#include "Keyboard.h"
class Game
{
public:
Game( HWND hWnd,const KeyboardServer& kServer );
void Go();
private:
void ComposeFrame();
/********************************/
/* User Functions */
void DrawReticle(int xP, int yP, int cR, int cG, int cB);
/*
xP = x position,
yP = y position,
cR = color red,
cG = color green,
cB = color blue
*/
// TODO: User functions go here
/********************************/
private:
D3DGraphics gfx;
KeyboardClient kbd;
/********************************/
/* User Variables */
int pointA; // Starting at pointA (100, 100) - the top left
int pointAb; // Move from pointA to pointAb (700, 100) - the top right
int pointBc; // Move from pointAb to pointBc (700, 500) - the bottom right
int pointCd; // Move from pointBc to pointCd (100,500) - the bottom left
int pointDa; // Move from pointCd to pointDa (100,100) - the top left
/*
These points describe the process of starting, then four movements. The four points are A, B, C, D. We start at A, then go to B (pointAb, read as A to b), then go to C (pointBc, read as B to c), then go to D (pointCd, read as C to d) then go to A (pointDa, read as D to a).
This can be very confusing, because there are five varibles used. But if we drew it out there would only four points, as well as only four movements. The best way to think of it is that starting is itself a movement, and as you need a place to start from, it itself must have a point. Since you start at A, but haven't yet gone anywhere, pointA is our starting point. Once you start moving, you go from pointA to pointB. Now if we used pointB as our variable it would be confusing,because we would have to move from pointA to pointB to pointC to pointD and then back to pointA. Still five variables, one is repeating, but the first pointA describes where you start, and the last where you end. Since these are two different actions on the same point, I have elected to use two letter names for each of the points you move to, while the point you start at has a single letter name. It was the best way I could clearly think about this process.
*/
int itemLocX; // Initial position of item on the x axis
int itemLocY; // Initial position of item on the y axis
int reticleX; // Initial position of reticle on the x axis
int reticleY; // Initial position of reticle on the y axis
// TODO: User variables go here
/********************************/
};
Here is my game.cpp:
#include "Game.h"
Game::Game( HWND hWnd,const KeyboardServer& kServer )
: gfx(hWnd),
kbd(kServer),
itemLocX(100), // Initial position of item on the x axis
itemLocY(100), // Initial position of item on the y axis
reticleX(400), // Initial position of reticle on the x axis
reticleY(300), // Initial position of reticle on the y axis
pointA(100), // Movement from 0 to A, stopping at A
pointAb(700), // Movement from A to b, stopping at B
pointBc(500), // Movement from B to c, stopping at C
pointCd(700), // Movement from C to d, stopping at D
pointDa(500) // Movement from D to a, stopping at A
{}
void Game::Go()
{
gfx.BeginFrame();
ComposeFrame();
gfx.EndFrame();
}
void Game::DrawReticle(int xP, int yP, int cR, int cG, int cB)
/*
xP = x position,
yP = y position,
cR = color red,
cG = color green,
cB = color blue
*/
{
gfx.PutPixel(xP-5,yP,cR,cG,cB);
gfx.PutPixel(xP-4,yP,cR,cG,cB);
gfx.PutPixel(xP-3,yP,cR,cG,cB);
gfx.PutPixel(xP+3,yP,cR,cG,cB);
gfx.PutPixel(xP+4,yP,cR,cG,cB);
gfx.PutPixel(xP+5,yP,cR,cG,cB);
gfx.PutPixel(xP,yP,cR,cG,cB);
gfx.PutPixel(xP,yP-5,cR,cG,cB);
gfx.PutPixel(xP,yP-4,cR,cG,cB);
gfx.PutPixel(xP,yP-3,cR,cG,cB);
gfx.PutPixel(xP,yP+3,cR,cG,cB);
gfx.PutPixel(xP,yP+4,cR,cG,cB);
gfx.PutPixel(xP,yP+5,cR,cG,cB);
}
void Game::ComposeFrame()
{
// Start draw reticle code
DrawReticle(reticleX, reticleY, 100, 155, 255);
// End draw reticle code
// Start color change code
int yT = 200; // Border 200 pixels from top
int yB = 400; // Border 200 pixels from bottom
int xL = 300; // Border 200 pixels from left
int xR = 500; // Border 200 pixels from right
if(reticleX < xL || reticleX > xR) // Defining color change area for X
{
DrawReticle(reticleX, reticleY, 255, 255, 255);
}
if(reticleY < yT || reticleY > yB) // Defining color change area for Y
{
DrawReticle(reticleX, reticleY, 255, 255, 255);
}
// End color change code
// Start moving reticle code
DrawReticle(itemLocX, itemLocY, 255, 255, 255);
if(itemLocX == pointA && itemLocX != pointAb)
{
itemLocX += 2;
}
else if(itemLocX == pointBc && itemLocX != pointDa)
{
itemLocX -= 2;
}
if(itemLocY == pointAb && itemLocY != pointBc)
{
itemLocY += 2;
}
else if(itemLocY == pointDa && itemLocX != pointA)
{
itemLocY -= 2;
}
// End moving reticle code
// Start border code
if(reticleX < 6)
{
reticleX = 6;
}
else if(reticleX > 794)
{
reticleX = 794;
}
if(reticleY < 6)
{
reticleY = 6;
}
else if(reticleY > 594)
{
reticleY = 594;
}
// End border code
// Start speed change code
int cSpeed = 4; // Default cursor speed
if(kbd.EnterIsPressed()) // Change to high speed
{
cSpeed = 8;
}
if(kbd.SpaceIsPressed()) // Change to low speed
{
cSpeed = 1;
}
if(kbd.RightIsPressed())
{
reticleX += cSpeed;
}
if(kbd.LeftIsPressed())
{
reticleX -= cSpeed;
}
if(kbd.UpIsPressed())
{
reticleY -= cSpeed;
}
if(kbd.DownIsPressed())
{
reticleY += cSpeed;
}
// End speed change code
}
Now I should note here that this should be done without functions and only the basic C++ operators. That's as far as Chili has taught to this point. This is my second attempt to solve this myself, after hours thinking about it and working on it on paper. I'm stuck. Just not seeing it. I think there is a logic error here on my part. I want to understand where my thinking may be mistaken, but more than that, how to think correctly, like the computer, about this.
I am also open to advice regarding my coding style. If I am not being clear enough, or am doing something that should not become a bad habit - basically if there is something I should be doing differently in writing my code I would like to know about it.
Thank you for your help - it is very much appreciated!
I see how you have tried to do this. Personally you have over complexed it.
1: you don't need the != operator in your if statements.
2: try this:
if(itemLocX < 700)
{
itemLocX += 2;
}
3: This worked fine during testing. Another point is that the if statements could be in the wrong order. I changed it to the order in which it moved across the screen in. I have X Y X Y and you have X X Y Y. (unconfirmed) It executes the if statements in order. I have hard coded the answer. set them to variables if you really want to. Hope this helped!

GDI: How to fill RoundRect with color?

While the question title seems dumb, that's not exactly what I need. To fill whole area with color, one needs to select appropriate brush - that's trivial. But I want to fill upper half of it with different color, and bottom half of it with the different one. If it was the normal (not round) rectangle, I could draw two rectangles (with different brushes). But with RoundRect I don't have any ideas how to do it.
Here is what I need it for: I draw each node in my graph visualization with RoundRect, and those nodes should have several compartments (cells) that should be filled with different colors.
I hope you get the idea what I mean :)
If you have to use legacy GDI instead of GDI+, here I wrote you a function to draw such a (cell) as you needed I hope it is what you have expected !
The basic idea is to create upper and lower regions (which they were both full overlapping rounded rectangles, then each has one of its halves cut off)
I have prepared the above illustration to show how the cell could be produced. It's for the upper side only, but you should have got the idea of creating the lower one.
Here is a wrapping function to create the cell you need:
void DrawCell(HDC& hdc, const RECT& rcTarget,const HBRUSH& hbrUpper, const HBRUSH& hbrLower)
{
HRGN hRgnUpper = CreateRoundRectRgn(rcTarget.left, rcTarget.top, rcTarget.right, rcTarget.bottom, 42, 38);
HRGN hRgnLower = CreateRoundRectRgn(rcTarget.left, rcTarget.top, rcTarget.right, rcTarget.bottom, 42, 38);
HRGN hRgnCutFromUpper = CreateRectRgn(rcTarget.left, rcTarget.top + ((rcTarget.bottom - rcTarget.top) / 2), rcTarget.right, rcTarget.bottom);
HRGN hRgnCutFromLower = CreateRectRgn(rcTarget.left, rcTarget.top , rcTarget.right, rcTarget.bottom - ((rcTarget.bottom - rcTarget.top) / 2));
CombineRgn(hRgnUpper, hRgnUpper,hRgnCutFromUpper, RGN_DIFF);
CombineRgn(hRgnLower, hRgnLower,hRgnCutFromLower, RGN_DIFF);
FillRgn( hdc, hRgnUpper, hbrUpper);
FillRgn( hdc, hRgnLower, hbrLower);
DeleteObject(hRgnCutFromLower);
DeleteObject(hRgnCutFromUpper);
DeleteObject(hRgnLower);
DeleteObject(hRgnUpper);
}
call this function from within your WM_PAINT handler:
RECT rcTarget;
rcTarget.left = 20;
rcTarget.top = 20;
rcTarget.right = 275;
rcTarget.bottom = 188;
HBRUSH hRed = CreateSolidBrush( RGB(255, 0, 0) );
HBRUSH hGreen = CreateSolidBrush( RGB(0, 255, 0) );
DrawCell(hdc, rcTarget, hRed, hGreen);

I am beginner in j2me.In J2me Ticker function, How to apply differnt Color in Single Ticker?

*I am developing one j2me-Lwuit Project for Nokia s40 devices.I have some problem abuot ticker. I have apply Only one color for tiker.But i want differnt color to apply for single ticker.This is my code for Ticker:
Ticker tick;
String tickerText=" ";
Label lblIndice=new Label();
Label ticker=new Label("");
for (int i = 0; i < tickerIndiceData.size(); i++)
{
tickerText +=" "+tickerIndiceData.elementAt(i).toString();
tickerText +=" "+tickerValueData.elementAt(i).toString();
tickerText +=" "+"("+tickerChangeData.elementAt(i).toString()+")";
lblIndice.setText(" "+tickerIndiceData.elementAt(i).toString());
lblValue.setText(" "+tickerValueData.elementAt(i).toString());
double val=Double.parseDouble(tickerChangeData.elementAt(i).toString());
if(val>0)
{
ticker.getStyle().setFgColor(0X2E9F37);
}
else
{
ticker.getStyle().setFgColor(0XFF0000);
}
lblChange.setText(" "+"("+val+")");
}
System.out.println("TICKER==="+tickerText);
ticker.setText(tickerText);
ticker.getStyle().setFont(Font.createSystemFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL));
ticker.startTicker(50, true);*
LWUIT doesn't support different colors for a label (hence ticker) since that would require quite a bit of processing.
Implementing a ticker from scratch in LWUIT is pretty easy though. Just derive label and override paint as such:
public void paint(Graphics g) {
UIManager.getInstance().setFG(g, this);
Style style = l.getStyle();
Font f = style.getFont();
boolean isTickerRunning = l.isTickerRunning();
int txtW = f.stringWidth(text);
// update this to draw two strings one with the color that's already set and the
// other with the color you want
g.drawString(getText(), getShiftText() + getX(), getY(),style.getTextDecoration());
}

Resources