GTK# on Windows: how to get desktop alpha-blending working? - gtk

Subj. I found this and translated into code below. But it shows only a dark-blue rectangle with no transparency at all.
class MainClass
{
public static void Main (string[] args)
{
Application.Init();
Gtk.Window w = new Gtk.Window(Gtk.WindowType.Toplevel);
w.ExposeEvent += new ExposeEventHandler(w_ExposeEvent);
w.DeleteEvent += new DeleteEventHandler(w_DeleteEvent);
w.Decorated = false;
w.BorderWidth = 20;
w.AppPaintable = true;
w.Colormap = w.Screen.RgbaColormap;
w.Show();
Application.Run();
}
static void w_ExposeEvent(object o, ExposeEventArgs args)
{
Context context = CairoHelper.Create(args.Event.Window);
context.SetSourceRGBA(0.0, 0.0, 50.0, 0.2);
context.Operator = Operator.Source;
context.Paint();
}
static void w_DeleteEvent(object o, DeleteEventArgs args)
{
Application.Quit();
}
}

Related

EventHandler and Rectangle JavaFx

I don't want to use an anonymous class.
How can I change / use my EventHandler on the rec ?
ColorswitchClass:
public class FarbtestEventHandlerAusserhalb extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Pane p = new Pane();
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(10.0, 20.0, 10.0, 10.0));
Rectangle rec = new Rectangle(150.0, 50.0, 100.0, 100.0);
p.getChildren().add(rec);
rec.setFill(Color.BLUE);
Button btn = new Button("Farbenwechsel");
btn.setOnAction(new MyEventHandler());
bp.setCenter(rec);
bp.setBottom(btn);
BorderPane.setAlignment(btn, Pos.CENTER);
Scene scene = new Scene(bp, 300.0, 280);
primaryStage.setTitle("Farb-Test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
MyEvendHandler Class
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.paint.Color;
public class MyEventHandler implements EventHandler<ActionEvent>{
public void handle(ActionEvent e)
{
int rot = (int) (Math.random() * 255.0);
int gruen = (int) (Math.random() * 255.0);
int blau = (int) (Math.random() * 255.0);
FarbtestEventHandlerAusserhalb feha = new FarbtestEventHandlerAusserhalb();
rec.setFill(Color.rgb(rot, gruen, blau));
}
}
The Error from Eclipse: rec cannot be resolved
I want to use the recto change the color
Because, MyEventHandler() doesn't know the "rec". Class MyeventHandler should be as follows,
public class MyEventHandler implements EventHandler<ActionEvent>{
Rectangle rec;
public MyEventHandler(Rectangle rec) {
this.rec = rec;
}
public void handle(ActionEvent e)
{
int rot = (int) (Math.random() * 255.0);
int gruen = (int) (Math.random() * 255.0);
int blau = (int) (Math.random() * 255.0);
FarbtestEventHandlerAusserhalb feha = new FarbtestEventHandlerAusserhalb();
rec.setFill(Color.rgb(rot, gruen, blau));
}
}
Usage :
btn.setOnAction(new MyEventHandler(rec));

Both client and server windows are blank

I am trying to test player position communication between a client and a server. I am doing this by building a game environment where the other player's position is also displayed. They connect properly, but both the windows are blank white. If I terminate one program only then the other displays the playground. Also in the action listener which listens for the timer only sends the data and doesn't proceed. Can I get help for displaying the windows properly and fixing the action listener?
Here is the client code :
public class SamplePlayerClient1
{
private static JFrame window = new JFrame("Sample Player Client 1");
private static class Ground extends JPanel {
private static final long serialVersionUID = 1L;
private int px = 50, py = 235, ex, ey, key;
private DataWriter writer;
private DataReader reader;
private ActionListener timerListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
writer.send(px, py);
System.out.println("data sent");
ex = reader.read().x;
ey = reader.read().y;
System.out.println("Data read");
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
};
private Timer animator = new Timer(30, timerListener);
private KeyAdapter keyListener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
py -= 8;
if(py < 0) {
py = 0;
}
} else if(key == KeyEvent.VK_DOWN) {
py += 8;
if(py > 430) {
py = 430;
}
} else if(key == KeyEvent.VK_LEFT) {
px -= 8;
if(px < 0) {
px = 0;
}
} else if(key == KeyEvent.VK_RIGHT) {
px += 8;
if(px > 455) {
px = 455;
}
}
}
};
Ground() throws UnknownHostException, IOException {
Socket soc = new Socket(InetAddress.getByName("192.168.0.3"), 9998);
System.out.println("client 2 connected");
writer = new DataWriter(soc);
reader = new DataReader(soc);
animator.start();
addKeyListener(keyListener);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
requestFocus();
g.setColor(Color.BLACK);
g.fillRect(px, py, 30, 30);
g.setColor(Color.RED);
g.fillRect(ex, ey, 30, 30);
}
}
private static class DataWriter {
private OutputStreamWriter writer;
private String point = "";
DataWriter(Socket soc) throws IOException {
writer = new OutputStreamWriter(soc.getOutputStream());
}
void send(int x, int y) throws IOException {
point = "" + x;
point += ",";
point += y;
point += "\n";
writer.write(point);
writer.flush();
}
}
private static class DataReader {
private InputStreamReader is;
private BufferedReader reader;
private String point = "";
DataReader(Socket soc) throws IOException {
is = new InputStreamReader(soc.getInputStream());
reader = new BufferedReader(is);
}
Point read() throws IOException {
point = reader.readLine();
int x = Integer.parseInt(point.split(",")[0]);
int y = Integer.parseInt(point.split(",")[1]);
return new Point(x, y);
}
}
public static void main(String [] args) throws UnknownHostException, IOException {
Ground ground = new Ground();
window.setContentPane(ground);
window.setSize(600, 600);
window.setLocation(0, 0);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
I don't see the problem but I did see that:
ex = reader.read().x;
ey = reader.read().y;
This reads two lines and throws away the y from the first and the x from the second.
Should be:
Point p = reader.read();
ex = p.x;
ey = p.y;
The peer might not generate a point for you to read every time you write one so the reading of the points from the peer should not occur in the event handler that writes a point to the peer.

Color Cycle C# Bunifu

I need help making my color cycle continue without stopping
I tried to loop the timer but that didn't work also im using bunifu
private void Form2_Load(object sender, EventArgs e)
{
panelColor(panel1, logo);
bunifuColorTransition1.ProgessValue = 0;
bunifuColorTransition1.Color1 = panel1.BackColor;
bunifuColorTransition1.Color2 = Color.FromArgb(210, 132, 120);
timer1.Start();
}
public void panelColor(Panel h, BunifuImageButton x)
{
panel1 = h;
logo = x;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (bunifuColorTransition1.ProgessValue < 100)
{
BunifuColorTransition bunifuColorTransition = bunifuColorTransition1;
int progessValue = bunifuColorTransition.ProgessValue;
bunifuColorTransition.ProgessValue = progessValue + 1;
panel1.BackColor = bunifuColorTransition1.Value;
logo.BackColor = bunifuColorTransition1.Value;
return;
}
timer1.Stop();
}

Error in printing a form

Hi guys I've got an error in this.printPreviewDialog1 (the project no contains a definition for printviewdialog and no extension for printviewdialog accepting a first argument of type Project print. I already added up the reference System.Printing and System.Windows.Form.Data Visualization. What I miss? Thanks
public partial class Print : Form
{
public Print()
{
InitializeComponent();
this.button1.Click += button1_Click;
this.printDocument1.PrintPage += printDocument1_PrintPage;
}
private void Print_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Document.DocumentName = "PictureTextDesignerFile1";
this.printPreviewDialog1.Size = new Size(800, 600);
this.printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float neededWidth = 700 ;
float neededHeight = 1100;
float availableWidth = 700;
float availableHeight = 1100;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Bitmap bmpPrint = null;
try
{
double multiplier = neededWidth / neededHeight;
double dblRef = availableWidth / availableHeight;
float zoom = 1;
if (multiplier >= dblRef)
{
zoom = availableWidth / neededWidth;
}
else
{
zoom = availableHeight / neededHeight;
}
bmpPrint = new Bitmap(this.Width, this.Height);
bmpPrint.SetResolution(300, 300);
this.DrawToBitmap(bmpPrint, new Rectangle(0, 0, bmpPrint.Width, bmpPrint.Height));
e.Graphics.Clip = new Region(e.MarginBounds);
e.Graphics.DrawImage(bmpPrint, e.MarginBounds.Left, e.MarginBounds.Top, Math.Min(neededWidth * zoom, availableWidth), Math.Min(neededHeight * zoom, availableHeight));
e.Graphics.Clip.Dispose();
}
catch
{
}
finally
{
if ((bmpPrint != null))
{
bmpPrint.Dispose();
bmpPrint = null;
}
}
}

I want to create objects (image orpicturebox) from a list menu then position or move them on a panel.... (like paint)

I try to create a class of object (picturebox), then calling it my main code to create other picturebox from a menu of different picturebox. I want to fix them on a panel to draw shcema or organigrams.
thanks
class _createpicture
{
public int p;
public int p_2;
PictureBox pictureBox2 = new PictureBox();
Panel panel1 = new Panel();
Color color = new Color();
Pen pen = new Pen(Color.Black, 1);
Bitmap flag = new Bitmap(40, 30);
ContextMenu cm = new ContextMenu();
public int x;
public int y;
public _createpicture () {
pictureBox2.Size = new Size(40, 30);
pictureBox2.MouseClick += new MouseEventHandler(pic_mouseclick);
pictureBox2.MouseMove += new MouseEventHandler(pic_mouseMouve);
MenuItem item_1 = cm.MenuItems.Add("Définir");
MenuItem item_2 = cm.MenuItems.Add("Supprimer");
item_1.Click += new EventHandler(item_1_Click);
item_2.Click += new EventHandler(item_2_Click);
}
private void item_1_Click(object sender, EventArgs e)
{
var myForm = new Form2();
myForm.Show();
}
private void item_2_Click(object sender, EventArgs e)
{
pictureBox2.Dispose();
}
public void _create_picture(PictureBox pictureBox2, int p, int p_2, Color color)
{
}
public void _add(Panel panel1, int p, int p_2, Color color)
{
panel1.Controls.Add(pictureBox2);
pictureBox2.BackColor = color;
Graphics flagGraphics = Graphics.FromImage(flag);
flagGraphics.DrawRectangle(pen, p, p_2, 20, 10);
flagGraphics.FillRectangle(Brushes.Red, p, p_2, 40, 30);
pictureBox2.Image = flag;
panel1.Controls.Add(pictureBox2);
}
public void pic_mouseclick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
pictureBox2.ContextMenu = cm;
}
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
public void pic_mouseMouve(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox2.Left += (e.X - x);
pictureBox2.Top += (e.Y - y);
}
}}
public partial class Form1 : Form
{
_createpicture _createpicture10 = new _createpicture();
.
.
.
.
if (radioButton2.Checked) {
_createpicture10._create_picture(picturebox10, e.X, e.Y, Color.Red); _createpicture10._add(this.panel1, e.X, e.Y, Color.Red);
}
}